Solved

Trying to be dynamic with controls

Posted on 2006-11-23
15
166 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

21 Experts available now in Live!

Get 1:1 Help Now