如何制作两个具有相同选定值的组合框 [英] How to make two combo box with same selected value
问题描述
我在windows中工作c#vs 2015
我有两个用于国家阿拉伯语和郡英语的组合框
我需要如果用户从组合框中选择值1从组合框中选择相同的值2
我的用户永久忘记并为组合框2选择错误的国家
我的代码如下:
I work in windows form c# vs 2015
I have two combo box for country Arabic and county English
I need if user select value from combo box 1 select same value from combo box 2
my user permanent forget and select wrong country for combo box 2
my code as below :
var dataSourceAr = new List<Country>();
dataSourceAr.Add(new Country() { Name = "اختر دوله", Value = 0 });
dataSourceAr.Add(new Country() { Name = "الاردن", Value = 1 });
dataSourceAr.Add(new Country() { Name = "الإمارات", Value = 2 });
dataSourceAr.Add(new Country() { Name = "السعوديه", Value = 3 });
dataSourceAr.Add(new Country() { Name = "البحرين", Value = 4 });
dataSourceAr.Add(new Country() { Name = "تونس", Value = 5 });
this.comboBox2.DataSource = dataSourceAr;
this.comboBox2.DisplayMember = "Name";
this.comboBox2.ValueMember = "Value";
var dataSourceEn = new List<Country>();
dataSourceEn.Add(new Country() { Name = "SelectCountry", Value = 0 });
dataSourceEn.Add(new Country() { Name = "Jordon", Value = 1 });
dataSourceEn.Add(new Country() { Name = "Emarate", Value = 2 });
dataSourceEn.Add(new Country() { Name = "Saudia", Value = 3 });
dataSourceEn.Add(new Country() { Name = "Bahreen", Value = 4 });
dataSourceEn.Add(new Country() { Name = "Tunisi", Value = 5 });
comboBox1.DataSource = dataSourceEn;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Value";
public class Country
{
public string Name { get; set; }
public int Value { get; set; }
}
如果用户从组合框1中选择值1,我需要从组合框2中自动选择值1
i需要强迫他在两个组合框中获得相同的值
如果他从com框中选择值3,从梳子中选择值3方框3
如果可能的话怎么做?
我尝试过:
so that i need if user select value 1 from combo box 1 automatic select value 1 from combo box 2
i need to force him to get same value in two combo box
IF HE select value 3 from com box 1 select value 3 from comb box 3
How to do that if possible ?
What I have tried:
How to make two combo box with same selected value
推荐答案
将SelectedIndexChanged
事件添加到dataSourceAr
组合框
AddSelectedIndexChanged
event to thedataSourceAr
combobox
private void dataSourceAr_SelectedIndexChanged(object sender, EventArgs e)
{
this.dataSourceEn.SelectedIndex = (sender as ComboBox).SelectedIndex;
}
这篇关于如何制作两个具有相同选定值的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!