How to delete pen line?

22-degrees

Well-known member
Joined
Feb 11, 2012
Messages
156
Location
South East Ireland
Programming Experience
1-3
Hi all, my latest project has me adding a line graph feature to a form.

I use the following to draw:

VB.NET:
Dim myPen As New System.Drawing.Pen(col(a))
Dim formGraphics As System.Drawing.Graphics

x = ************
y = **************

x2 = ***********
y2 = ***********

formGraphics = Me.CreateGraphics()
formGraphics.DrawLine(myPen, x, y, x2, y2)

The lines are all drawing correctly and i have set a feature that allows the user to highlight/enhance specific graph lines by selecting the line owner from a listbox. This sets the pen.width to double the default and redraws that line. This option allows multiple lines to be highlighted on the graph for better analysis.

What I would like to be able to do next is give the user the option of selecting one of those line-owners from the listbox again in order to get the effect of resetting that line to its default width. (Un-highlight / un-enhance). Is there a way to directly "un-draw" a line or am I going to have to go around it the long way and log each and every highlighted line and do a complete re-draw if user de-selects a line-owner???

PS: This is my first time drawing on a form so be gentle if the answer is looking me in the face :)
 
OK so I knew I would see a bit more sense once I posted here!

I am thinking, if I create a FormGraphics() array then that allows me one canvass for each line. As far as drawing goes, this works just fine, but when it comes to clearing a specific canvass so i can redraw, that's where I am stuck.

' "a" is the integer value that represents the line-owner

I tried Formgraphics(a).clear but it is looking for a color to redraw the entire background of that canvass layer. This is obviously causing me problems as it loses its transparency and blanks over all other lines that have been drawn.

Is there a way to simply reset this layer without effecting any other layers?
 
So I ended up ditching the drawing and going with the vb.powerpacks lines instead. Took a lot more code than i would have liked but worth it in the end.

Simple line.visible = true or false takes care of the feature mentioned above and it looks and feels a lot smoother too. Just set the border width for enhance or de-enhance instead of having to redraw etc..

Anyone feel free to post an answer to the original question. I'm sure a time will come where using powerpacks lines is just not feasable..
 
The problem was that you were drawing it wrong in the first place. NEVER call CreateGraphics. If you want to draw on a control then ALWAYS do so on its Paint event, either by overriding the OnPaint method in a custom control or by handling the Paint event. You never actually delete anything you've drawn using GDI+ because everything you draw gets erased next time the control is repainted anyway. If you want to remove something you drew then you simply raise a Paint event and then don't draw it again. For instance, you might store the data for your line in two Point fields and use a Boolean field to indicate visibility. In the Paint event handler, you just test the Boolean and call DrawLine only if it's True. To raise the Paint event you first call Invalidated and specify the area(s) that has, or might have, changed and then call Update. You might like to check this out:

Very Simple Drawing Program
 
To raise the Paint event you first call Invalidated and specify the area(s) that has, or might have, changed and then call Update.

That should be just Invalidate.
 
Back
Top