0.controls类

窗体上的可视控件一般都是Control类的派生类,它实现了窗体控件的基本功能。

0.1 公有属性

image.png

anchor

  1. button1.Anchor = AnchorStyles.Top | AnchorStyles.Left;

Font

  button1.Font = new Font("黑体", 15, FontStyle.Regular);
  button2.Font = new Font("Times New Romance", 13, FontStyle.Regular);

borderstyle

0.2 公有事件

image.png
有点像方法体
image.png

0.3公有方法

image.png

            button2.Text = "按钮2";
            button1.Focus();

1、消息对话框

        private void button1_Click(object sender, EventArgs e)
        {

            //show 静态函数 (string text,string caption,MessageBoxButtons buttons,MessageBoxIcon icon)
            // show 返回值为DialogResult 
            // DialogResult是枚举类型

            //DialogResult dialogresult1 =MessageBox.Show("Love me?", "酸臭的爱情", MessageBoxButtons.YesNoCancel);

            //if (dialogresult1==DialogResult.Yes)
            //{
            //    button1.Text = "Love";
            //    button1.Size = new Size(100, 50);
            //}
//------------------------------------------------------------------------------------------
            DialogResult dia = MessageBox.Show("确定要去世博会吗?", "上海世博会", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (dia==DialogResult.Yes)
            {
                button1.Text = "报名成功!";
            }


        }

2、窗体

2.1 多文档窗体MDI

2.2 单文档窗体SDI

2.2.1 模式窗体/对话框 只有它关闭后,才能操作其他

form.ShowDialog();

        private void button3_Click(object sender, EventArgs e)
        {
            button3.Text = "调用模式窗体";
            button3.Font = new Font("Times new romance", 12, FontStyle.Regular);
            Form form = new Form();
            form.ShowDialog();

        }

image.png

2.2.2 无模式窗体 可随意切换到其他窗体

form1.Show();

        private void button4_Click(object sender, EventArgs e)
        {
            button4.Text = "调用无模式窗体";
            button4.Font = new Font("Times new romance", 12, FontStyle.Regular);
            Form form1 = new Form();
            form1.Show();

            //窗体控件中的字体
            form1.Font= new Font("Times new romance",10, FontStyle.Regular);


            TextBox textbox = new TextBox();
            textbox.Text = "无模式窗体的文本框";
            form1.Controls.Add(textbox);
        }

image.png

tab 视图|tab键顺序

private void Form1_Load(object sender, EventArgs e)
       {
           this.Text = "窗体";
           button3.Focus();         //focus
           button2.TabIndex = 2;      //tab顺序
           button3.TabIndex = 1;
           button1.TabIndex = 3;
       }

image.png

关闭整个窗体

        private void button5_Click(object sender, EventArgs e)
        {
            button5.Text = "退出";
            DialogResult dia=   MessageBox.Show("确定退出吗?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
            if (dia == DialogResult.Yes)
            {
                this.Close();//关闭整个窗体
            } 
        }