CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2006
    Posts
    228

    Question 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

  2. #2
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    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

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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.

  4. #4
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    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
  •  





Click Here to Expand Forum to Full Width

Featured