Results 1 to 7 of 7

Thread: Do not work Focus changing

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Location
    Ukraine
    Posts
    8

    Do not work Focus changing

    Please answer me such question. I have two textboxes on a form and ten buttons with digits (as a simple calculator buttons). When i click on any digits it must appears in one textbox. When I set focus on another textbox and click the same button this digit must appears in the next text box. I have such code:

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    If TextBox1.Focus = True Then
    TextBox1.Text &= "1"
    TextBox2.Select()
    Else
    ...
    end sub
    But when the program is working digit is appearing just in the first textbox or in both textboxes at the same time.
    Thanks for Your help if you can.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Do not work Focus changing

    When you click on a button, which control will then have focus?

    Put a breakpoint on the first line inside the If statement, and see if it is EVER reached. I think you will find that it is not, which is the point behind my question. Showing what is in the Else part of that sub might further explain the behavior.

    I believe that you're going to have to use something other than Focus to indicate which textbox should be written to. Focus can shift around to other controls, or even no control, so having a variable at form scope to indicate which control should be the target will ultimately be a more reliable solution.
    My usual boring signature: Nothing

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Do not work Focus changing

    Declare a module level variable mActiveTextBox, when a TextBox got focus set mActiveTextBox to that TextBox, something like this
    vb Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Public Class Form1
    5.     Private mActiveTextBox As TextBox
    6.  
    7.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    8.         mActiveTextBox = TextBox1
    9.     End Sub
    10.  
    11.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
    12.         mActiveTextBox.Text &= "1"
    13.     End Sub
    14.  
    15.     Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
    16.         mActiveTextBox = TextBox1
    17.     End Sub
    18.  
    19.     Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus
    20.         mActiveTextBox = TextBox2
    21.     End Sub
    22.  
    23. End Class



  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Location
    Ukraine
    Posts
    8

    Re: Do not work Focus changing

    Thank you.
    Well, my question wasn't so correct.
    I need the next: when cursor is in the textbox1 and I click the button the digit is appearing in the textbox1, and when cursor is in the textbox2 and I click the button the digit is appearing in the textbox2. But now I have a such situation: I press the button and digits appears in both textboxes (or triple, when I add textbox3). The code is like this:
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    If TextBox1.Focus Then
    TextBox1.Text &= "1"
    End If
    If TextBox2.Focus = True Then
    TextBox2.Text &= "1"
    End If
    End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Location
    Ukraine
    Posts
    8

    Re: Do not work Focus changing

    Quote Originally Posted by 4x2y View Post
    Declare a module level variable mActiveTextBox, when a TextBox got focus set mActiveTextBox to that TextBox, something like this
    vb Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Public Class Form1
    5.     Private mActiveTextBox As TextBox
    6.  
    7.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    8.         mActiveTextBox = TextBox1
    9.     End Sub
    10.  
    11.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
    12.         mActiveTextBox.Text &= "1"
    13.     End Sub
    14.  
    15.     Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
    16.         mActiveTextBox = TextBox1
    17.     End Sub
    18.  
    19.     Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus
    20.         mActiveTextBox = TextBox2
    21.     End Sub
    22.  
    23. End Class
    Very-very thanks a lot Your method is working absolutely correctly. You're an Angel for me tonight. My problem is solved. Thank you again.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Do not work Focus changing

    Quote Originally Posted by 4x2y View Post
    Declare a module level variable mActiveTextBox, when a TextBox got focus set mActiveTextBox to that TextBox, something like this
    vb Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Public Class Form1
    5.     Private mActiveTextBox As TextBox
    6.  
    7.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    8.         mActiveTextBox = TextBox1
    9.     End Sub
    10.  
    11.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
    12.         mActiveTextBox.Text &= "1"
    13.     End Sub
    14.  
    15.     Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
    16.         mActiveTextBox = TextBox1
    17.     End Sub
    18.  
    19.     Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus
    20.         mActiveTextBox = TextBox2
    21.     End Sub
    22.  
    23. End Class
    As the documentation states, application developers should use the Enter and Leave events rather than the GotFocus and LostFocus events. Likewise, use the Select method rather than the Focus method.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Do not work Focus changing

    I've never trusted the LostFocus event. Back in VB5/6, I found that it wasn't reliably raised. For that reason, I stopped using it and have never used it since. I'm not surprised that MS discourages use of those events.
    My usual boring signature: Nothing

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