dcsimg
Results 1 to 12 of 12

Thread: Problem controlling mouse event at the edge of a text box when arrow cursor appears

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2017
    Location
    Port St Lucie, Florida, United States
    Posts
    51

    Problem controlling mouse event at the edge of a text box when arrow cursor appears

    I have a text box in a picturebox in a window's form. The user creates the text box and can move it around and resize it. The user can draw lines outside and around the text box (by clicking and dragging). I have a problem that occurs when the user decides to draw a line but points the cursor, which for drawing purposes is a cross, at the very edge of the text box, where the cursor suddenly changes into an arrow cursor. If the user now presses down to start drawing at that moment, no start point for the line is made, and the arrow fails to draw properly. What happens is that the start point of the arrow becomes the default (0, 0) location point of the client area and form. So the user gets a line that starts at that point (after dragging off of the text box edge to draw the line). I was wondering if there was a way to get around this problem by somehow controlling what happens when the user points the cursor at the edge of the text box, gets that arrow cursor, and then does a mouse down to start dragging to make the line. It seems that the edge of the text box is a no-man's land that's beyond control. I can't find any way to control it, and a mouse down event on the very edge of a text box doesn't seem to produce anything. Perhaps I need to disable this behavior at the edge of the text box, I don't know. But I don't want to disable the whole text box. That's not an option. Thanks.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,026

    Re: Problem controlling mouse event at the edge of a text box when arrow cursor appea

    Quote Originally Posted by dwigley View Post
    Perhaps I need to disable this behavior at the edge of the text box
    That makes the most sense to me. If the mouse cursor is not a cross then that indicates that the user cannot draw so the user should not be able to draw.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2017
    Location
    Port St Lucie, Florida, United States
    Posts
    51

    Re: Problem controlling mouse event at the edge of a text box when arrow cursor appea

    How do you disable just the edge of the text box? I can't find how to do that. Thanks.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,026

    Re: Problem controlling mouse event at the edge of a text box when arrow cursor appea

    You don't have explicitly disable anything. What's the obvious difference? The mouse cursor. Check whether the mouse cursor is a cross and only start drawing if it is.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2017
    Location
    Port St Lucie, Florida, United States
    Posts
    51

    Re: Problem controlling mouse event at the edge of a text box when arrow cursor appea

    Yes, as I tried to explain, when the user drags off the text box, the cursor becomes a cross, and a line is then drawn from the cursor to the point (0,0). For some reason, the start point becomes (0,0). I suppose that’s the default. In fact, it may jump to other points on the form if they are there from other actions. What I have done to solve the issue is to remove stray points that might get created. For instance, I set any point going to (0,0) to nothing, and now the crazy line doesn’t get drawn.

    I was wondering if it were possible to change the behavior occurring at the edge of the text box that would allow a different solution. If not, then I’ll do it this other way. Thanks for your time.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,026

    Re: Problem controlling mouse event at the edge of a text box when arrow cursor appea

    I'm assuming that you start drawing on a MouseDown event. If the cursor is not a cross on that event then don't start drawing. If you don't start drawing then you won't draw at all. Problem solved. If that still doesn't make sense to you then it's time to show us the relevant code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,620

    Re: Problem controlling mouse event at the edge of a text box when arrow cursor appea

    p.s. I overlapped responding while jmcilhinney was responding, so repetitive most likely....

    Did you write the code?
    Do you enable drawing in the Mouse Down event, or just draw anytime the mouse is down in the Mouse Move event?
    I believe that jmcilhinney's assumes you would enable drawing (i.e. set a Boolean) in the Mouse Down event, and if the cursor wasn't a cross at the time of the Mouse Down event you wouldn't enable the drawing.
    My assumption would be that you didn't actually get the Mouse Down event in the picturebox (it may have occurred for the textbox), so if you added code to to set a Boolean in the picturebox's Mouse Down, then it wouldn't get set so you wouldn't be drawing in the Mouse Move event.

    It seems like you might initialize your drawing coordinates in the Mouse Down event of the picturebox, and since you're not getting that event your starting coordinates end up at 0,0 when you get to the Mouse Move event. Many drawing code do follow the paradigm of setting a Boolean to Enable drawing in the Mouse Down event, and clear that Boolean in the Mouse Up event and don't rely only on the condition of the mouse button in the Move event to draw.

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2017
    Location
    Port St Lucie, Florida, United States
    Posts
    51

    Re: Problem controlling mouse event at the edge of a text box when arrow cursor appea

    I wrote the code. That's the first thing I tried to do -- shut down drawing if the cursor isn't a cross. But I've tried everything that I could think of to shut it down when the cursor is either an arrow or not cross. Yes, I use a Boolean variable to turn on drawing.

    When that cursor becomes an arrow at the very edge of the text box and there is a mouse down, I'm not getting that mouse down event, everything continues, but now the mouse is down, and the start point becomes (0,0).

    Further when the cursor becomes an arrow at the edge of the text box, I can't get that change either. I've tried the following everywhere to get it:

    Code:
     Form1.Cursor = Cursors.Arrow
    Form1.PictureBox1.Cursor = Cursors.Arrow
    Form1.TextBox1 = Cursors.Arrow
    
    'and
    
    Form1.Cursor <> Cursors.Cursor
    Form1.PictureBox1.Cursor <> Cursors.Cursor
    Form1.TextBox1 <> Cursors.Cursor
    That's why I said that the edge of the text box seems to be a kind of no-man's land.

    So I must be missing something somehow. The next thing I can do is make a mini version of the application, try it out, and show it here if it does the same thing, because the code for the application is too complex to show here and try to explain things.

    That will take a little time.

    Thanks.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,026

    Re: Problem controlling mouse event at the edge of a text box when arrow cursor appea

    Show us the drawing code you have and then we can determine how to change that to work the way you want.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,620

    Re: Problem controlling mouse event at the edge of a text box when arrow cursor appea

    Quote Originally Posted by dwigley View Post
    ...Yes, I use a Boolean variable to turn on drawing...When that cursor becomes an arrow at the very edge of the text box and there is a mouse down, I'm not getting that mouse down event, everything continues, but now the mouse is down, and the start point becomes (0,0)....
    If you're not getting "that mouse down event", then how is the Boolean variable being set to turn on the drawing? Without a Mouse Down event to set the boolean, you shouldn't be doing any drawing, whether the button is down or not.

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,620

    Re: Problem controlling mouse event at the edge of a text box when arrow cursor appea

    I just did a quick test, and when I click in the "No man's land", area, since neither mousedown event is triggered, I don't get a debug.print out and the boolean value is not set, and I don't draw in the MouseDown event, so the code works as expected.
    Given your code allows dragging and specialized drawing, I'm sure your code is more involved than this simple test, but your decision of whether you draw or not should be right along the lines shown here.
    Code:
    Public Class Form1
      Private DrawEnabled As Boolean
      Private StartPoint As Point
      Private EndPoint As Point
    
      Private Sub TextBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
        Debug.Print("textbox mousedown")
      End Sub
    
      Private Sub PictureBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Debug.Print("picturebox mousedown")
        DrawEnabled = True
        StartPoint = e.Location
      End Sub
    
      Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If DrawEnabled Then
          If e.Button = Windows.Forms.MouseButtons.Left Then
            EndPoint = e.Location
            PictureBox1.Invalidate()
          End If
        End If
      End Sub
    
      Private Sub PictureBox1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        DrawEnabled = False
      End Sub
    
      Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        e.Graphics.DrawLine(Pens.Black, StartPoint, EndPoint)
      End Sub
    End Class

  12. #12

    Thread Starter
    Member
    Join Date
    Sep 2017
    Location
    Port St Lucie, Florida, United States
    Posts
    51

    Re: Problem controlling mouse event at the edge of a text box when arrow cursor appea

    Ok, I fixed it. My mouse down event wasn't determining whether drawing takes place or not. I thought it was, but I had undone it. Right, it goes along the lines you have there, passel.

    In my defense, there's that no-man's land on the edge of a text box where the arrow comes up! But if I couldn't get an event there, then why would there be drawing?! Right. So my mouse down event wasn't determining whether drawing takes place.

    Once again, you guys know immediately what's wrong! Amazing, thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width