代码之家  ›  专栏  ›  技术社区  ›  Al Lelopath

无法以编程方式设置组合框选择

  •  0
  • Al Lelopath  · 技术社区  · 6 年前

    我无法以编程方式设置组合框中的选择。 我尝试设置各种属性(selectEditem、selectedText、selectedIndex),但组合框不显示名称。组合框的第一行(空白)被选中。属性设置的返回值正确。
    我做错什么了?

    ...
    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 回复  |  直到 6 年前
        1
  •  1
  •   NicoRiff    6 年前

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

    cmbEditContractorName.SelectedIndex = cmbEditContractorName.FindString(contractor.Name);
    
        2
  •  1
  •   k1dev    6 年前

    这是因为您需要从组合框本身的数据源获取要分配的对象引用。 对于组合框,我建议您使用 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 .