无法以编程方式设置ComboBox选择

时间:2018-06-12 12:54:06

标签: c# .net winforms

我无法以编程方式在ComboBox中设置选择。 我尝试设置各种属性(SelectedItem,SelectedText,SelectedIndex),但ComboBox不显示名称。选择空白的ComboBox的第一行。属性设置的返回值是正确的 我做错了什么?

...
this.bsConstructionContractors.DataSource = typeof(Contractor);
...
public partial class EditContractorDialog : Form
{
    public EditContractorDialog(Contractor contractor)
    {
        InitializeComponent();

        this.cmbEditContractorName.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", projectView.bsProject, "ContractorId", true));
        // new BindingSource is important here, so as to use a separate CurrencyManager
        // because bsConstructionContractors is shared with another ComboBox on another form
        this.cmbEditContractorName.DataSource = new BindingSource(projectView.bsConstructionContractors, null);
        this.cmbEditContractorName.ValueMember = "ContractorId";
        this.cmbEditContractorName.DisplayMember = "Name";

         cmbEditContractorName.Enabled = true;
         cmbEditContractorName.Focus();

        // None of the following cause the ComboBox to display Contractor.Name
        // The first entry in the ComboBox, which is a blank, is displayed
        object myObject = cmbEditContractorName.SelectedItem = contractor; 
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject: " + myObject.GetType()); // type is Contractor
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject: " + myObject); // myObject is the contractor

        object myObject2 = cmbEditContractorName.SelectedText = contractor.Name;
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject2: " + myObject2.GetType()); // type is String
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject2: " + myObject2); // myObject2 is the contractor.Name

        object myObject3 = cmbEditContractorName.SelectedIndex = 3; // arbitrary index, just to see if it would be selected
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject3: " + myObject3.GetType()); // type is Int32
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject3: " + myObject3); // myObject3 = 3
     }
}

public partial class Contractor : System.IComparable
{
    protected int id;
    protected string name;
    protected string licenseNumber;
    protected Project project;

    public virtual int ContractorId
    {
        get { return this.id; }
        set { this.id = value; }
    }

    public virtual string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

    public virtual string LicenseNumber
    {
        get { return this.licenseNumber; }
        set
        { this.licenseNumber = value; }
    }

    public virtual int CompareTo(object obj)
    {
        if (!(obj is Contractor))
        {
            throw new System.InvalidCastException("This object is not of type Contractor");
        }
        return this.Name.CompareTo(((Contractor)obj).Name);
    }

}

2 个答案:

答案 0 :(得分:1)

使用SelectedIndex按承包商名称查找索引:

cmbEditContractorName.SelectedIndex = cmbEditContractorName.FindString(contractor.Name);

答案 1 :(得分:1)

这是因为您需要从组合框本身的DataSource获取一个对象引用。 对于组合框,我建议您使用ObservableCollection<T>

cmbEditContractorName.DataSource = new ObservableCollection<Type>(your List<Type>);
cmbEditContractorName.SelectedItem = ((ObservableCollection<Type>)cmbEditContractorName.DataSource).FirstOrDefault(c=> c.yourProperty = "something"); // this will select the first item meeting your condition

您的代码无效,因为您为SelectedItem属性分配了组合框DataSource中不存在的对象的引用