(1)设计以上界面,点击其中某项目,弹出对话框显示 每个项目的文字,注意列表框中的各个项目不是在设计属性框添加,通过编程方式在Form_load事件处理函数中添加代码完成

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace WindowsFormsApp1 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 21 { 22 MessageBox.Show(listBox1.SelectedItem.ToString()); 23 } 24 25 private void Form1_Load(object sender, EventArgs e) 26 { 27 listBox1.Items.Add("爱你一万年"); 28 listBox1.Items.Add("真的爱你"); 29 listBox1.Items.Add("晚秋"); 30 listBox1.Items.Add("开心做一出戏"); 31 } 32 } 33 }
(2)在form_load事件处理函数中完成 左边组合框各个项目名称的添加
当选择省份的时候 右边组合框 填充 相关的城市
省份:河北省、湖北省
河北省对应的城市:唐山市、石家庄市、邯郸市
湖北省对应的城市:武汉市、荆州市、十堰市
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace WindowsFormsApp1 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 private void Form1_Load(object sender, EventArgs e) 20 { 21 22 comboBox1.Items.AddRange(new string[] { "河北省","湖北省"}); 23 comboBox1.SelectedIndex = 0; 24 } 25 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 26 { 27 comboBox2.Items.Clear(); 28 switch (comboBox1.SelectedIndex) 29 { 30 case 0: 31 comboBox2.Items.AddRange(new string[] { "唐山市", "石家庄市", "邯郸市" }); 32 comboBox2.SelectedIndex = 0; 33 break; 34 case 1: 35 comboBox2.Items.AddRange(new string[] { "武汉市", "十堰市", "荆州市" }); 36 comboBox2.SelectedIndex = 0; 37 break; 38 } 39 40 } 41 42 private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 43 { 44 45 } 46 47 48 49 } 50 }
posted on
浙公网安备 33010602011771号