Solved

Identify textbox whose text matches combobox in vb.net

Posted on 2014-11-19
3
223 Views
Last Modified: 2014-11-20
Hi all.

I'm working on a vb.net form in Visual Studio 2012 that has 5 textboxes (txtbox1, txtbox2, etc.) and a combobox (mycombobox).

The combobox list shows the 5 values in each of the textboxes described above. After the end user selects an item from the combobox I want to display in a message box which textbox has the exact value. So the message box would display "txtbox1" if the text inside of it matched what was selected in the combobox.

Any ideas?

Thank you in advance.
0
Comment
Question by:printmedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 40453741
How are you populating the ComboBox? If you post that code it'll help in determining the correct textbox.
0
 

Author Comment

by:printmedia
ID: 40453752
Private Sub cmbSetMin_Enter(sender As System.Object, e As System.EventArgs) Handles cmbSetMin.Enter

        cmbSetMin.Items.Clear()

        If Not (String.IsNullOrWhiteSpace(txtQty2.Text)) Then cmbSetMin.Items.Add(txtQty2.Text)
        If Not (String.IsNullOrWhiteSpace(txtQty3.Text)) Then cmbSetMin.Items.Add(txtQty3.Text)
        If Not (String.IsNullOrWhiteSpace(txtQty5.Text)) Then cmbSetMin.Items.Add(txtQty5.Text)
        If Not (String.IsNullOrWhiteSpace(txtQty7.Text)) Then cmbSetMin.Items.Add(txtQty7.Text)
        If Not (String.IsNullOrWhiteSpace(txtQty9.Text)) Then cmbSetMin.Items.Add(txtQty9.Text)

        cmbSetMin.SelectedIndex = -1

    End Sub

Open in new window

0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40454205
		For Each ctl As Control In Me.Controls
			If TypeOf ctl Is TextBox AndAlso ctl.Text = ComboBox1.Text Then
				MessageBox.Show(ctl.Name)
			End If
		Next

Open in new window


This will loop through all the TextBox controls on the form. If you want to make sure that you check only for specific controls, add something in their Tag property (X in the following example) and go like this:

 
		For Each ctl As Control In Me.Controls
			If CStr(ctl.Tag)="X" AndAlso ctl.Text = ComboBox1.Text Then
				MessageBox.Show(ctl.Name)
			End If
		Next

Open in new window

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question