如何在C#中编写组合框代码。 [英] How do I code for combo box in C#.

查看:100
本文介绍了如何在C#中编写组合框代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我是新来的,也是编码。我想制作一个小页面进入,但有一个小问题来找我。我的问题是......



我已经拿了2个组合框。例如,在第一个组合框中,项目是巴基斯坦,印度,斯里兰卡和美国,第二个组合中有伊斯兰堡,信德省,KPK,旁遮普邦,孟买,德里,Banglore,Kalkata,中部,东部,Sabaragamuwa,Uva,华盛顿,纽约,德克萨斯和佛罗里达。

当我在第一个组合框中选择PAKISTAN时,我想在选择INDIA时出现这个名字Islamabad,Sindh,KPK,Punjab,孟买,德里,Banglore,Kalkata将出现,当SRI LANKA被选中时,Central,Eastern,Sabaragamuwa,Uva将出现,当AMERICA被选中时,Washington,Newyork,Texas,Florida。



我不知道如何为此做代码。



我的尝试:



i为此尝试了一个简单的代码。就像在第一个组合框中选择每个国家名称一样,所有名称都将在第二个组合框中。

Hi! I am new here and coding also. I want to make small page for entry but there is a small problem came to me. my problem is...

I have take 2 combo box. For example in the 1st combo box the item is "Pakistan, India, Srilanka and America, and in the second one there is Islamabad, Sindh, KPK, Punjab, Mumbai, Del Banglore, Kalkata, Central, Eastern, Sabaragamuwa, Uva, Washington, Newyork, Texas and Florida.
When i select "PAKISTAN" in the first Combo box, i want to appear this names "Islamabad, Sindh, KPK, Punjab" when is Select "INDIA" , Mumbai, Del Banglore, Kalkata" will appear, when "SRI LANKA" is selected, "Central, Eastern, Sabaragamuwa, Uva" will appear and when "AMERICA" selected, "Washington, Newyork, Texas,Florida".

I don't know how to do code for this.

What I have tried:

i Have tried a simple code code for this. like every country name is selected in first combobox all the name will be in the second combo box.

推荐答案

另一种解决方法:
public Dictionary<string, List<string>> dctCombo; 

private void Form1_Load(object sender, EventArgs e)
{
    dctCombo = new Dictionary<string, List<string>>
    {
        {"Pakistan", new List<string> { "Islamabad", "Sindh", "KPK", "Punjab"} },

        {"America", new List<string> { "Washington", "New York", "Texas","Florida"} }
    };
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    comboBox2.DataSource = null;
    comboBox2.DataSource = dctCombo[comboBox1.SelectedItem.ToString()];
}


Fill second combobox in SelectedIndexChanged event of First Combobox




comboBox2.ValueMember = "id";  
comboBox2.DisplayMember = "capitalname";  
comboBox2.DataSource = datatable; 




Fill your datatable with where condition for country code/id


这篇关于如何在C#中编写组合框代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆