C # question about adding items to ListBox and non-repeated insertion

Source: Internet
Author: User

I have never used anything on Windows form before. The web is very simple. HTML tags support the value attribute of elements. I have used Delphi's combox and ListBox. Its items is a stringlist type thing. Each element is a string and can save an object at the same time; when used, a subtype of an object is often defined, and a new object is added to items. Finally, the release of this object is managed.
Open msdn and check the ListBox on Windows form.
After research, we assume that:
The ListBox. Items type is ListBox. objectcollection type.

The add function parameter is an object, not a string.

Therefore, if an element type is object, the system naturally needs tostring () to display this element ()
So you may do this:
Write a small class in the window and inherit from the object

Data member that contains two strings

One is the name and the other is the ID

Then, the tostring () function of the base class object is reloaded ()

Let the tostring function of this small class return name;
After this class is defined,

When a record is added to the list box,

New a small class Object

Assign the name and ID to two members.

Then listbox1.items. Add (yourobj );

This is probably enough.
Then

OBJ = listbox1.selected ......

OBJ. ID .....

Code:

///
/// List box Element Object
///
///
Public class myitem: Object
{
Public string name;
Public String ID;
Public override string tostring ()
{
// Todo: Add myitem. tostring to implement
Return name;
}
}
//----------------------------------------------------

Private void form1_load (Object sender, system. eventargs E)
{
Myitem Item1 = new myitem ();
Item1.id = "001 ";
Item1.name = "";
This. listbox1.items. Add (Item1 );

Myitem item2 = new myitem ();
Item2.id = "002 ";
Item2.name = "Heaven ";
This. listbox1.items. Add (item2 );


Myitem item3 = new myitem ();
Item3.id = "003 ";
Item3.name = "";
This. listbox1.items. Add (item3 );

Myitem item4 = new myitem ();
Item4.id = "004 ";
Item4.name = "";
This. listbox1.items. Add (item4 );
}
//------------------------------------------------------
Private void button#click (Object sender, system. eventargs E)
{
Myitem TMP = (myitem) This. listbox1.items [This. listbox1.selectedindex];

MessageBox. Show (TMP. ID );
}

 

This is true after tests. Haha
======================================Feelings ================ from a small place like ListBox, we can see that the. NET class library design is indeed elegant and can reflect the idea.
For example, the element in the list box is not a string, because the designer expected that the user should not use the list box only as a string, so he abstracts the item into the object but needs to normally display the text of the element, so he calls the tostring of the object when displaying the text, which is much more elegant.
Users (programmers) can use any data type as the element type, as long as this element supports tostring. net all types are inherited from the object, so tostring is a standard "interface" of items and item"
-----------------------------------------------------------------
Let's look at the ListBox in Delphi. Its items is a stringlist. Each element in the stringlist must first "be" a string and then the corresponding string can correspond to an additional object.
Compared with the. NET design, item does not have to be "a string (it can be of any type) as long as this type can provide a tostring interface to items.
The design of the latter is more in line with the object-oriented thinking.

**************************************** **

No repeated Inserts

View code

for(int   i=0;i<listBox1.Items.Count;i++)   
{
if(! listBox1.SelectedItems.Contains(listBox1.Items[i]))
{
MessageBox.Show(listBox1.Items[i].ToString());
}
}

 

Related Article

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.