Reputation: 1
I am trying to add items in a ComboBox out of which 2 are ReadOnly and 1 is an editable item.. The problem I am facing,is i cant come up with a solution to edit the selected editable item.. I have create a ComboBox and added 3 items and set the dropdownStyle to DropDownList.. Can anybody help me??? Thanks
Upvotes: 0
Views: 438
Reputation: 941337
It's not that easy. But possible, you can use the TextUpdate event to detect changes to the text. Then restore the original selection later, Control.BeginInvoke() is handy for that. This sample form worked well, drop a combobox on it in the designer. The second item is protected:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
comboBox1.TextUpdate += new EventHandler(comboBox1_TextUpdate);
comboBox1.Items.Add(new Content { text = "one" });
comboBox1.Items.Add(new Content { text = "two", ro = true });
comboBox1.Items.Add(new Content { text = "three" });
}
private void comboBox1_TextUpdate(object sender, EventArgs e) {
int index = comboBox1.SelectedIndex;
if (index < 0) return;
var content = (Content)comboBox1.Items[index];
if (content.ro) this.BeginInvoke(new Action(() => {
comboBox1.SelectedIndex = index;
comboBox1.SelectAll();
}));
}
private class Content {
public string text;
public bool ro;
public override string ToString() { return text; }
}
}
Note that you can't use DropDownList, that style doesn't allow editing.
Upvotes: 1