-
Jun 29th, 2017, 12:32 PM
#1
Thread Starter
New Member
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.
-
Jun 29th, 2017, 01:51 PM
#2
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
-
Jun 29th, 2017, 01:59 PM
#3
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:
Option Strict On
Option Explicit On
Public Class Form1
Private mActiveTextBox As TextBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
mActiveTextBox = TextBox1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
mActiveTextBox.Text &= "1"
End Sub
Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
mActiveTextBox = TextBox1
End Sub
Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus
mActiveTextBox = TextBox2
End Sub
End Class
-
Jun 29th, 2017, 02:10 PM
#4
Thread Starter
New Member
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
-
Jun 29th, 2017, 02:22 PM
#5
Thread Starter
New Member
Re: Do not work Focus changing
Originally Posted by 4x2y
Declare a module level variable mActiveTextBox, when a TextBox got focus set mActiveTextBox to that TextBox, something like this
vb Code:
Option Strict On Option Explicit On Public Class Form1 Private mActiveTextBox As TextBox Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load mActiveTextBox = TextBox1 End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click mActiveTextBox.Text &= "1" End Sub Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus mActiveTextBox = TextBox1 End Sub Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus mActiveTextBox = TextBox2 End Sub 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.
-
Jun 30th, 2017, 02:16 AM
#6
Re: Do not work Focus changing
Originally Posted by 4x2y
Declare a module level variable mActiveTextBox, when a TextBox got focus set mActiveTextBox to that TextBox, something like this
vb Code:
Option Strict On Option Explicit On Public Class Form1 Private mActiveTextBox As TextBox Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load mActiveTextBox = TextBox1 End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click mActiveTextBox.Text &= "1" End Sub Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus mActiveTextBox = TextBox1 End Sub Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus mActiveTextBox = TextBox2 End Sub 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.
-
Jun 30th, 2017, 10:32 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|