我有下面的代碼,我試圖用來填充組合框,但它沒有顯示我添加的對象的實際文本。組合框不顯示文本
internal partial class SortBox : UserControl {
private Field[] FieldReferences
...
internal Field[] Fields {
...
set {
this.FieldReferences = value;
this.cboFields.Items.Clear();
string NoneString = "(none)";
this.cboFields.Items.Add(NoneString);
this.cboFields.SelectedItem = NoneString;
foreach (Field Field in this.FieldReferences) {
MessageBox.Show(Field.ToString()); // <- This displays what I want displayed perfectly.
this.cboFields.Items.Add(Field);
}
}
}
...
}
public partial class Field : UserControl {
protected string LabelValue;
...
public override string ToString() {
return this.LabelValue;
}
}
這是我得到的;他們都是空白:
我在做什麼錯?
編輯:顯然,我的Field類繼承自UserControl。我做了一些測試,它顯然與類繼承自System.ComponentModel.Component的事實有關。
只是一些小問題:這將只在DropDownStyle是DropDownList(這正是我所需要的)時才起作用。否則,該列表將填充,但實際選定的項目將爲空白。此外,我用'新的SolidBrush(e.ForeColor)'代替'Brushes.Black'。你得到賞金。 –