如何通过编码从组合框中选择特定项目 [英] How do I select a particular item from combobox via coding

查看:119
本文介绍了如何通过编码从组合框中选择特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从包含'n'个项目的组合框中选择一个特定项目,以便我可以在表单运行时在组合框上显示该项目。

注意:我已经存储了'n'个项目在数组中然后使用数据源将它们添加到组合框中

如:

 comboBox1.DataSource = array1; 





代码示例:

 string [] array2 = new string [3] {10:00,16:00,22:00 }; 
string [] array1 = new string [3] {8:30,13:30,19:00};
if(no == 1 || no == 3)
{
comboBox1.DataSource = array1;
}
if(no == 2 || no == 4)
{
comboBox1.DataSource = array2;
}
if(visit == 1&& j!= -1)//这里有问题
{
comboBox1.SelectedIndex = j;
}
// j的值为1,因此它应该显示13:30为'no'= 1或3
但它的剂量





我尝试过:



i尝试过selectedindex,selectedvalue,selectedtext,selecteditem但没有任何作用它总是显示索引0项(即第一项)。

此外它的剂量给出错误但是剂量改变了组合框中显示的项目它始终是第一项

解决方案

如果您的ComboBox中的项目是字符串,您可以尝试:
comboBox1.SelectedItem =test1;

如果以上方法无效,请尝试使用以下方法

public static void SelectItemByValue(此ComboBox cbo,字符串值)
{
for( int i = 0; i< cbo.Items.Count; i ++)
{
var prop = cbo.Items [i] .GetType()。GetProperty(cbo.ValueMember);
if(prop!= null&& prop.GetValue(cbo.Items [i],null).ToString()== value)
{
cbo.SelectedIndex = i;
休息;
}
}
}
然后只使用方法:

ddl.SelectItemByValue(value);


也许你需要类似的东西:

 comboBox1.DataBindings.Add( new  System.Windows.Forms.Binding(  SelectedValue,bsCustomers,  id true )); 

看到这个问题: c# - 在绑定到数据源的组合框上设置SelectedItem - Stack溢出 [ ^ ]

此外,您在表单设计器中更改的某些属性可能存在问题,只需尝试使用新的For代码m和一个带有默认属性的 ComboBox


适用于我:

 public class MyObject 
{
public string Text {get;组; }
public int Value {get;组; }
}
private void MyButton_Click(object sender,EventArgs e)
{
MyObject [] data = new MyObject [] {new MyObject(){Text =abc,值= 1},
new MyObject(){Text =def,Value = 2},
new MyObject(){Text =ghi,Value = 3}};
myComboBox.DataSource = data;
myComboBox.ValueMember =Value;
myComboBox.SelectedValue = 2;





 myComboBox.SelectedItem = data [1]; 





 myCOmboBox.SelectedText =abc; 



那么我是什么?这样做与你不同?


I want to select a particular item from combobox containing 'n' items, so that i can display that item on combobox while form is running.
note: i have stored the 'n' items in an array and then added them to combobox using datasource
like :

comboBox1.DataSource = array1;



code sample:

 string[] array2 = new string[3] { "10:00", "16:00", "22:00" };
            string[] array1 = new string[3] { "8:30", "13:30", "19:00" };
            if (no == 1 || no == 3)
            {
                comboBox1.DataSource = array1;
            }
            if (no == 2 || no == 4)
            {
                comboBox1.DataSource = array2;
            }
            if (visit == 1 && j != -1)//here is the problem it
            {
                comboBox1.SelectedIndex = j;
            }
//j has a value say 1, so it should display 13:30 for 'no'=1 or 3
but it dosent



What I have tried:

i have tried selectedindex, selectedvalue, selectedtext, selecteditem but nothing works it always displays index 0 item (i.e first item).
Also it dosent gives an error but dosent change the displayed item in combobox its always the first item

解决方案

if the items in your ComboBox are strings, you can try:
comboBox1.SelectedItem = "test1";

If the above is not working then try with following method
 
public static void SelectItemByValue(this ComboBox cbo, string value)
{
    for(int i=0; i < cbo.Items.Count; i++)
    {
        var prop = cbo.Items[i].GetType().GetProperty(cbo.ValueMember);
        if (prop!=null && prop.GetValue(cbo.Items[i], null).ToString() == value)
        {
             cbo.SelectedIndex = i;
             break;
        }
    } 
}
Then just consume the method:

ddl.SelectItemByValue(value);


Maybe you need something like:

comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", bsCustomers, "id", true));

See this question: c# - Set SelectedItem on a combobox bound to datasource - Stack Overflow[^]
Also there might be a problem with some property you changed in the Forms designer, just try your code with a fresh Form and a ComboBox with default properties.


Works for me:

public class MyObject
    {
    public string Text { get; set; }
    public int Value { get; set; }
    }
private void MyButton_Click(object sender, EventArgs e)
    {
    MyObject[] data = new MyObject[] {new MyObject(){Text = "abc", Value = 1},
                                      new MyObject(){Text = "def", Value = 2},
                                      new MyObject(){Text = "ghi", Value = 3}};
    myComboBox.DataSource = data;
    myComboBox.ValueMember = "Value";
    myComboBox.SelectedValue = 2;


Or

myComboBox.SelectedItem  = data[1];


Or

myCOmboBox.SelectedText  = "abc";


So what am I doing that is different to you?


这篇关于如何通过编码从组合框中选择特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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