|
Article relates to
|
RadListBox for WinForms, Q3 2008 SP2
|
|
Created by
|
Nikolay Diyanov
|
|
Last modified
|
Jan 26, 2008
|
|
Last modified by
|
Nikolay Diyanov
|
HOW-TO
Edit RadListBoxItems' Text at runtime.
SOLUTION
In order to edit the RadListBoxItems' Text at runtime, we will use a RadTextBoxElement as an editor. When we double-click a RadListBoxItem, the RadTextBoxElement will be added to the Children collection of this item (i.e. it will be shown). This will allow you to type some text in it. Next, when you press the Enter key or the RadTextBoxElement loses its focus, this will remove the RadTextBoxElement (i.e. it will hide) and the changes will be saved. However, if you press the Esc key, the RadListBoxItem will get its old Text:
| public partial class Form1 : Form |
| { |
| RadTextBoxElement textBoxEditor; |
| |
| public Form1() |
| { |
| InitializeComponent(); |
| |
| this.radListBox1.Virtualized = false; |
| this.textBoxEditor = new RadTextBoxElement(); |
| this.textBoxEditor.UseGenericBorderPaint = false; |
| this.textBoxEditor.Padding = Padding.Empty; |
| this.textBoxEditor.TextBoxItem.LostFocus += new EventHandler(TextBoxItem_LostFocus); |
| |
| foreach (RadListBoxItem item in this.radListBox1.Items) |
| { |
| item.DoubleClick += new EventHandler(item_DoubleClick); |
| } |
| } |
| |
| private void item_DoubleClick(object sender, EventArgs e) |
| { |
| RadListBoxItem listBoxItem = sender as RadListBoxItem; |
| if (listBoxItem != null) |
| { |
| this.textBoxEditor.Text = listBoxItem.Text; |
| this.textBoxEditor.TextBoxItem.AcceptsReturn = true; |
| this.textBoxEditor.TextBoxItem.KeyDown += new KeyEventHandler(TextBoxItem_KeyDown); |
| listBoxItem.Children.Add(textBoxEditor); |
| this.ActiveControl = this.textBoxEditor.TextBoxItem.HostedControl; |
| } |
| } |
| |
| void TextBoxItem_LostFocus(object sender, EventArgs e) |
| { |
| CloseTextBoxAndSave(true); |
| } |
| |
| private void CloseTextBoxAndSave(bool saveChanges) |
| { |
| RadListBoxItem listBoxItem = (RadListBoxItem)this.radListBox1.SelectedItem; |
| if (listBoxItem.Children.Contains(this.textBoxEditor)) |
| { |
| listBoxItem.Children.Remove(this.textBoxEditor); |
| if (saveChanges) |
| { |
| listBoxItem.Text = this.textBoxEditor.Text; |
| } |
| } |
| } |
| |
| void TextBoxItem_KeyDown(object sender, KeyEventArgs e) |
| { |
| if (e.KeyCode == Keys.Enter) |
| { |
| CloseTextBoxAndSave(true); |
| } |
| else if (e.KeyCode == Keys.Escape) |
| { |
| CloseTextBoxAndSave(false); |
| } |
| } |
| } |
Below you can find solutions in both VB.NET and C# demonstrating the approach.
Please
Sign In
to rate this article.