Hi Guys,

just wondered if anyone knows how to change the focus from 1 textbox to another?

i have 2 textboxes and a few buttons, what i am trying to do is select a textbox (either) then hit the button and the value from the button (i.e 1) will go into the selected/focused textbox.
i can get this to go to the first textbox, but cant figure out how to change the focus, i have trawled the internet but cant find exactlly what im looking for, my code has changed nurmous times, so il just paste what i have got now,
thanks guys

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Focus()

        '''TextBox1.Focus()
        '''If TextBox1.Focused Then
        '''    TextBox1.Text = num1
        '''ElseIf TextBox2.Focused Then
        '''    TextBox2.Text = num1
        '''End If

        If TextBox1.Focused Then
            TextBox1.Text = TextBox1.Text & num1

        End If

        If TextBox2.Focused Then
            TextBox2.Text = TextBox2.Text & num1
        End If


    End Sub

As soon as you click the button whatever control had focus loses it. What you can do is define a class level variable

Private LastTextFocused As TextBox

Then in the Enter event for each TextBox you can add the line

LastTextFocused = sender

Then in the button click you can set the text for that textbox by

If Not IsNothing(LastTextFocused) Then
    LastTextFocused.Text = "some text"

hi mate, thanks for the fast reply, not really sure i follow the 1st bit, i understand the logic with the rest.
sorry i should of said im a newbie :)

Aah, the old who has focus when I get clicked dilemma.

If there is no reason that the button needs to be focused, then you can create a nonselectable button or any other control for that matter. The only drawback of this technique is that the button can not be access via the keyboard (Tab, Enter).

  Public Class NoFocusButton
     Inherits System.Windows.Forms.Button
     Public Sub New()
        SetStyle(ControlStyles.Selectable, False)
     End Sub
  End Class

Just add this to your project and perform a build operation. It should show up at the top of your toolbox.

This is a useful alternative to a toolstrip button.

commented: Cleaner and easier. Thanks. +12

When you click on a button then the button gets the focus. At that point you don't know what textbox last had focus so you have to keep track of that yourself. When a textbox gets focus, it fires the Enter event. What you can do is create a class level variable and in the Enter event of each textbox (or at least the ones you are interested in) you can assign a reference to that textbox to the class level variable. Here's an example

Public Class Form1

    Private LastFocus As TextBox

    Private Sub TextBox_Enter(sender As Object, e As System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter, TextBox3.Enter
        LastFocus = sender
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        If Not IsNothing(LastFocus) Then
            LastFocus.Text = TimeOfDay
        End If

    End Sub

End Class

Note that I am using one Enter handler for all three textboxes. Click in any textbox, then click on Button1. The last selected TextBox will be updated.

guys!!
thats awesome, cheers, code is working (in my code i mean) once i redo my code (as i do a muck around 1 first before final project) i will post to show you :)
thanks again :)
i'm not sure why but i can seem to get the hang of C# but no VB ~ strange

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.