Editable ListBox
| Download : Source , Executable |
|
A ListBox is a read-only list of items. But let us suppose your application needs to edit an item in a listbox at runtime. This example will demonstrate a simple approach to create an editable listbox .ie. to change listbox items on the fly.. To edit an item in a listbox, all you need is a simple editbox.that overlays on a listbox item. The idea is to create a TextBox control and keep it hidden and show it whenever required. |
![]() |
| private System.Windows.Forms.TextBox editBox ;
private void Form1_Load(object sender, System.EventArgs e) |
|
Two events are added to the EditBox.
|
| editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver); editBox.LostFocus += new System.EventHandler(this.FocusOver); |
|
When the user hits the enter key or double clicks the mouse on any item in the list or clicks the F2 key, we display our editbox control and overlay the control on the item selected in the list. |
| private void listBox1_KeyPress(object sender , System.Windows.Forms.KeyPressEventArgs e) { if ( e.KeyChar == 13 ) CreateEditBox(sender); } private void listBox1_DoubleClick(object sender, System.EventArgs e) { CreateEditBox(sender); } private void listBox1_KeyDown(object sender, private void CreateEditBox(object sender) editBox.Locatio |
| When the control looses the focus or the enter key is pressed the editbox is hidden and the listbox item is updated from the text in the editbox. |
| private void FocusOver(object sender, System.EventArgs e) { listBox1.Items[itemSelected] = editBox.Text ; editBox.Hide(); } private void EditOver(object sender |













No comments yet... Be the first to leave a reply!