Solved

Trying to be dynamic with controls

Posted on 2006-11-23
15
172 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
SharePoint Admin?

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

 
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

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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