About. NET in the WinForms inside the listbox to achieve data binding ...

Source: Internet
Author: User
Tags tostring
Data about. NET in the WinForms inside the listbox to achieve data binding ...

--------------------------------------------------------------------------------



In. NET, the list box control under WINDOW forms is developed with its own data binding and cannot change the listbox's items after it has been bound. But the actual development of the situation will often encounter to change, Here I offer a method. A custom control that inherits the ListBox control with development. Then in the inside provides two SortedList class's attribute, one can store the ID, one holds the text, thus solves the above question!!

The code for the control is as follows:

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Drawing;
Using System.Data;
Using System.Windows.Forms;

Namespace Flowmanage
{
<summary>
Summary description of the Syslistbox.
</summary>
public class SysListBox:System.Windows.Forms.ListBox
{
Private SortedList _sl=new SortedList ();
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;

Public Syslistbox ()
{
This call is required for the Windows.Forms form designer.
InitializeComponent ();

TODO: Add any initialization after the InitializeComponent call

}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Public SortedList DataValues
{
Get
{
return _SL;
}
Set
{
_sl=value;
}
}
public void AddItem (object key,object text)
{
if (this. Datavalues==null)
{
This. Datavalues=new SortedList ();
}
This. Datavalues.add (Key,text);
}
public void RemoveItem (int index)
{
This. Datavalues.removeat (index);
}
public void RemoveItem ()
{
This. Datavalues.clear ();
}
public void Boundlist ()
{
This. Items.clear ();
if (this. Datavalues!=null)
{
This. BeginUpdate ();
for (int i=0;i<this. datavalues.count;i++)
{
This. Items.Add (this. Datavalues.getbyindex (i). ToString ());
}
This. EndUpdate ();
}
}
#region Component Designer generated code
<summary>
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
</summary>
private void InitializeComponent ()
{
components = new System.ComponentModel.Container ();
}
#endregion
}
}
The code to call this control is as follows:

String Mkey=this.listcansel.datavalues.getkey (This.listCanSel.SelectedIndex). ToString ();
String Mtext=this.listcansel.datavalues.getbyindex (This.listCanSel.SelectedIndex). ToString ();
This.listSel.AddItem (Mkey,mtext);
This.listCanSel.RemoveItem (This.listCanSel.SelectedIndex);

THIS.LISTSEL.ITEMS.ADD (Mtext);
This.listCanSel.Items.RemoveAt (This.listCanSel.SelectedIndex);


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.