Differences between ComboBox. selecteditem and selectedtext

Source: Internet
Author: User

 

Differences between ComboBox. selecteditem and selectedtext

-> Selecteditem refers to the object of the selected item, not the text

When the selecteditem attribute is set to an object, ComboBox tries to make this object the currently selected object in the list. If this object is found in the list, it is displayed in the edit section of ComboBox and the selectedindex attribute is set to the corresponding index. If this object does not exist in the list, the selectedindex attribute retains its current value.

ComboBox. selectedtext: string of the currently selected text in the combo box. If dropdownstyle is set to comboboxstyle. dropdownlist, the return value is a null string ("").
ComboBox. selectedvalue: the object that contains the value of the data source member specified by the valuemember attribute.

 

String STR = (string) ComboBox. selecteditem; you can get the current value of ComboBox.
String STR = ComboBox. Text; you can also obtain the value of the current item.

 

ComboBox. selecteditem: This is an object
ComboBox. selectedtext: the number of characters selected, not the number of characters selected. For example, if you select "hello" with the mouse, ComboBox. selectedtext is the second character of the current item. For example, if the current item is "frog", the value of ComboBox. selectedtext is "green ". The dropdownstyle of ComboBox must be simple.
ComboBox. selectedvalue: return the value of the field specified by valuemember.

For example, set valuemember to ID and ComboBox to name, with the following records:
ID name
001 frog
When the current item of ComboBox is displayed as "frog", the value of selectedvalue is: 001
The current value of ComboBox is ComboBox. Text.

Certificate ----------------------------------------------------------------------------------------------------------------------------------------

Using system;
Using system. Windows. forms;
Using system. drawing;
Using system. collections;

Namespace mylistcontrolsample
{

Public class usstate
{
Private string myshortname;
Private string mylongname;

Public usstate (string strlongname, string strshortname)
{

This. myshortname = strshortname;
This. mylongname = strlongname;
}

Public String shortname
{
Get
{
Return myshortname;
}
}

Public String longname
{

Get
{
Return mylongname;
}
}

Public override string tostring ()
{
Return this. shortname + "-" + this. longname;
}
}

Public class listboxsample3: Form
{
Private ListBox listbox1 = new ListBox ();
Private textbox textbox1 = new Textbox ();

[Stathread]
Static void main ()
{
Application. Run (New listboxsample3 ());
}

Public listboxsample3 ()
{

This. autoscalebasesize = new size (5, 13 );
This. clientsize = new size (292,181 );
This. Text = "ListBox sample3 ";

Listbox1.location = new point (24, 16 );
Listbox1.name = "listbox1 ";
Listbox1.size = new size (232,130 );

Textbox1.location = new point (24,160 );
Textbox1.name = "textbox1 ";
Textbox1.size = new size (240, 24 );
This. Controls. addrange (new control [] {listbox1, textbox1 });

// Populates the list box using datasource.
// Displaymember is used to display just the long name of each State.

Arraylist usstates = new arraylist ();
Usstates. Add (New usstate ("Alabama", "Al "));
Usstates. Add (New usstate ("Washington", "wa "));
Usstates. Add (New usstate ("West Virginia", "WV "));
Usstates. Add (New usstate ("Wisconsin", "Wi "));
Usstates. Add (New usstate ("Wyoming", "WY "));

Listbox1.selectedvaluechanged + = new eventhandler (listbox?selectedvaluechanged );

Listbox1.datasource = usstates;
Listbox1.displaymember = "longname ";
Listbox1.valuemember = "shortname ";

}
Private void initializecomponent ()
{

}

Private void listbox1_selectedvaluechanged (Object sender, eventargs E)

{
If (listbox1.selectedindex! =-1)
Textbox1.text = listbox1.selectedvalue. tostring ();
}
}
}

Article transferred from: http://www.cnblogs.com/xuguangren/archive/2011/09/29.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.