Solved

Trying to be dynamic with controls

Posted on 2006-11-23
15
176 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

632 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