Solved

Control Name From Text

Posted on 2011-09-14
7
266 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now