Why Isn't My Mouse Click Code Working

G

Guest

I have the following code and it returns the x and y coordinates in the text
box, but when I click the mouse on say 362,61 does not bring up form2.Any
suggestions?
Thanks,
Jerry

Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
Dim mouseX As Integer = e.X
Dim mouseY As Integer = e.Y
TextBox1.Text = e.X
TextBox2.Text = e.Y
If TextBox1.Text = 341 > 553 And TextBox2.Text = 22 > 148 Then
Form2.Show()
Me.Hide()


End If
End Sub
 
N

Norman Yuan

What are you trying to do with this line of code:

If TextBox1.Text = 341 > 553 And TextBox2.Text = 22 > 148 Then
...............
End If

IMO, a code like that should not get complied, but, well, it is VB and it
simply does too much on user's behalf sometimes, so that user was fooled on
what is wrong. In this case, if VB does not do the implicit type conversion,
then you cannot compare TextBox.Text to a non-string value, so that you know
what is wrong.

Since the code seems compiled, then VB actually does is, first compares
341>553, it gets "False", then VB compares TextBox1.Text to False, the
result is False; the same for the next part. The overall result for the
"If..." statement would be False, so, you would never get Form2.Show() to
run.

VB's such behaviour IMO, is doing too much on user's behalf, that is one
reason one should give up VB.NET for C#.
 
G

Guest

What my ultimate goal is to have my mouse click on a part of the picture box
top left location 341,22/bottom left location 341,48/top right location
553,22/bottom right location 553,148 and to have the program detect if the
mouse click was anywhere in the square, if so load form2
Thanks,
Jerry
 
N

Norman Yuan

So, the code wuold look like
Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick

If e.X >= 341 AND e.X <= 553 And e.Y >= 22 AND e.Y <= 148 Then

TextBox1.Text=e.X.ToString()
TextBox2.Text=e.Y.ToString()

Form2.Show()
Me.Hide()

End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top