Editable ListBox Control

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, i. e., 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.
 
 
  1. private System.Windows.Forms.TextBox editBox ;  
  2. private void Form1_Load(object sender, System.EventArgs e)  
  3. {  
  4.     editBox = new System.Windows.Forms.TextBox();  
  5.     editBox.Location = new System.Drawing.Point(0,0);  
  6.     editBox.Size = new System.Drawing.Size(0,0);  
  7.     editBox.Hide();  
  8.     listBox1.Controls.AddRange(new System.Windows.Forms.Control[ {this.editBox});   
  9.     editBox.Text = "";  
  10.     editBox.BackColor = Color.Beige;  
  11.     editBox.Font = new Font("Varanda" , 15 , FontStyle.Regular | FontStyle.Underline ,GraphicsUnit.Pixel);  
  12.     editBox.ForeColor = Color.Blue;  
  13.     editBox.BorderStyle = BorderStyle.FixedSingle;   
  14. }  
Two events are added to the EditBox.

KeyPress event to check if the enter key is pressed when the user has finished editing the item.

LostFocus event in case the user edits the item and instead of using the enter key to indicate the end of editing, the user clicks some other item in the list box.
  1. editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);  
  2. 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.
  1. private void listBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)  
  2. {  
  3.     if (e.KeyChar == 13)  
  4.         CreateEditBox(sender);  
  5. }  
  6. private void listBox1_DoubleClick(object sender, System.EventArgs e)  
  7. {  
  8.     CreateEditBox(sender);  
  9. }  
  10.   
  11. private void listBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)  
  12. {  
  13.     if (e.KeyData == Keys.F2)  
  14.         CreateEditBox(sender);  
  15. }  
  16. private void CreateEditBox(object sender)  
  17. {  
  18.     listBox1 = (ListBox)sender;  
  19.     itemSelected = listBox1.SelectedIndex;  
  20.     Rectangle r = listBox1.GetItemRectangle(itemSelected);  
  21.     string itemText = (string)listBox1.Items[itemSelected];  
  22.     editBox.Location = new System.Drawing.Point(r.X + delta, r.Y + delta);  
  23.     editBox.Size = new System.Drawing.Size(r.Width - 10, r.Height - delta);  
  24.     editBox.Show();  
  25.     listBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.editBox });  
  26.     editBox.Text = itemText;  
  27.     editBox.Focus();  
  28.     editBox.SelectAll();  
  29.     editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);  
  30.     editBox.LostFocus += new System.EventHandler(this.FocusOver);  
  31. }  
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. 
  1. private void FocusOver(object sender, System.EventArgs e)  
  2. {  
  3.     listBox1.Items[itemSelected] = editBox.Text;  
  4.     editBox.Hide();  
  5. }  
  6.   
  7. private void EditOver(object sender, System.Windows.Forms.KeyPressEventArgs e)  
  8. {  
  9.     if (e.KeyChar == 13)  
  10.     {  
  11.         listBox1.Items[itemSelected] = editBox.Text;  
  12.         editBox.Hide();  
  13.     }  
  14. }