We'll continue here the program started in example 14.
Currently, when we turn the first checkbox on and press the "capture" button, we can't click on the "cancel" properly since the timer is constantly re-focusing back on the "capture" button. We should be able to click on the "cancel" button normally.
Let's create a whole new sub for button2. Below all subs in the coding page, add this:
Private Sub Button2_Mouse(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseMove, Button2.MouseLeave
End Sub
-> the name of the sub is gonna be: Button2_Mouse. The arguments it holds (the variables) can be copied from other subs. At the very end we add two events: MouseMove and MouseLeave
Inside the sub we will add this:
This sub registers two events MouseMove - sub gets triggered when you cause mouse movement over the object, while MouseLeave - triggers the code of the sub when you leave the object with your mouse cursor.
After we remove the MouseMove event we can either proceed by moving away from the "cancel" button or by clicking on it. Both of those choices must return the MouseMove event back. That's why we have to go back to the "Button2_Click" sub and this at the bottom:
AddHandler Button2.MouseMove, AddressOf Button2_Mouse
The timer will be still fighting against our choice to transfer the focus on the "cancel" button at the moment. We need to go to the timer sub and change this line:
If Button2.Visible = True Then
into:
If Button2.Visible = True And Button2.Focused = False Then
-> so we're saying to the timer: only focus back on the "capture" button IF the "cancel" button is not focused.
Code:
Private x1 As Integer
Private gg As String()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button2.Visible = True Then
x1 = x1 + 1
Label1.Text = ""
Button2.Visible = False
Label1.Focus()
Exit Sub
End If
If CheckBox1.Checked Or CheckBox2.Checked Or CheckBox3.Checked Then
Label1.Text = "! Scroll with your mouse and then press Space !"
Button2.Visible = True
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If
If CheckBox1.Checked = False Then
gg(x1) = ""
TextBox1.Lines = gg
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If CheckBox1.Checked Then
ReDim Preserve gg(0 To x1)
gg(x1) = Cursor.Position.ToString
TextBox1.Lines = gg
End If
If Button2.Visible = True And Button2.Focused = False Then
Button1.Focus()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button2.Visible = False
Label1.Text = ""
AddHandler Button2.MouseMove, AddressOf Button2_Mouse
End Sub
Private Sub Button2_Mouse(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseMove, Button2.MouseLeave
If Button2.Focused = True Then
Button1.Focus()
Label1.Text = "! Scroll with your mouse and then press Space !"
AddHandler Button2.MouseMove, AddressOf Button2_Mouse
Exit Sub
End If
If Button2.Visible = True Then
Button2.Focus()
Label1.Text = "Cancel button selected"
RemoveHandler Button2.MouseMove, AddressOf Button2_Mouse
End If
End Sub
End Class
The program should work for the first checkbox. Adding the other two ones is quite simple. Each checkbox will have it's two variables just like the first option.
The most important part to notice is how we get the values for the other coordinates.
Inside the Timer1 sub we will have this line for checkbox2 - Form + Borders.
Bounds:Location - contains both x and y values. This is the location of the top-left pixel of the main field form on the whole computer screen. The value of "Bounds.Location" changes only when we drag the main form around our computer screen. Try to get the second textbox on (0,0) and then check the coordinates inside the first textbox -> those are gonna be the location values of the form bounds. Those are gonna be reduced from the whole screen coordinates in every moment to give you the results in checkbox2.
Third option (inside timer):
nn(x3) = PointToClient(Cursor.Position).ToString
-> this gives you coordinates relative to the main field form while neglecting the borders of the form.
Final code:
Private x1 As Integer
Private x2 As Integer
Private x3 As Integer
Private gg As String()
Private kk As String()
Private nn As String()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button2.Visible = True Then
If CheckBox1.Checked Then
x1 = x1 + 1
End If
If CheckBox2.Checked Then
x2 = x2 + 1
End If
If CheckBox3.Checked Then
x3 = x3 + 1
End If
Label1.Text = ""
Button2.Visible = False
Label1.Focus()
Exit Sub
End If
If CheckBox1.Checked Or CheckBox2.Checked Or CheckBox3.Checked Then
Label1.Text = "! Scroll with your mouse and then press Space !"
Button2.Visible = True
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = False Then
gg(x1) = ""
TextBox1.Lines = gg
End If
enabl()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If CheckBox1.Checked Then
ReDim Preserve gg(0 To x1)
gg(x1) = Cursor.Position.ToString
TextBox1.Lines = gg
End If
If CheckBox2.Checked Then
ReDim Preserve kk(0 To x2)
kk(x2) = ((Cursor.Position - Bounds.Location).ToString)
TextBox2.Lines = kk
End If
If CheckBox3.Checked Then
ReDim Preserve nn(0 To x3)
nn(x3) = PointToClient(Cursor.Position).ToString
TextBox3.Lines = nn
End If
If Button2.Visible = True And Button2.Focused = False Then
Button1.Focus()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button2.Visible = False
Label1.Text = ""
AddHandler Button2.MouseMove, AddressOf Button2_Mouse
End Sub
Private Sub Button2_Mouse(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseMove, Button2.MouseLeave
If Button2.Focused = True Then
Button1.Focus()
Label1.Text = "! Scroll with your mouse and then press Space !"
AddHandler Button2.MouseMove, AddressOf Button2_Mouse
Exit Sub
End If
If Button2.Visible = True Then
Button2.Focus()
Label1.Text = "Cancel button selected"
RemoveHandler Button2.MouseMove, AddressOf Button2_Mouse
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked = False Then
kk(x2) = ""
TextBox2.Lines = kk
End If
enabl()
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
If CheckBox3.Checked = False Then
nn(x3) = ""
TextBox3.Lines = nn
End If
enabl()
End Sub
Sub enabl()
If CheckBox1.Checked = False And CheckBox2.Checked = False And CheckBox3.Checked = False Then
Timer1.Enabled = False
Else
Timer1.Enabled = True
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Clear()
x1 = 0
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox2.Clear()
x2 = 0
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
TextBox3.Clear()
x3 = 0
End Sub
End Class
enabl() - this is a sub used to check if all the checkboxes are turned off, when they are (only then!) the timer is disabled. If any of the checkboxes is turned on, the timer gets turned on too.
- all three of the checkbox subs have an enabl() call
Buttons 3, 4, 5 are the "Clear" buttons below the textboxes.
If you want a quicker check of the timer, go to design page -> Timer1 -> Properties -> change interval to 10.
I hope everything makes sense. Program should work.
There probably might be ways to somewhat shorten the code too.