goodmorningsky Posted December 11, 2003 Posted December 11, 2003 Current ComboBox control doesn't have Locked Property as same as the Locked property in ComboBox in VB6. Locked= false : prevent user changing text in combo box. I tried to add this by inheriting ComboBox. and overried OnKeyPress. But, followings.. do work... Please help.. bool locked=false; public bool MyLocked{ set{locked = value;} } protected override void OnKeyDown(KeyEventArgs e) { if(locked) { e.Handled=true; } } second try : this looks worked but, not.. It doesn't work when user type fast.. private string tempText; private bool byKeyStroke=false; protected override void OnKeyDown(KeyEventArgs e) { tempText = this.Text; byKeyStroke = true; if(textLocked) { e.Handled=true; } } protected override void OnTextChanged(EventArgs e) { if(textLocked && this.byKeyStroke) { this.Text = tempText; byKeyStroke = false; } } Quote Sun Certified Web component Developer, Microsoft Certified Solution Developer .NET, Software Engineer
Roey Posted December 11, 2003 Posted December 11, 2003 I use the DropDown and DropDownList properties for allowing clients to edit combobox data Quote
techmanbd Posted December 11, 2003 Posted December 11, 2003 set the dropdownstyle property to dropdownlist of the combobox Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
goodmorningsky Posted December 11, 2003 Author Posted December 11, 2003 thanks.. But, those doesn't work as I need.. so, its there any ways doing it by handling event as I wrote previously. Quote Sun Certified Web component Developer, Microsoft Certified Solution Developer .NET, Software Engineer
pendragon Posted December 12, 2003 Posted December 12, 2003 The first example works if you use the KeyPress event instead of the KeyDown event Quote
goodmorningsky Posted December 12, 2003 Author Posted December 12, 2003 Thank you for considering Actually I tried with all 3 of Key events.. All don't work.. after e.Handle = true; still the OnTextChanged event called and text is modified. e.Handle = true; should prevent further event as I know..but, not.. Quote Sun Certified Web component Developer, Microsoft Certified Solution Developer .NET, Software Engineer
Roey Posted December 12, 2003 Posted December 12, 2003 Can I ask why setting the dropdown style doesn't work for you. I have lots of forms using that method, and found it worked OK. Quote
pendragon Posted December 12, 2003 Posted December 12, 2003 I ran a quick test to see if it was working on my machine and using the KeyPress worked, this is what I did private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (this.comboBox1.Tag.ToString() == "LOCK") { e.Handled = true; } } I used the tag property as a quick way of testing. This didn't allow me to type anything into the comboBox but it still allows you to select an item. If you don't want them to be able to select items then the easiest way to do this is by setting the enabled property to False, the only draw back is that it greys out the control. Quote
goodmorningsky Posted December 12, 2003 Author Posted December 12, 2003 Can I ask why setting the dropdown style doesn't work for you. I have lots of forms using that method, and found it worked OK. Thank you for your comment. the reason I don't want to use DropDownStyle is.. this.skyComboBox1.DropDownStyle=ComboBoxStyle.DropDownList; this.skyComboBox1.Text = "Current Text"; //this is not shown As shown above I can't set Text on Combobox. I want some text to be shown on text area.. but, after chaing DropDownList style I can't set text.. Quote Sun Certified Web component Developer, Microsoft Certified Solution Developer .NET, Software Engineer
goodmorningsky Posted December 12, 2003 Author Posted December 12, 2003 (edited) Thank you pendragon. You are absolutely right. I'm sorry I missed referring that I'm inheriting ComboBox class. I'm inheriting from ComboBox so that I can reuse it without handling event on every client. I can just set TextLocked property. This make exactly SkyComboBox as same as ComboBox in VB6. ( I dont' use the name 'Locked' property since VS.NET use it for Form modification lock functionality.) I don't know why this approach doesn't work, meaning if I use it inside of subclass. Please check following class.. and feel free to comment about other methods.. public class DriverClass{ private SkyComboBox skybox; .... public void NeedTextLocked(){ skybox.TextLocked = true; } } public class SkyComboBox:ComboBox{ private bool textLocked; public bool TextLocked{ set{ textLocked = value;} get{return textLocked;} } protected override void OnKeyPress(KeyPressEventArgs e) { if(textLocked) { e.Handled = true; //this doesn't work!!!!!!!!!!!!!!! } } //I created this since combo.SelectedItem = itemObject; //doesn't make the itemObject selected. public void SetSelectedItem(object item) { for(int i=0; i< this.Items.Count; i++) { if(this.Items[i] == item){ this.SelectedIndex = i; } } } //I created this since combo.SelectedText = "item Text" //doesn't make the item selected. public void SetSelectedText(string text) { text = text.Trim(); for(int i=0; i< this.Items.Count; i++) { Debug.WriteLine(this.Items[i].ToString()); if(this.Items[i].ToString().Equals(text)) this.SelectedIndex = i; } } } Edited December 12, 2003 by goodmorningsky Quote Sun Certified Web component Developer, Microsoft Certified Solution Developer .NET, Software Engineer
pendragon Posted December 13, 2003 Posted December 13, 2003 (edited) About the DropDownList, The only reason I know for it not setting the text is If it can't be found in the ComboBox's Item List. As for you class this is the test I did using System; using System.Windows.Forms; namespace MyComboBox { public class MyComboBox : ComboBox { private bool isLocked; public bool IsLocked { get { return this.isLocked; } set { this.isLocked = value; } } public MyComboBox() { } protected override void OnKeyPress(KeyPressEventArgs e) { base.OnKeyPress(e); if(this.isLocked == true) { e.Handled = true; } } } } This seems to be the same as yours but it works here, even with a button to toggle IsLocked (But as I said before it still lets me change the combobox by selecting an Item from the List). Have you done a messagebox to check the value of textLocked in the OnKeyPress event to make sure it is true, apart from that I don't know what to suggest :confused: To select the Index, Item and text on the combobox I use the following this.MyComboBox.SelectedIndex = this.MyComboBox.FindStringExact(text); To make it so that they can't select an Item in the ComboBox I added the following to the class private int iIndex = -1; protected override void OnSelectedIndexChanged(EventArgs e) { base.OnSelectedIndexChanged(e); if(this.isLocked == true) { this.SelectedIndex = this.iIndex; } else { this.iIndex = this.SelectedIndex; } } Edited December 13, 2003 by pendragon Quote
goodmorningsky Posted December 16, 2003 Author Posted December 16, 2003 Thanks.. Now I got it.. I forgot calling base method!!! I often forget what I have to do certainly...when coding^^--> very bugging... I fill like Dolry..(watch finding nemo for this name)^^;; Quote Sun Certified Web component Developer, Microsoft Certified Solution Developer .NET, Software Engineer
pendragon Posted December 16, 2003 Posted December 16, 2003 Glad to be of help and I know what you mean about forgetting things when coding i'm always doing it.:D Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.