|
-
January 16th, 2008, 04:46 AM
#1
SetFocus a Control...
hello,
I have a textbox (txt1) and a command button (cmd1) and a number of user controls (usercontrol1 - usercontrol2 ...)
I want to be able to SetFocus the user control by clicking on the command button..
however, I want to SetFocus the usercontrol which is in the textbox..
eg. The Textbox might say '4' so I want to SetFocus usercontrol4
anyone know how to do this?
Thanks
-
January 16th, 2008, 05:40 AM
#2
Re: SetFocus a Control...
you can create an array of usercontrols. instead of pasting one usercontrol at a time into a form try to copy the usercontrol that's already pasted in the form, by repeating that procedure you can create an array of usercontrols. in that way, you can reference the control by index which is easy.
Code:
UserControls(0) ' referenced user control 1
UserControls(1) ' referenced user control 2
etc..
however, if you *really* want to reference a control by name, like typing the name in a textbox, check the function CallByName.. here is a sample code anyway..
Code:
Dim o As Usercontrol1
Set o = CallByName(Me, "usercontrol" & txt1.text, VbGet)
o.SetFocus
Set o = Nothing
hope that helps
Busy 
-
January 16th, 2008, 03:50 PM
#3
Re: SetFocus a Control...
In any case you can use a Select Case statement.
Code:
Select Case txt1.Text
Case "1"
UserControl1.SetFocus
Case "2"
UserControl2.SetFocus
End Select
Obviously not the most elegant way to do it, but you can make a Case for every possible occurance.
-
January 16th, 2008, 07:42 PM
#4
Re: SetFocus a Control...
You can always just loop through the controls and test for a name match too:
Code:
Dim oCont As Control
For Each oCont In Me.Controls
If LCase$(oCont.Name) = Trim$(LCase$(txt1.Text)) Then
oCont.SetFocus
End If
Next oCont
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|