1.11M Members

Getting combo box value

 
0
 

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

 
0
 

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

 
0
 

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

 
0
 

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

 
0
 

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

 
0
 
    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
 
0
 

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)
 
0
 

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

Question Answered as of 1 Year Ago by lolafuertes, tinstaafl and xerohomicide
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article