winform学习第一天

Do You Love小程序

private void LoveMe_Click(object sender, EventArgs e)
        {
            //点击后爱你按钮显示
            MessageBox.Show("真的嘛,我也爱你啊");
            //关闭当前窗口
            //this.Close();
            Form2 form2 = new Form2();
            form2.ShowDialog();
            //form2.Show();
            
        }

        private void NoLoveMe_MouseEnter(object sender, EventArgs e)
        {
            //给按钮一个坐标,这个按钮活动的最大长宽就是窗体长宽减去按钮的长宽
            int x = this.ClientSize.Width - NoLoveMe.Width;
            int y = this.ClientSize.Height - NoLoveMe.Height;
            //给按钮一个随机的坐标
            Random r = new Random();
            NoLoveMe.Location = new Point(r.Next(0, x+1), r.Next(0, y+1));
        }

        private void NoLoveMe_Click(object sender, EventArgs e)
        {
            //点击不爱你按钮后显示
            MessageBox.Show("你点到也没用,我就是不喜欢你");
            //关闭主窗口
            this.Close();
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        //点击送花按钮后显示
        private void songhua_Click(object sender, EventArgs e)
        {
            MessageBox.Show("谢谢你送的❀");
        }
        //让送个锤子按钮移动
        private void songgechuiz_MouseEnter(object sender, EventArgs e)
        {
            //这个按钮的活动最大长宽是窗体长宽减去按钮的长宽
            int x = this.ClientSize.Width - songgechuiz.Width;
            int y = this.ClientSize.Height - songgechuiz.Height;
            //给按钮一个随机的坐标
            Random r = new Random();
            songgechuiz.Location =new Point(r.Next(0, x + 1), r.Next(0, y + 1));

















闹钟小程序:首先要创建label和timer两个工具

 public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //当窗体加载时,把当前时间赋值给label文本
            label1.Text = DateTime.Now.ToString();
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            //把当前时间赋值给label文本
            label1.Text=DateTime.Now.ToString();
        //如果当前时间是某小时某分钟某秒时,会发生什么
     if (DateTime.Now.Hour == 18 && DateTime.Now.Minute == 12 && DateTime.Now.Second == 1)
            {
                MessageBox.Show("时间到了,该学习了");
            }
        }

简单记事本小程序,首先创建用户和密码两个文本和文本框以及登录和重置按钮,然后创建记事本文本框和自动换行和保存两个按钮

  }
        /// <summary>
        /// 在文件加载时取消文本框的自动换行,以及让两个按钮和文本框隐藏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //取消文本框的自动换行
            textBox1.WordWrap = false;
            //让两个按钮和文本框隐藏
            buttonLogn.Visible = false;
            buttonRest.Visible = false;
            textBox1.Visible = false;
        }
        //登录按钮
        private void button1_Click(object sender, EventArgs e)
        {
            string name=textname.Text.Trim();
            string pwa = textpwa.Text;
            if (name == "admin" && pwa == "123456")
            {
                MessageBox.Show("欢迎登录记事本");
                //将登录和重置按钮隐藏
                buttonLoad.Visible = false;
                buttonRest.Visible = false;
                //将用户名和密码文本和文本框隐藏
                label1.Visible = false;
                label2.Visible = false;
                textname.Visible = false;
                textpwa.Visible = false;

                //将自动换行和保存两个按钮显示
                buttonLogn.Visible = true;
                buttonRest.Visible = true;
                //将文本框显示
                textBox1.Visible = true;

            }
            else
            {
                MessageBox.Show("用户名或密码错误,请重新输入");
                //将用户名和密码都清空
                textname.Clear();
                textpwa.Clear();
                //将焦点定在用户名框
                textname.Focus();
            }
        }
        //重置按钮
        private void button2_Click(object sender, EventArgs e)
        {
            //将用户名和密码清空
            textname.Clear();
            textpwa.Clear();
            //将焦点定在用户名框
            textname.Focus();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
        //自动换行按钮
        private void buttonLogn_Click(object sender, EventArgs e)
        {
            if (buttonLogn.Text == "自动换行")
            {
                //取消自动换行
                textBox1.WordWrap = true;
                //按钮变成取消自动换行
                buttonLogn.Text = "取消自动换行";
            }
            else if (buttonLogn.Text == "取消自动换行") 
            {
                //取消自动换行
                textBox1.WordWrap = false;
                //按钮变成自动换行
                buttonLogn.Text = "自动换行";
            }
        }
        //保存按钮
        private void buttonRest_Click(object sender, EventArgs e)
        {
using (FileStream fsWrite=newFileStream(@"C:\Users\SpringRain\Desktop\newnew.txt",FileMode.OpenOrCreate,FileAccess.Write)) 
            {
                //将文本框中的内容中空格删除然后赋值给一个字符串
                string str=textBox1.Text.Trim();
                //将文本框中的内容按照指定的编码格式转换成字节数组
                byte[]buffer=System.Text.Encoding.Default.GetBytes(str);
                fsWrite.Write(buffer, 0, buffer.Length);
            }
            MessageBox.Show("保存成功");
        }

学生登录界面,首先创建用户名和密码两个文本和文本框工具,还有学生和老师两个单选按钮

还有登录按钮和重置按钮

 private void buttonSign_Click(object sender, EventArgs e)
        {
            //如果用户选择老师或者学生后
            if (radioButton1.Checked || radioButton2.Checked)
            {
                //将用户输入的用户名和密码转换为字符串
                string name = textName.Text.Trim();
                string pwa = textPwa.Text;
                //如果选择的是学生
                if (radioButton1.Checked)
                {
                    //如果输入的用户名为student并且密码为123456,就显示学生登录成功
                    if (name == "student" && pwa == "123456")
                    {
                        MessageBox.Show("学生登录成功");
                    }
                    else//用户名不为student或者密码不为123456
                    {
                        MessageBox.Show("用户名或密码错误,请重新输入");
                        //将用户名和密码清空
                        textName.Clear();
                        textPwa.Clear();
                        //将焦点定在用户名框
                        textName.Focus();
                    }
                }
                else if (radioButton2.Checked)
                {

                    if (name == "teacher" && pwa == "123456")
                    {
                        MessageBox.Show("老师登录成功");
                    }
                    else
                    {
                        MessageBox.Show("用户名或密码错误,请重新输入");
                        textName.Clear();
                        textPwa.Clear();
                        textName.Focus();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择学生或者老师");
            }
//重置按钮
 private void buttonReset_Click(object sender, EventArgs e)
            {
                     //将用户名和密码清空
                        textName.Clear();
                        textPwa.Clear();
                        //将焦点定在用户名框
                        textName.Focus();
            }

MDI窗体设计:首先创建父类窗体和几个子类窗体,在窗体属性中的IsMdicontainer确定MDI窗体,添加生成子窗体和横向排列和竖行排列三个菜单

     private void 显示子窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //创建子窗口对象
            Form2 form2 = new Form2();
            //声明子窗口的父窗口是form1
            form2.MdiParent = this;
            //显示子窗口
            form2.Show();
            Form3 form3 = new Form3();
            form3.MdiParent = this;
            form3.Show();
            Form4 form4 = new Form4();
            form4.MdiParent = this;
            form4.Show();
            Form5 form5 = new Form5();
            form5.MdiParent = this;
            form5.Show();
        }

        private void 横向排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //排列子窗口
            LayoutMdi(MdiLayout.TileHorizontal);

        }

        private void 竖向排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

图片上一张,下一张翻动:首先创建picturebox和上一张和下一张button工具

  private void Form1_Load(object sender, EventArgs e)
        {
            //设置图片在图片盒子中的显示适应方式
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            //设置文件夹第一个在图片盒子中显示
            pictureBox1.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\picture\1.jpg");

        }
        //获得文件夹中所有路径
            string[] str = Directory.GetFiles(@"C:\Users\Administrator\Desktop\picture");
        //所有路径都存储在数组中,通过i++来转换下一张
            int i = 0;//只能放点击外面,因为放里面点击一次又会变成0,只能转换一次
        ///<summary>
        ///点击下一张
        /// </summary>
        /// <param name = "sender" ></ param >
        /// < param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            i++;
            //当跳到最后一张的时候,再点击就要跳到第一张
            if (i == str.Length)
            {
                i = 0;
            }
            //通过下标访问当前图片路径
            pictureBox1.Image = Image.FromFile(str[i]);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            i--;
            //当跳到第一张的时候,再点击就要跳到最后一张
            if (i < 0)
            {
                i = str.Length-1;
            }
            //通过下标访问当前图片路径
            pictureBox1.Image = Image.FromFile(str[i]);
        }

循环播放图片:创建几个picturebox和tiemr两个工具

 private void Form1_Load(object sender, EventArgs e)
        {  
            //在加载窗体时,调整图片在每个图片盒子中的大小
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox5.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox6.SizeMode = PictureBoxSizeMode.StretchImage;
            //在加载窗体时,在每个图片盒子中显示文件夹中第一个图片
            pictureBox1.Image = Image.FromFile(@"E:\picture\1.jpg");
            pictureBox2.Image = Image.FromFile(@"E:\picture\1.jpg");
            pictureBox3.Image = Image.FromFile(@"E:\picture\1.jpg");
            pictureBox4.Image = Image.FromFile(@"E:\picture\1.jpg");
            pictureBox5.Image = Image.FromFile(@"E:\picture\1.jpg");
            pictureBox6.Image = Image.FromFile(@"E:\picture\1.jpg");

        }
        //获得文件中所有路径储存在一个字符串数组中
        string[] vs = Directory.GetFiles(@"E:\picture");
        int i = 0;
        Random r = new Random();
        private void timer1_Tick(object sender, EventArgs e)
        {
            {
                //每隔一秒钟换一张
                i++;
                pictureBox1.Image = Image.FromFile(vs[r.Next(0, vs.Length)]);
                pictureBox2.Image = Image.FromFile(vs[r.Next(0, vs.Length)]);
                pictureBox3.Image = Image.FromFile(vs[r.Next(0, vs.Length)]);
                pictureBox4.Image = Image.FromFile(vs[r.Next(0, vs.Length)]);
                pictureBox5.Image = Image.FromFile(vs[r.Next(0, vs.Length)]);
                pictureBox6.Image = Image.FromFile(vs[r.Next(0, vs.Length)]);
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值