Solved

Control Name From Text

Posted on 2011-09-14
7
271 Views
Last Modified: 2012-05-12
Is it possible to access a control using a text value?  That is, can I do something like this:
For i As Integer = 1 To 5
    Dim sText = "btnAdd" & i.ToString()
    sText.Enabled = False  '<-- Yes, I know this specific line is wrong.  What is right?
Next

Open in new window


Hopefully you understand what I'm trying to do, and can help.
0
Comment
Question by:Clif
[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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 6

Expert Comment

by:yjchong514
ID: 36536967
R u mean assign text value to button object??
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 36536978
Call it through the Controls collection in the form:

Me.Controls(sText).Enabled = False

Open in new window


If you are calling a property that is common to all controls, that will be enough. If however you want to call a method or property that is specific to a Control, you will have to tell the compiler which control it is through a DirectCast:

DirectCast(Me.Controls(sText),TextBox).MaxLength = 7

Open in new window

0
 
LVL 10

Author Comment

by:Clif
ID: 36537268
It's not working, though I don't know why.

The error thrown is:
Object reference not set to an instance of an object.

I check the spelling, etc, and it's all correct.

The only thing that's different in the actualk app is the fact that I'm running the code in a subroutine in a module (where the form is passed to the subroutine), like this:
Public Sub disableContols(ByVal frmSource As frmMain)
    For i As Integer = 0 To frmSource.NumButtons '(NumButtons is a readonly property of frmSource/frmMain)
        Dim sButtonName As String = "picSiteImage" & i.ToString("00")
        DirectCast(frmSource.Controls(sButtonName), Button).Enabled = False
    Next
End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 36537340
The error is caused because the control is not being found.  

Instead of using Controls(someString), use Controls.Find():
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.find(VS.80).aspx
*Or use the Controls collection of the particular container directly, as in Panel1.Controls(someString)

Public Sub disableContols(ByVal frmSource As frmMain)
    Dim ctls() As Control
    For i As Integer = 0 To frmSource.NumButtons '(NumButtons is a readonly property of frmSource/frmMain)
        ctls = frmSource.Controls.Find("picSiteImage" & i.ToString("00"), True)
        If ctls.Length > 0 AndAlso TypeOf ctls(0) Is Button Then
            DirectCast(ctls(0), Button).Enabled = False
        End If
    Next
End Sub

Open in new window

0
 
LVL 40
ID: 36537543
Idle_Mind has a point. if the Button is included in a container, you wont't get it by looping through the form controls. You will need to loop through the container controls.

Which of the lines trigger the error?

Check the value of all the variables on that line. If one is Nothing, then it is the culprit.

It could be the control, but it could also be frmSource.

You could also temporarily try the following to make sure that the name you are looking for exists in the collection:
Public Sub disableContols(ByVal frmSource As frmMain)
    For i As Integer = 0 To frmSource.NumButtons '(NumButtons is a readonly property of frmSource/frmMain)
        'Dim sButtonName As String = "picSiteImage" & i.ToString("00")
        'DirectCast(frmSource.Controls(sButtonName), Button).Enabled = False
        Debug.WriteLine(frmSource.Controls(i).Name)
    Next
End Sub

Open in new window

The name of all the controls that your loop see will be displayed in the Output window.
0
 
LVL 10

Author Closing Comment

by:Clif
ID: 36537763
The find was the key.

The control was within a container within a container.  Too much hassle to find it without "Find"

Thanks.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36537778
*If they are all in the same container then use that containers name:

    Panel2.Controls("blah")

If they are in multiple containers, or you need the code generic for future changes, then use Find().
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal reports login after closing form 3 56
Need help with a query 14 54
Web page design problem 3 41
XML and Linq - reading fields in repeating nodes 3 25
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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