The ComboBox in WinForm cannot be set to the same value as the DropDownList control in the WebForm property, and can be implemented by writing a new class.
1, first add a new class ComboBoxItem in Form1:
public class ComboBoxItem
{
private string _text=null;
Private Object _value=null;
public string Text{get{return this._text;} set{this._text=value;}}
public object Value{get {return this._value;} Set{this._value=value;}}
public override string ToString ()
{
return this._text;
}
}
2. Add in the Form1_Load function:
ComboBoxItem newitem = new ComboBoxItem ();
NewItem. Text = "ABC";
NewItem. Value = "1";
COMBOBOX1.ITEMS.ADD (NewItem);
3. Add in ComboBox1_SelectedIndexChanged:
ComboBoxItem myitem = (comboboxitem) Combobox1.items[combobox1.selectedindex];
MessageBox.Show (MyItem.Value.ToString ());
You can see the output effect!!!
Upstairs, I also used it in the project.
C # code?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
/// <summary>
/// ComboBox的Item
/// </summary>
public
class
TextValue
{
public
TextValue() { }
public
TextValue(
string
inText,
int
inValue)
{
this
.Text = inText;
this
.Value = inValue;
}
private
string
_text;
private
int
_value;
/// <summary>
/// 文本
/// </summary>
public
string
Text
{
set
{
this
._text = value; }
get
{
return
this
._text; }
}
/// <summary>
/// 值
/// </summary>
public
int
Value
{
set
{
this
._value = value; }
get
{
return
this
._value; }
}
}
public
partial
class
TempLateWave : Form
{
List<TextValue> PurposeList =
new
List<TextValue>();
public
TempLateWave()
{
InitializeComponent();
PurposeList.Add(
new
TextValue(
"精度"
, 0));
PurposeList.Add(
new
TextValue(
"线性度"
, 1));
PurposeList.Add(
new
TextValue(
"一致性"
, 2));
cbx.DataSource = PurposeList;
//cbx 就是ComboBox控件
cbx.DisplayMember =
"Text"
;
cbx.ValueMember =
"Value"
;
}
}
|
The ComboBox in WinForm sets both the text and value methods