Solved

Control Name From Text

Posted on 2011-09-14
7
270 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 85

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 85

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 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