Solved

Control Name From Text

Posted on 2011-09-14
7
265 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

11 Experts available now in Live!

Get 1:1 Help Now