How to make a non-editable Combo-box?
-
Sorry to ask such a basic question, actually I'm new to C#. I want to have a non-editable combo-box on my form (where user can choose from the items, but can not edit/type a new item into the combo. The .net 2005 forms editor gives me 2 choices (i) Create a list-box, which I don't want, as I need a drop-down (ii) Create a Combo-box, but that is always editable. In VS6, we could create a combo and then in its properties we could choose the type to be list box, but I could not find any such property in C#. Any help is appreciated.
It's better to know some of the questions than all of the answers.
Pravin. -
Sorry to ask such a basic question, actually I'm new to C#. I want to have a non-editable combo-box on my form (where user can choose from the items, but can not edit/type a new item into the combo. The .net 2005 forms editor gives me 2 choices (i) Create a list-box, which I don't want, as I need a drop-down (ii) Create a Combo-box, but that is always editable. In VS6, we could create a combo and then in its properties we could choose the type to be list box, but I could not find any such property in C#. Any help is appreciated.
It's better to know some of the questions than all of the answers.
Pravin. -
Sorry to ask such a basic question, actually I'm new to C#. I want to have a non-editable combo-box on my form (where user can choose from the items, but can not edit/type a new item into the combo. The .net 2005 forms editor gives me 2 choices (i) Create a list-box, which I don't want, as I need a drop-down (ii) Create a Combo-box, but that is always editable. In VS6, we could create a combo and then in its properties we could choose the type to be list box, but I could not find any such property in C#. Any help is appreciated.
It's better to know some of the questions than all of the answers.
Pravin.Use DropDownStyle property of combobox class.
Giorgi Dalakishvili #region signature my articles My blog[^] #endregion
-
Use DropDownStyle property of combobox class.
Giorgi Dalakishvili #region signature my articles My blog[^] #endregion
Thank you guys for the quick help :)
It's better to know some of the questions than all of the answers.
Pravin. -
Thank you guys for the quick help :)
It's better to know some of the questions than all of the answers.
Pravin.You are welcome :)
Giorgi Dalakishvili #region signature my articles My blog[^] #endregion
-
Sorry to ask such a basic question, actually I'm new to C#. I want to have a non-editable combo-box on my form (where user can choose from the items, but can not edit/type a new item into the combo. The .net 2005 forms editor gives me 2 choices (i) Create a list-box, which I don't want, as I need a drop-down (ii) Create a Combo-box, but that is always editable. In VS6, we could create a combo and then in its properties we could choose the type to be list box, but I could not find any such property in C#. Any help is appreciated.
It's better to know some of the questions than all of the answers.
Pravin.do this
private void MyComboBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < 256)
e.Handled = true;
};P user can´t press any key! just select You may try also use this for textboxes that you want to get just numbers (and backspace key)
private void MyTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48) || (e.KeyChar > 57))
e.Handled = true;
if (e.KeyChar == 8)
e.Handled = false;
}As you see this way you can avoid doind some validations like try{}catch{} Learn the ascii code to know the keys you are allowing or not :rolleyes:
nelsonpaixao@yahoo.com.br trying to help & get help
-
do this
private void MyComboBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < 256)
e.Handled = true;
};P user can´t press any key! just select You may try also use this for textboxes that you want to get just numbers (and backspace key)
private void MyTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48) || (e.KeyChar > 57))
e.Handled = true;
if (e.KeyChar == 8)
e.Handled = false;
}As you see this way you can avoid doind some validations like try{}catch{} Learn the ascii code to know the keys you are allowing or not :rolleyes:
nelsonpaixao@yahoo.com.br trying to help & get help
This won't handle the case of somebody using the mouse to paste data or cutting data for that matter. The solutions given previously are what the OP needed in his situation. In the more general scenario that you're preempting, to be successful you need to at least ... override OnKeyDown override OnTextChanged override WndProc and trap WM_PASTE and WM_CUT ... being careful to allow navigation keys like Home, End, Tab, Shift+Tab and the arrow keys if appropriate. If you just disable all keys the UI becomes very non-standard and the user will hate you and your software! ;)
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
This won't handle the case of somebody using the mouse to paste data or cutting data for that matter. The solutions given previously are what the OP needed in his situation. In the more general scenario that you're preempting, to be successful you need to at least ... override OnKeyDown override OnTextChanged override WndProc and trap WM_PASTE and WM_CUT ... being careful to allow navigation keys like Home, End, Tab, Shift+Tab and the arrow keys if appropriate. If you just disable all keys the UI becomes very non-standard and the user will hate you and your software! ;)
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)i see what you mean, i didn´t knew that, i learn it westerday :doh: sometimes we waste time (and code) in simply thinks!!! X| waste of time but works. The way i used, i learned it last year so i think (not sure) that you couldn´t use that dropdownlist propriety in c# 2005! So i drop it but still i have to use it in other cases (like textboxes the only accept numbers for exemple). (I am using it in a combobox also because i can´t handle, by now, that propriety for a particulat task. Maybe my next post ;P )
nelsonpaixao@yahoo.com.br trying to help & get help
-
This won't handle the case of somebody using the mouse to paste data or cutting data for that matter. The solutions given previously are what the OP needed in his situation. In the more general scenario that you're preempting, to be successful you need to at least ... override OnKeyDown override OnTextChanged override WndProc and trap WM_PASTE and WM_CUT ... being careful to allow navigation keys like Home, End, Tab, Shift+Tab and the arrow keys if appropriate. If you just disable all keys the UI becomes very non-standard and the user will hate you and your software! ;)
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)Thank you DaveyM69 for the tip. Although the solution proposed by nelsonpaixao is not what I was looking for and my current need is perfectly addressed by making DropdownStyle=DropDwonList, I will keep this solution in mind for some later day, e.g. when I need a textbox that only accepts numbers. Thank you nelsonpaixao. :)
It's better to know some of the questions than all of the answers.
Pravin.