Solved

Trying to be dynamic with controls

Posted on 2006-11-23
15
168 Views
Last Modified: 2010-04-23

I need to be able to constuct this so i can access the control dynamically


me.uxbay1.text ="blah"

But the number might be different and i can pass it with a variable



so i need to do something like



me.uxbay & mynum & .text ="blah"


any ideas

cheers

0
Comment
Question by:davoman
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 10

Expert Comment

by:Kinger247
ID: 18002119
your could loop through the controls on the form and check its name ?

For each control as control in me.controls
  If control.name.toupper = "uxbay1".toupper Then
   '' your code here ...
  End If
Next
0
 
LVL 1

Author Comment

by:davoman
ID: 18002125
is there a more efficient way of doing this I have a heck of a lot of controls

regards
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 18002141
Try : Me.Controls.ContainsKey("uxbay1")
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 10

Expert Comment

by:Kinger247
ID: 18002152
sorry that won't work for you, it only returns a boolean value.
you may have to resort to the loop ;)
0
 
LVL 10

Accepted Solution

by:
Kinger247 earned 350 total points
ID: 18002171
although you could use :

Dim FoundControl() As Control = Me.Controls.Find("uxbay1", False)

Console.WriteLine(FoundControl(0).Name)
0
 
LVL 1

Author Comment

by:davoman
ID: 18002238

I need to be able to put a value in as per question above I have 24 uxbay controls numbered 1 to 24 and i am looking to write the code efficently

to acces one at a time
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 18002252
Well, in the background, the

Dim FoundControl() As Control = Me.Controls.Find("uxbay1", False)

will loop anyway.

Try:

Dim controlName as string

Dim FoundControl() As Control = Me.Controls.Find("Button1", False)
FoundControl(0).Text = "Your Data"
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 18002259
sorry to quick on the enter key ... this will work for you ...

        Dim ControlName As String = "uxbay1"

        Dim FoundControl() As Control = Me.Controls.Find(ControlName, False)
        FoundControl(0).Text = "Your Data"
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18004224
So, to add my two cents worth, since you want to get all 24 controls...

    Dim controlPrefix As String = "uxbay"

    For i As Integer = 1 To 24
      Dim ctl As TextBox = CType(Me.Controls.Find(controlPrefix & i.ToString, True)(0), TextBox)
      ctl.Text = "Your Data"
    Next i

One note... if you had a panel (or some other container) containing the uxbay, then you could loop through that container's Controls collection without worrying about the other controls on your form.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18004646
Controls.Find() only works in VB.Net 2005.

If you have an earlier version then you need to loop.  If the controls are inside a container (not the form directly) then you need a recursive search algorithm.

Something like...

    Dim c As Control
    Dim i As Integer
    For i = 1 to 24
        c = GetControlByName("uxbay" & i, Me)
        If Not IsNothing(c) Then
            c.Text  = “xxxx”
        End If
    Next i

Using the function:

    Private Function GetControlByName(ByVal ctlName As String, ByVal container As Control) As Control
        Dim c As Control
        For Each c In container.Controls
            If c.Name.ToLower.Equals(ctlName.ToLower) Then
                Return c
            ElseIf c.Controls.Count > 0 Then '                  <---- If it's a container...
                Return GetControlByName(ctlName, c) '       <--- make the recursive call
            End If
        Next
    End Function
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 18004692
So, in summary davoman,

If your using:

vs2005 - use me.Controls.Find()
vs2003 - you'll have to loop through all controls on the parent. (Which isn't a bad thing :))
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 150 total points
ID: 18004955
There is one other possibility...

If all the controls are present  on the form at design-time then you can also use Reflection to get references to them.

Something like...

    Dim c As Control
    Dim i As Integer
    For i = 1 to 24
        c = GetControlByName("uxbay" & i, Me)
        If Not IsNothing(c) Then
            c.Text  = “xxxx”
        End If
    Next i

Using the function:

    Protected Overridable Function FindControl(ByVal Name As String) As Control
        Dim propInfo As System.Reflection.PropertyInfo
        propInfo = Me.GetType().GetProperty(Name, System.Reflection.BindingFlags.IgnoreCase Or System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Public)
        If Not propInfo Is Nothing Then
            Dim value As Object = propInfo.GetValue(Me, Nothing)
            If TypeOf value Is Control Then
                Return CType(value, Control)
            End If
        End If
        Return Nothing
    End Function
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18004960
And finally, you could loop thru the controls (using the recursive technique shown above) in the Form Load() event and add them all to a HashTable using their name as the Key so you can do a fast lookup.
0
 
LVL 1

Author Comment

by:davoman
ID: 18004977
wow good stuff

I have VS 2005 and will try them out tommorrow when i am back at work

report back soon, cheers
0
 
LVL 1

Author Comment

by:davoman
ID: 18100238
that me.controls find is excellent

thanks for all your advice guys sorry i did not finalise this ealier
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

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