winform 基础

目录

(1)string.Format()

(2)控制隐藏

(3)练习:求和

(4)练习:Email分析

(5)练习:页面上一张图,默认隐藏,用户在文本框中输入身份证(131415198105223221),点击按钮,如果大于18岁则显示图片。(取当前年份:DateTime.Now.Year)

(6)TextBox:Multiline、PasswordChar

     案例分析

(7)ComboBox

     案例分析

(1)string.Format()

     private void button1_Click(object sender, EventArgs e)
        {
            string name = this.textBox1.Text;
            //this.Text = name + ",你好!";            
this.Text = string.Format("{0},你好!", name); }

 

string.Format():

将指定的 String 中的每个格式项替换为相应对象的值的文本等效项。

image

 

 

(2)控制隐藏

     private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Hide();
        }

可以使textBox1隐藏

image

结果

image

 

 

(3)求和

简单的加法计算器  int.TryParse    MessageBox.Show    练习:加法计算器,考虑异常情况,聚焦。

    private void button1_Click(object sender, EventArgs e)
       {
           int a, b;
           if (int.TryParse(textBox1.Text, out a) == false)
           {
               MessageBox.Show("第1次数是不合法的");
               
                return;//不是退出程序,而是退出button1_Click函数}
           if (!int.TryParse(textBox2.Text, out b))
           {
               MessageBox.Show("第2次数是不合法的");
               return;
           }
           int c = a + b;
           txt_jg.Text = c.ToString();//结果
       }

image

 

 

(4)Email分析

     private void button1_Click(object sender, EventArgs e)
        {
            string email = this.textBox1.Text;
            string[] args = email.Split('@');
            if (args.Length !=2)
            {
                MessageBox.Show("Email非法!");
                return;
            }
            textBox2.Text = args[0];
            textBox3.Text = args[1];
        }

 

imageimage

 

 

(5)页面上一张图,默认隐藏,用户在文本框中输入身份证(131415198105223221),点击按钮,如果大于18岁则显示图片。(取当前年份:DateTime.Now.Year)

        private void btn_select_Click(object sender, EventArgs e)
        {
            int  i1;
            if (!isNumber(this.textBox1.Text.Trim()))
            {
                MessageBox.Show("身份证号为数字组成!");
                return;
            }
            if (!int.TryParse(this.textBox1.Text.Substring(6, 4), out i1))
            {
                MessageBox.Show("请正确输入身份证号!");
                return;
            }
            int b = DateTime.Now.Year - i1;
            if (b >= 18)
            {
                pictureBox1.Visible = true;
            }
            else            {
                pictureBox1.Visible = false;
                MessageBox.Show("Sorry,未满18拉!");
            }
        }
        //判断是否为数字        
private bool isNumber(string s) { int Flag = 0; char[] str = s.ToCharArray(); for (int i = 0; i < str.Length; i++) { if (Char.IsNumber(str[i])) { Flag++; } else                { Flag = -1; break; } } if (Flag > 0) { return true; } else            { return false; } }

 

imageimageimage

 

 

(6)TextBox:Multiline、PasswordChar

控件名要有含义、控件名前缀的“潜规则”。按钮Button:btn;文本框  TextBox:txt;复选框CheckBox:cb。

AppendText:向文本框追加文本。

image

image

 

 

案例1:

登录界面。登录错误三次退出程序。(易错点:局部变量与类变量)

退出程序Close()或者Application.Exit()

 1:          private int Error;  //只要Form1(程序)不关闭,Error就会一直保存,不像局部变量每次都变成0
 2:  private void btn_Enter_Click(object sender, EventArgs e)
 3:          {    
 4:              string user = this.txt_UserName.Text.Trim();
 5:              string pass = this.txt_PassWord.Text;
 6:              //StringComparison.OrdinalIgnoreCase 不区分大小写
 7:              if (user.Equals("admin", StringComparison.OrdinalIgnoreCase) && pass == "888888")
 8:              {
 9:                  MessageBox.Show("登录成功!");
10:              }
11:              else
12:             {
13:                  //int Error = 0;   局部变量每次运行完都会销毁并初始化,重新从0开始,所以错误
14:                  Error++;
15:                  if (Error>=3)
16:                  {
17:                       MessageBox.Show("登录次数过多,程序退出!");
18:                       Close();   //退出程序,等于Application.Exit();
19:  }
20:                  MessageBox.Show("登录失败!");
21:              }
22:          }
23:   

image

案例2:

修改密码。界面上有旧密码、新密码、重复新密码,假设旧密码是888888,两次输入新密码与旧密码不一样,并且新密码与重复密码必须一致。

        private void btn_Enter_Click(object sender, EventArgs e)
        {
            string pass = this.txt_pass.Text;
            string newpass = this.txt_newpass.Text;
            string newpass2 = txt_newpass_2.Text;
            if (pass != "888888")
            {
                MessageBox.Show("旧密码错误!");
                return;
            }
            if (newpass != newpass2)
            {
                MessageBox.Show("两次输入的密码不一致!");
                return;
            }
            if (pass == newpass)
            {
                MessageBox.Show("新密码不能等于旧密码!");
                return;
            }
            else
{
                MessageBox.Show("修改成功!");
            }    
        }
 

image

案例3:

在多行文本框中输入多行“姓名=成绩”格式的数据,要求输出成绩最高的学生姓名和成绩。

textBox.Lines:设置或获取文本框中的文本行)

private void button1_Click(object sender, EventArgs e)
        {
            string[] lines = textBox1.Lines;
            string maxName = "";
            int maxScore = -1;
            foreach (string line in lines)
            {
                string[] strs = line.Split('=');
                string name = strs[0];
                string Score = strs[1];
                int scoreNext = Convert.ToInt32(Score);
                if (scoreNext > maxScore) //下一个分数大于前一个,赋值
                {
                    maxName = name;
                    maxScore = scoreNext; 
                }
            }
            MessageBox.Show(
                   string.Format("{0}分数最高,成绩{1}", maxName, maxScore)
                   );
        }
 

4301EE09027647D1BA270C65735459A2image

(7)ComboBox

SelectedIndex :没有任何选中的时候是-1,否则是序号(0开始)

SelectItem:获取或设置当前选择中的第一个项。

image

备注

ListControl 类为 ListBoxComboBox 控件提供了共同元素的实现方法。

以下属性是数据绑定 ListBoxComboBox 的用户的首要考虑因素:DataSourceDisplayMemberSelectedValueValueMember 属性。

DisplayMember  获取或设置要为此 ListControl 显示的属性。 (继承自 ListControl。)

ValueMember   获取或设置一个属性,该属性将用作 ListControl 中的项的实际值。 (继承自 ListControl。)

image

例题:

两个ComboBox联动

image

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    comboBox2.Items.Clear();
                    comboBox2.Items.Add("A");
                    comboBox2.Items.Add("B");
                    comboBox2.Items.Add("C");
                    break;
                case 1:
                    comboBox2.Items.Clear();
                    comboBox2.Items.Add("1");
                    comboBox2.Items.Add("2");
                    comboBox2.Items.Add("3");
                    break;
                case 2:
                    comboBox2.Items.Clear();
                    comboBox2.Items.Add("ChongQing");
                    comboBox2.Items.Add("Beijing");
                    comboBox2.Items.Add("HuNan");
                    break;

                default:
                    break;
            }
        }

结果:

image

原文地址:https://www.cnblogs.com/tangge/p/2688583.html