Hello,

I am creating a toolstrip and then adding a toolstripcombobox to it. My code so far is as follows:

        Dim tsMenu As New ToolStrip
        tsMenu.Name = "tsMenu"
        tsMenu.Dock = DockStyle.Top
        tsMenu.Visible = True

        Dim tsComboBox As New ToolStripComboBox
        tsComboBox.Name = "tsComboBox"
        tsComboBox.Visible = True

        tsComboBox.Items.Add("Item 1")
        tsComboBox.Items.Add("Item 2")

        tsMenu.Items.Add(tsComboBox)

        Me.Controls.Add(tsMenu)

I can access the toolstrip. For example I can get the toolstrip name:

Me.Controls("tsMenu").Name

My question is how can I access the combobox, for example if I wanted to get the value of the combobox.

I hope I have managed to explain my problem.

Many thanks in advance,

Minko

Dani AI

Generated

Good callout by : a ToolStripComboBox is not a Control on the form, it is a ToolStripItem hosted inside a ToolStrip. That is why walking Form.Controls(...).Controls(...) returns Nothing. To reach it, look in the ToolStrip’s Items collection instead. Items holds all the ToolStripItem children, even when they are not visible. (learn.microsoft.com)

Two reliable ways to grab it by name anywhere in your form code:

  • Indexer by key: Items("YourComboName") returns the item whose Name matches the key. The Name is explicitly intended to be used as the lookup key. (learn.microsoft.com)
  • Search by key: Items.Find("YourComboName", searchAllChildren:=True) returns an array of matches you can cast.

Example that safely gets the current value without assuming a selection:

Dim hits = Me.mainStrip.Items.Find("filterBox", True)
Dim cbo As ToolStripComboBox = If(hits.Length > 0, TryCast(hits(0), ToolStripComboBox), Nothing)

If cbo IsNot Nothing Then
    Dim current As String =
        If(cbo.SelectedItem IsNot Nothing, cbo.SelectedItem.ToString(), cbo.Text)
    ' use current ...
End If

Notes you can apply immediately:

  • Prefer SelectedItem (or SelectedIndex) for a non-editable drop-down; fall back to Text if the box is editable. SelectedItem is the canonical way to read the selection. (learn.microsoft.com)
  • If you need ComboBox-only members like SelectedValue/DisplayMember, use the underlying control via the ComboBox property: cbo.ComboBox.SelectedValue, etc. (learn.microsoft.com)
  • If you want to react as the user changes selection, wire the event once when you create the control:
AddHandler cbo.SelectedIndexChanged, AddressOf OnFilterChanged

Private Sub OnFilterChanged(sender As Object, e As EventArgs)
    Dim cb = DirectCast(sender, ToolStripComboBox)
    Dim value = If(cb.SelectedItem IsNot Nothing, cb.SelectedItem.ToString(), cb.Text)
    ' handle change ...
End Sub

This aligns with ’s pointer to look under Items, while avoiding the null reference that hit. (learn.microsoft.com)

Recommended Answers

All 7 Replies

You can use some thing like this untested code:

Dim selectedValue As String = ""
If not tsComboBox.SelectedItem is Nothing then
    selectedValue = CStr(tsComboBox.SelectedItem)
End If

MsgBox("The selected value is '" & selectedValue & "'")

Hope this helps

Hey, thanks for replying.

The problem is I am trying to access it from a seperate sub. I was wondering if there was a way to access it similar to the way I would access the toolstrip, something like this:

Form1.Controls("tsMenu").Controls("tsComboBox").Name

However, the above does not work.

Minko

Probably because the default is to declare the Modifier property of the controls in a form as Private. Change it to Public to access to its properties from another form.

Then to create the new form Form2 and show it, you need to pass the Form1 be reference to the Form2 like:

Dim f2 as new Form2(byref Form1)
f2.Show()       

and in the Form2, you shoud change the

Public Sub New()
    InitializeComponent()
End Sub

to

Dim F1 as Form1
Public Sub New(ByRef TheF1 as Form1)
    InitializeComponent()
    F1 = TheF1
End Sub

Then, later, on Form2 you can use:

F1.tsComboBox.Name

Hope this helps

Hello,

Sorry but I do not think I have explained myself properly. the problem is that it is a toolstripcombobox and I am adding it to tsMenu and I can not find a way to access it after it has been added so I can get the selected text. When I try to access it to get the name for example:

Form1.Controls("tsMenu").Controls("tsComboBox").Name

I get the following error: Object reference not set to an instance of an object.

I hope I have managed to explain myself a bit clearer. Sorry for any confusion.

Many thanks for helping so far.

Minko

    Dim tsMenu As New ToolStrip
    tsMenu.Name = "tsMenu"
    tsMenu.Dock = DockStyle.Top
    tsMenu.Visible = True
    Dim tsComboBox As New ToolStripComboBox
    tsComboBox.Name = "tsComboBox"
    tsComboBox.Visible = True
    tsComboBox.Items.Add("Item 1")
    tsComboBox.Items.Add("Item 2")
    tsMenu.Items.Add(tsComboBox)
    Me.Controls.Add(tsMenu)

'access the combobox

  For Each c In tsMenu.Items
        If TypeName(c) = "ToolStripComboBox" Then
            Dim m As ToolStripComboBox = DirectCast(c, ToolStripComboBox)

            For Each itm In m.Items
                MsgBox(itm.ToString())
            Next
        End If
  Next

You can use the below code, without passing anything to Form2. The menutstrip and the combobox is accessible without changing the default modifier,Friend.

Form1.tsMenu.Items("tsComboBox").Name

instead of

Form1.Controls("tsMenu").Controls("tsComboBox").Name

If you want to do more than just access the name, i.e. add more items, get selectedtext, etc., from Form2, use

Dim NewComboBox As ToolStripComboBox = DirectCast(Form1.tsMenu.Items("tsComboBox"), ToolStripComboBox)

Many thanks to everyone who has helped. What I wanted is now working, so I will mark this as solved.

Once again,

Many thanks to everyone who helped,

Minko

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.