Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Control Name From Text

Posted on 2011-09-14
7
Medium Priority
?
275 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 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

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!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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 …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

963 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