How to use a combobox
Add a combobox in a form, and resize it.
Then, in the code, we adjust the properties. I choose to do at the start of the form. You can also do the same thing.
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ComboBox1.BackColor = Color.Ivory ComboBox1.Enabled = True ComboBox1.Visible = True ComboBox1.FlatStyle = FlatStyle.Standard End Sub |
When the combobox is initialized and when the properties satisfies you, we can add words to the combobox. We could remain in the same function and start adding text right after.
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ComboBox1.BackColor = Color.Ivory ComboBox1.Enabled = True ComboBox1.Visible = True ComboBox1.FlatStyle = FlatStyle.Standard ComboBox1.Items.Add("John") ComboBox1.Items.Add("Peter") ComboBox1.Items.Add("Kevin") End Sub |
Now that you put information in the combobox, it is good idea to set an initial choice.
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ComboBox1.BackColor = Color.Ivory ComboBox1.Enabled = True ComboBox1.Visible = True ComboBox1.FlatStyle = FlatStyle.Standard ComboBox1.Items.Add("John") ComboBox1.Items.Add("Peter") ComboBox1.Items.Add("Kevin") ComboBox1.SelectedIndex = 2 'will display Peter End Sub |
You can also enhance the use of the combobox by sorting alphabetically with the command shorted.
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ComboBox1.BackColor = Color.Ivory ComboBox1.Enabled = True ComboBox1.Visible = True ComboBox1.FlatStyle = FlatStyle.Standard ComboBox1.Items.Add("John") ComboBox1.Items.Add("Peter") ComboBox1.Items.Add("Kevin") ComboBox1.SelectedIndex = 2 'will display Peter ComboBox1.Sorted = True End Sub |
Please note after you sort your combobox, the SelectedIndex is reseted.
ComboBox1.Sorted = True ComboBox1.SelectedIndex = 1 'will display Kevin |
To access the selected text in the combobox, we use the text property.
MsgBox(ComboBox1. Text) |
If you are tempted to do some manipulations with the items in the combobox, keep in mind that combobox are collections. They use the same feature than regular collection.
To add:
Combobox1.items.add = "Obama"
To get the number of items:
Combobox1.items.count
To remove item according to its index in:
Combobox1.items.Remove (2), for example, to remove the second element of the collection
Reference :
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :