|
-
Apr 24th, 2010, 11:38 PM
#1
Thread Starter
Frenzied Member
Drawing an rectangle around the panel
I have used this code to draw an rectangle around the panel
vb Code:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint With Panel1 e.Graphics.DrawRectangle(Pens.Gray, .Left - 1, .Top - 1, .Width + 1, .Height + 1) End With End Sub
but when the form is resized the rectangle isnt drawn properly. The panel has Left,R,T,B anchor.
Last edited by aashish_9601; Apr 25th, 2010 at 01:47 AM.
-
Apr 25th, 2010, 12:14 AM
#2
Re: Drawing an rectangle around the panel
try removing the anchor.. seems like that would conflict with the code positioning.
-
Apr 25th, 2010, 01:00 AM
#3
Re: Drawing an rectangle around the panel
Can you not just set the BorderStyle of the Panel?
-
Apr 25th, 2010, 01:49 AM
#4
Thread Starter
Frenzied Member
Re: Drawing an rectangle around the panel
 Originally Posted by stateofidleness
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
-
Apr 25th, 2010, 02:08 AM
#5
Re: Drawing an rectangle around the panel
vb Code:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint With Panel1 e.Graphics.DrawRectangle(Pens.Gray, .Left - 1, .Top - 1, .Width + 1, .Height + 1) End With End Sub Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize Me.Refresh() End Sub
-
Apr 25th, 2010, 02:10 AM
#6
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.
-
Apr 25th, 2010, 02:15 AM
#7
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.
-
Apr 25th, 2010, 02:18 AM
#8
Re: Drawing an rectangle around the panel
 Originally Posted by stateofidleness
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.
-
Apr 25th, 2010, 02:38 AM
#9
Thread Starter
Frenzied Member
Re: Drawing an rectangle around the panel
vb Code:
Private Sub From1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize Dim rec As New Rectangle(Panel2.Left - 1, Panel2.Top - 1, Panel2.Width + 1, Panel2.Height + 1) Dim r As New Region(rec) Me.DoubleBuffered = True Me.Invalidate(rec) 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.
-
Apr 25th, 2010, 02:45 AM
#10
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?
-
Apr 25th, 2010, 02:56 AM
#11
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|