3 Replies - 260 Views - Last Post: 07 February 2019 - 10:06 PM Rate Topic: -----

#1 ExitWound   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 27-December 09

Call ComboBox from String Name

Posted 07 February 2019 - 08:47 PM

Hey guys! It's been quite some time since I've had to call on you for help! So that's good!

I'm having a problem with calling a ComboBox by name from a string.

This is basically what I'm trying to do:

Dim a As String = "Combo"
Dim b As String = "Box1"

If (a & b).Text <> "" Then
MsgBox((a & b).Text)
End If


I honestly don't even know how I would start to do this, but that's as simple as I can put it.

I figured it would be something like this:

MsgBox(DirectCast(Me.Controls(a & b), ComboBox).Text)


But no dice there.

Any ideas?

EDIT: I finally figured it out!

For anybody looking for this answer, here is what I did:

Dim a As String = "Combo"
Dim b As String = "Box1"
Dim c As ComboBox = Me.Controls.Find(a & b, True)(0)
MsgBox(c.Text)


This post has been edited by ExitWound: 07 February 2019 - 09:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Call ComboBox from String Name

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14845
  • View blog
  • Posts: 59,243
  • Joined: 12-June 08

Re: Call ComboBox from String Name

Posted 07 February 2019 - 09:20 PM

If this is indeed a combobox then casting it as a textbox is a bad plan.

You would then also want the selected value/text and not just '.text'.
Was This Post Helpful? 0
  • +
  • -

#3 ExitWound   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 27-December 09

Re: Call ComboBox from String Name

Posted 07 February 2019 - 09:39 PM

View Postmodi123_1, on 07 February 2019 - 11:20 PM, said:

If this is indeed a combobox then casting it as a textbox is a bad plan.


Oops, my bad. I have so many comboboxes and textboxes that I got them mixed up. But you understand, haha.

Even this
MsgBox(DirectCast(Me.Controls(a & b), ComboBox).Text)

does not work.

View Postmodi123_1, on 07 February 2019 - 11:20 PM, said:

You would then also want the selected value/text and not just '.text'.


I'm not quite sure what you mean?

This post has been edited by ExitWound: 07 February 2019 - 09:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14845
  • View blog
  • Posts: 59,243
  • Joined: 12-June 08

Re: Call ComboBox from String Name

Posted 07 February 2019 - 10:06 PM

I'm not sure how you have things setup, but presumably your combobox is backed by a dataset.

Example of filling a dataset and making it the data source for a combobox.

      Dim _ds As DataSet
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

_ds = New DataSet
        _ds.Tables.Add()
        _ds.Tables(0).Columns.Add("lVal")
        _ds.Tables(0).Columns.Add("sVal")

        _ds.Tables(0).Rows.Add({1, "one"})
        _ds.Tables(0).Rows.Add({2, "two"})
        _ds.Tables(0).Rows.Add({3, "three"})

        ComboBox1.DataSource = _ds.Tables(0)
        ComboBox1.DisplayMember = "sVal"
        ComboBox1.ValueMember = "lVal"

        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList



Then you can cast the object from the control that you referenced by the text name of the control and tell it to display the selected value.
  Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Debug.WriteLine(CType(Me.Controls("Combobox1"), ComboBox).SelectedValue)

    End Sub

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1