Clarus Dignus
Clarus Dignus

Reputation: 3897

Basic solution for adding list item to combo box via code on load

What I need:

  1. I was a basic equivalent of a select box i.e. a combobox of dropdown list style (that preferably doesn't allow text input).
  2. I need to add the list items by code rather than the property box.

What I have:

Private Sub Form_Load ()
   ComboStaffMember.AddItem "John Murphy"
End Sub

...produces "...add item is not a member of system.windows.forms.comboxbox".

Private Sub Form_Load ()
   ComboStaffMember.Items.Add("John Murphy")
End Sub

...produces no result.

My question:

Why is the item not adding? The form name is FrmStaffLogIn and it's in Form1.vb. Should Form_Load correspond to either of these or is my code incorrect elsewhere?

Upvotes: 1

Views: 1648

Answers (3)

Clarus Dignus
Clarus Dignus

Reputation: 3897

Working code:

Private Sub FrmIdentCust_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ComboStaffMember.Items.Add("John Murphy")
End Sub

I was missing (sender As Object, e As EventArgs) Handles MyBase.Load.

Upvotes: 0

Rahul
Rahul

Reputation: 77926

Are you sure your code line ComboStaffMember.Items.Add("John Murphy") doesn't work? it should work just fine.

The Add() method On Item collection expects object parameter and string as well can be passed as argument to it. Like below [C# code sample]:

this.comboBox1.Items.AddRange(
                   new string[] {"SomeText","SomeOtherText","LastText"});

Also, you probably don't see any item cause you haven't set a default selected item. Just expand the dropdown and you will see the items. To set the default selected item

this.comboBox1.SelectedIndex = 0;

Upvotes: 1

Mohamed Bawaneen
Mohamed Bawaneen

Reputation: 101

Try to put combo add statement in following format in form load event :

   Private Sub Form_Load () 

   Me.ComboStaffMember.Items.Add(New DictionaryEntry("Text to be displayed", 1))

   End Sub

Upvotes: 1

Related Questions