Results 1 to 11 of 11

Thread: Drawing an rectangle around the panel

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Drawing an rectangle around the panel

    I have used this code to draw an rectangle around the panel

    vb Code:
    1. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    2. With Panel1
    3.             e.Graphics.DrawRectangle(Pens.Gray, .Left - 1, .Top - 1, .Width + 1, .Height + 1)
    4.         End With
    5. End Sub


    but when the form is resized the rectangle isnt drawn properly. The panel has Left,R,T,B anchor.

  2. #2

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Drawing an rectangle around the panel

    Can you not just set the BorderStyle of the Panel?
    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

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: Drawing an rectangle around the panel

    Quote Originally Posted by stateofidleness View Post
    try removing the anchor.. seems like that would conflict with the code positioning.
    I cant remove the anchor as i want to panel to resize along with the form.

    Can you not just set the BorderStyle of the Panel?
    No i cant. Actually i want the border to be in Gray color and 1 pixel away from the panel

  5. #5
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Drawing an rectangle around the panel

    vb Code:
    1. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    2.    With Panel1
    3.        e.Graphics.DrawRectangle(Pens.Gray, .Left - 1, .Top - 1, .Width + 1, .Height + 1)
    4.    End With
    5. End Sub
    6.  
    7. Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    8.     Me.Refresh()
    9. End Sub

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Drawing an rectangle around the panel

    The problem is that resizing doesn't cause the entire form to be invalidated, so the entire form isn't redrawn. Call the form's Invalidate method in its Resize event handler to change that behaviour. You may have some issue with flickering, depending on how complex your form is. In that case set DoubleBuffered to True. If there are still issues then you'll have to look at other options.
    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
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Drawing an rectangle around the panel

    thought about that jmc, but i just tested the Refresh call on a whim and it worked great with no flickering issues even! Should do what he wants.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Drawing an rectangle around the panel

    Quote Originally Posted by stateofidleness View Post
    thought about that jmc, but i just tested the Refresh call on a whim and it worked great with no flickering issues even! Should do what he wants.
    Refresh internally calls Invalidate followed by Update. Invalidate specifies what area needs to be redrawn on the next repaint. It can be called multiple times to specify multiple areas. Update actually does the repaint. I'm not sure but calling Refresh may end up causing twice as many repaints as needed.
    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

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: Drawing an rectangle around the panel

    vb Code:
    1. Private Sub From1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    2.         Dim rec As New Rectangle(Panel2.Left - 1, Panel2.Top - 1, Panel2.Width + 1, Panel2.Height + 1)
    3.         Dim r As New Region(rec)
    4.         Me.DoubleBuffered = True
    5.         Me.Invalidate(rec)
    6.     End Sub

    I have used this code as suggested. Not much sure if its correct as i have used the invalidate method first time.

    The design of my form is very simple, not many controls in it. The flickering occurs even though the DoubleBuffered is set true.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Drawing an rectangle around the panel

    Think about it. Would you set DoubleBuffered to True every time there's a Resize event? Do you ever set it to False? If not, why do you need to keep setting it to True over and over? It's no different to any other form property. Set it in the designer and be done with it.

    I tested with just a Panel and nothing else on the form and DoubleBuffered left False, plus I called Invalidate without arguments. I saw no flickering. What happens when you try that?
    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

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: Drawing an rectangle around the panel

    ok Done with DoubleBuffer.

    Actually inside my panel
    1. I am drawing a line
    2. Contains another panel in which i am drawing 2 rectangles an 1 line.

    These both are flickering

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