Results 1 to 6 of 6

Thread: Accessing controls by name [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Location
    Huddersfield, England
    Posts
    83

    Accessing controls by name [Resolved]

    How do I access a control on a form by name i.e. something on the lines of:

    frmMain.Controls("btn1").Text = "Hello"

    where the name will be dynamic e.g. "btn" & CStr(intCount).

    The Controls method requires an integer index, how do I do the lookup?
    Last edited by consciouspnm; Mar 5th, 2005 at 06:31 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Location
    Huddersfield, England
    Posts
    83

    Re: Accessing controls by name

    Found this code, it works but I don't know if it the best way of doing it:

    http://www.planetsourcecode.com/URLS...10/anyname.htm

    VB Code:
    1. Public Function GetControlByName(ByVal Name As String) As Control
    2.  
    3.         'now, why would I put a "_" in front of the name?
    4.         Dim info As System.Reflection.FieldInfo = Me.GetType().GetField("_" & Name, _
    5.         System.Reflection.BindingFlags.NonPublic Or _
    6.         System.Reflection.BindingFlags.Instance Or _
    7.         System.Reflection.BindingFlags.Public Or _
    8.         System.Reflection.BindingFlags.IgnoreCase)
    9.  
    10.         If info Is Nothing Then Return Nothing
    11.         Dim o As Object = info.GetValue(Me)
    12.         Return o
    13.  
    14.     End Function

    I can now say:

    VB Code:
    1. frmMain.GetControlByName("btn"+Cstr(intCount)).Text = "Hello"

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Accessing controls by name

    Hi,

    One way is to use an array of controls, e.g.

    Dim arrControls(,) As Control

    You can then add controls to the array at runtime and refer to them by their array index number

    You could also use Reflection.

    You might also be able to use the function CallByName(). It is supposed to be able to refer to Properties but I have only used it to refer to methods.

    See if you can understand MSDN Help on this point
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Location
    Huddersfield, England
    Posts
    83

    Re: Accessing controls by name

    I had a look at CallByName before but the first arg seems to be a fixed object, which obviously I need to generate in code.

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Accessing controls by name

    Hi,

    The first argument in CallByName is the container. i.e. the form (Me) or a panel or groupbox.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Accessing controls by name

    Hi,
    I've never tried it but I suppose you could put the CallByName code in a separate sub in the module and then call it, passing a parameter.

    EG the following works for calling a method

    [Highlight=VB]
    In a frm1 event

    ccc(frm1, "Test")

    Public Sub ccc(ByVal str1 As Object, ByVal str2 As String)
    Dim str3 As String = "Test"
    CallByName(str1, str2, CallType.Method)
    End Sub

    Public Sub Test()
    MessageBox.Show("It Works")
    End Sub
    [Highlight=VB]
    Last edited by taxes; Mar 3rd, 2005 at 05:58 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width