Results 1 to 5 of 5

Thread: Could someone help me correct my code trying to resize a panel?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Massachusetts
    Posts
    205

    Question Could someone help me correct my code trying to resize a panel?

    I need for a user to be able to resize a panel using at least one corner at runtime. I did this by making a panel and placing a small picturebox in the bottom left corner. I'm only testing it, so I just gave both the picturebox and panel black borders so I could test my code. Here's what I came up with:

    Code:
    Public Class Form1
        Dim isresize As Boolean = False
        Dim currentx As Integer
        Dim currenty As Integer
        Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            isresize = True
            currentx = e.X
            currenty = e.Y
        End Sub
    
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If isresize = True Then
                If e.X < currentx Then
                    Panel1.Width = Panel1.Width - (currentx - e.X)
                    currentx = e.X
                End If
    
                If e.X > currentx Then
                    Panel1.Width = Panel1.Width + (e.X - currentx)
                    currentx = e.X
                End If
    
                If e.Y < currenty Then
                    Panel1.Height = Panel1.Height - (currenty - e.Y)
                    currenty = e.Y
    
                End If
    
                If e.Y > currenty Then
                    Panel1.Height = Panel1.Height + (e.Y - currenty)
                    currenty = e.Y
                End If
            End If
        End Sub
    
        Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
            isresize = False
        End Sub
    End Class
    Yet when I try it, my panel flickers and the picturebox doesn't keep up with my cursor. Is there any easy way to revise my code? Thanks.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Could someone help me correct my code trying to resize a panel?

    have a look at the movable/resizable controls link in my signature

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Could someone help me correct my code trying to resize a panel?

    Panels are going to cause you trouble, but trouble that you can readily fix. You will need to subclass it to get at the DoubleBuffered property and set it to true. Here's code that I use. Put this on the form, then go into the .designer.vb file for that form and find the references to the Panel and change them to FlickerPanel.

    Code:
     Private Class FlickerPanel
            Inherits Windows.Forms.Panel
    
            Public Sub New()
                Me.DoubleBuffered = True
            End Sub
    
        End Class
    I was having the same flickering problem, and this was the solution. A pretty simple class, though tinkering with the .designer.vb file may be something new for you.

    After that comes the bit about the picturebox not keeping up with the cursor. There could be two problems here based on that description, but I think what you are describing is a problem with your coordinate system. All those events that have a location, such as the mouse events, give you that location in some coordinate system. I keep forgetting which coordinate system you get, but I suspect that e.x and e.y are relative to the picturebox, and you are using them in the panel. Put a breakpoint in one of those event handlers and take a look at the x and y. By putting the mouse near one corner, it should be readily apparent whether you are getting picturebox coordinates, panel coordinates, screen coordinates, or form coordinates (though a couple of those alternatives are absurd). Most likely, you will have to convert the coordinates to the proper system before you use them in your calculations. That could be awkward, as you might have to use the PictureBox.PointToScreen conversion to convert the coordinates from picturebox to screen coordinates, then the panel.PointToClient to convert the coordinates from screen to panel client coordinates.
    My usual boring signature: Nothing

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Could someone help me correct my code trying to resize a panel?

    Quote Originally Posted by Shaggy Hiker View Post
    Panels are going to cause you trouble, but trouble that you can readily fix. You will need to subclass it to get at the DoubleBuffered property and set it to true. Here's code that I use. Put this on the form, then go into the .designer.vb file for that form and find the references to the Panel and change them to FlickerPanel.
    @RadXPictures: I agree with SH, but I would call it a NonFlickerPanel. An alternative to editing the designer.vb file is:
    1. Delete the panel.
    2. Build the solution. The new [Non]FlickerPanel tool appears in the Toolbox with a cogwheel icon.
    3. Drag it onto the form the same way as a Panel or any other tool.

    After that comes the bit about the picturebox not keeping up with the cursor. There could be two problems here based on that description, but I think what you are describing is a problem with your coordinate system. All those events that have a location, such as the mouse events, give you that location in some coordinate system. I keep forgetting which coordinate system you get, but I suspect that e.x and e.y are relative to the picturebox, and you are using them in the panel. Put a breakpoint in one of those event handlers and take a look at the x and y. By putting the mouse near one corner, it should be readily apparent whether you are getting picturebox coordinates, panel coordinates, screen coordinates, or form coordinates (though a couple of those alternatives are absurd). Most likely, you will have to convert the coordinates to the proper system before you use them in your calculations. That could be awkward, as you might have to use the PictureBox.PointToScreen conversion to convert the coordinates from picturebox to screen coordinates, then the panel.PointToClient to convert the coordinates from screen to panel client coordinates.
    It can be easier than that. Just use MousePosition.X and MousePosition.Y everywhere instead of e.X and e.Y. That applies both to the MouseDown sub and the MouseMove sub. Explanation: MousePosition is relative to the screen, so it doesn't depend on which control fires the MouseMove event. Also, add some extra handlers to the handles clause of the MouseMove and MouseUp subs (but not MouseDown):
    Code:
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles PictureBox1.MouseMove, Panel1.MouseMove, Me.MouseMove
    That will make sure the MouseMove event still fires when the cursor slips out of picture box.

    BB

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Could someone help me correct my code trying to resize a panel?

    Quote Originally Posted by boops boops View Post
    @RadXPictures: I agree with SH, but I would call it a NonFlickerPanel.
    That's a good point. My name is 180 degrees wrong.

    Frankly, I prefer the designer.vb means of changing over the panel, but only because it is quicker if you know what you are doing, and well worth learning if you don't.
    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