SIGN UP MEMBER LOGIN:    
ARTICLE

Editable ListBox Control

Posted by Shripad Kulkarni Articles | Windows Controls C# July 01, 2002
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.
Reader Level:
Download Files:
 

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.

 
  
private System.Windows.Forms.TextBox editBox ;

private void Form1_Load(object sender, System.EventArgs e)
{
editBox =
new System.Windows.Forms.TextBox();
editBox.Location =
new System.Drawing.Point(0,0);
editBox.Size =
new System.Drawing.Size(0,0);
editBox.Hide();
listBox1.Controls.AddRang
e(
new System.Windows.Forms.Control[] {this.editBox});
editBox.Text = "";
editBox.BackColor = Color.Beige;
editBox.Font =
new Font("Varanda" , 15 , FontStyle.Regular
| FontStyle.Underline ,GraphicsUnit.Pixel);
editBox.ForeColor = Color.Blue;
editBox.BorderStyle = BorderStyle.FixedSingle;
 
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. 
 
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, System.Windows.Forms.KeyEventArgs e)
{
if ( e.KeyData == Keys.F2 )
CreateEditBox(sender);
}

private void CreateEditBox(object sender)
{
listBox1= (ListBox)sender ;
itemSelected = listBox1.SelectedIndex ;
Rectangle r = listBox1.GetItemRectangle(itemSelected);
string itemText = (string)listBox1.Items[itemSelected];
editBox.Location =
new System.Drawing.Point(r.X + delta , r.Y + delta ) ;
editBox.Size =
new System.Drawing.Size(r.Width -10 , r.Height- delta);
editBox.Show();
listBox1.Controls.AddRange(
new System.Windows.Forms.Control[] {this.editBox});
editBox.Text = itemText ;
editBox.Focus();
editBox.SelectAll();
editBox.KeyPress +=
new System.Windows.Forms.KeyPressEventHandler(this.EditOver);
editBox.LostFocus +=
new System.EventHandler(this.FocusOver);
}

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, System.Windows.Forms.KeyPressEventArgs e)
{
if ( e.KeyChar == 13 )
{
listBox1.Items[itemSelected] = editBox.Text ;
editBox.Hide();
}
}

share this article :
post comment
 

thank U its nice one

Posted by kalpana reddy Apr 01, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor