[VC++/C#/Visual Basic]Make combBox Read Only, Set dafault Value and Add items

visual c++ c# comboBox read only In this tutorial you will see some usually actions applied to comboBoxes namely how to make a combo box read only, how to set value of a combo box and how to add items on a combo box on .Net platforms namely Visual C++, C#, Visual Basic namely Visual Studio.

How to make a combobox read only

Making a comboBox Read Only on Visual C++ or C# or Visual Basic or Visual Studio headaches. Why? Because at the Properties menu we don’t have an Read Only Option.  So you are gonna to learn how to stop users modifying imput values on a comboBox and pur and simple make it Read Only.

As default a comboBox looks like this:

default combo box

And added values from Collection could be modified by the user:

combo box default

So if you want to stop users by changing values you have to:

Click on respective comboBox >> go to Properties Panel (left-down) >> DropDownStyle >> Select DropDownList as you can see on image:

drop down style combo box

And now comboBox is ReadOnly and looks like:

read only comboBox

And now the comboBox is read Only can not submit another values different from values from list.

How to set default value of combo box – Default Focused Value

On Visual C++ you cam simply add this code on Form1_Load (Form.h[Design] >> double-click on the main created window/form and you will get the code):

this->comboBox1->SelectedIndex = 2;              //it will set by default the second item
or
this->comboBox1->SelectedItem = "123";     //if item “123” exists it will be set by default

And now when program starts program will have default comboBoxes values.

On C# or Visual Basic is the same method but with syntax rules from that.

How to add items on comboBox

Form.h[Design] Tab must be selected before. All you have to do is to Click on comboBox in which you want to add items (from design tab) >> go to Properties Menu >> Items >> And click on Left Button. In the new window you have to insert items (one per line).

combo box add items

And the new window:

add items comboBox

And the output will be:

combo box validated output

Of course you can add items on a comboBox with this command:

this->comboBox1->Items->Add(systemstring);

*where systemstring is a value that have type System^

How to empty a comboBox on VC++

this->comboBox1->Items->Clear();

Conclusion

You know a few more procent how to validate a comboBox by setting default value of a comboBox, add/insert items/values on a comboBox and how to make a comboBox Read Only

Please let me know if you want to know more or if something is unclear.

Related posts:

One Comment on "[VC++/C#/Visual Basic]Make combBox Read Only, Set dafault Value and Add items"

  1. Sohel says:

    Thank you for a useful solution for C# application

Got something to say? Go for it!