点击ComboBox控件的一个选项另一个ComboBox的内容发生相应变化

如有两个ComboBox,希望点击第一个的选中项时,另外一个的内容作出相应的变化。

方法一:

<span style="font-size:14px;"><span style="font-size:14px;">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace testCB
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            if(comboBox1.SelectedItem=="植被")
            {
                comboBox2.Items.Add("草地");
                comboBox2.Items.Add("林地");
            }
            else if(comboBox1.SelectedIndex==1)
            {
                comboBox2.Items.Add("gengdi01");
                comboBox2.Items.Add("gengdi02");
                comboBox2.Items.Add("gengdi3");
            }
            else if (comboBox1.SelectedIndex == 2)
            {
                comboBox2.Items.Add("shandi01");
                comboBox2.Items.Add("shandi02");
                comboBox2.Items.Add("shandi3");
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("植被");
            comboBox1.Items.Add("耕地");
            comboBox1.Items.Add("山地");
        }
    }
}</span></span>
效果:


方法二(绑定到List<string>数据源):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace testCB
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //comboBox2.Items.Clear();
            if(comboBox1.SelectedIndex==0)
            {
                List<string> listCB01_0 = new List<string>();
                listCB01_0.AddRange(new string[] { "Apple", "Pear", "Banana", "Watermalen" });
                comboBox2.DataSource = listCB01_0;
            }
            else if(comboBox1.SelectedIndex==1)
            {
                List<string> listCB01_1 = new List<string>();
                listCB01_1.AddRange(new string[] { "豆角", "蒜苔", "彩椒", "青菜" });
                comboBox2.DataSource = listCB01_1;
            }
            else if (comboBox1.SelectedIndex == 2)
            {
                List<string> listCB01_2 = new List<string>();
                listCB01_2.AddRange(new string[] { "猪肉", "鸡肉", "羊肉", "牛肉", "鱼" });
                comboBox2.DataSource = listCB01_2;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<string> listCB01 = new List<string>();
            listCB01.Add("水果");
            listCB01.Add("蔬菜");
            listCB01.Add("肉");
            comboBox1.DataSource = listCB01;
        }
    }
}
效果:


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值