1

I have a c# visual studio program that uses combo boxes on a windows form. I would like to have EXACTLY the functionality of the .net 4.5 Combobox.IsEditable function:

http://msdn.microsoft.com/en-us/library/system.windows.controls.combobox.iseditable%28v=vs.110%29.aspx (I need the readonly=true, iseditable=false functionality)

Similar questions to this can be found dotted around the internet everywhere (eg. How to disable editing of elements in combobox for c#?) and the answer is always the same: "Set dropdownstyle to dropdownlist"

However, I cannot do this! In this program I frequently need to set and retrieve the text value of the combo box from within my code which is not possible with the dropdownlist style.

Is there any way I can get the .net 4.5 iseditable function into my .net 4 combobox?

Thanks in advance, Anya

Community
  • 1
  • 1
Anya Hope
  • 1,301
  • 1
  • 17
  • 33
  • In what way is it not possible with a DropDownList style? What have you tried? Check out the Selected* properties. I haven't used WinForms for a couple of years so don't remember which one it is, but I used to do that all the time. – simon at rcl Oct 22 '14 at 16:35
  • 1
    @Anya, When you say that you cannot set and retrieve the text value of the combo box when it's set to DropDownList style, why not? You can get/set it with comboBox.Text property. Works fine for me in the test I just did using .Net 4.0. Please elaborate on why this is an issue for you. Thanks. – blitz_jones Oct 22 '14 at 20:29
  • I don't quite know why combobox.text property isn't working for me in this instance. However, if I have my combobox set to 'dropdown', my code: combobox.Text = "group 1"; works correctly and when the form loads, the combobox text is set to 'group 1', however if I set the property to 'dropdownlist', when the form loads, there is no text in the combobox. On looking with breakpoints and trying to set the selectedindex property instead, the combobox actually throws an error whenever i try to set selectedindex to anything other than -1 (even if there are definitely items in the collection.) – Anya Hope Oct 23 '14 at 07:49

2 Answers2

0

You can disable editing in combo box by following code:

ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

Don't know that .net 4.5 functionality can be use in .net 4.0 but alternative is you can change framework of existing project (If you want to change) you can do by following way:-->

  1. Right Click on Project in Solution Explorer
  2. Go to Property
  3. Then in Application tab change Target framework:
  4. install other framework if there is no .Net 4.5
Vishnu S. Divetia
  • 305
  • 1
  • 2
  • 10
  • 2
    The OP specifically stated that .DropDownList style does not work for her situation. I've asked her to elaborate on why it does not work. – blitz_jones Oct 22 '14 at 20:38
0

I found a way around this issue but it does seem rather clumsy. I really don't know why the combobox.dropdownlist property is not working properly on my form, but as mentioned in the comment above, if the combobox is set to dropdownlist, if I try to set the .text, .selecteditem, .selectedvalue or .selectedtext properties of the combobox, they always revert to "". If I try to set the .selectedindex I get an error saying that I cannot set the index to anything other than -1. The error is: "InvalidArgument=Value of '0' is not valid for 'SelectedIndex' (but to be honest, this error also occurs when i try to set the selected index of my combobox when it is set to 'dropdown'.

Here is the code I used to mimic the 'Iseditable=false' property:

private void comboBox_Leave(object sender, EventArgs e)
    {
        //Called from customer group and customer tag combo boxes
        //Prevents any entered text that doesn't match the list from being saved
        ComboBox control = (ComboBox)sender;
        if(!control.Items.Contains(control.Text))
        {
            control.Text = "";
        }
        if (control.Text == "")
        {
            control.Text = "Select item";
        }
    }
Anya Hope
  • 1,301
  • 1
  • 17
  • 33