Solved

Trying to be dynamic with controls

Posted on 2006-11-23
15
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 86

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 86

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 86

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

729 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