Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been working on this code:
Private Sub ABToolStripMenuItem_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles ABToolStripMenuItem.Paint


       Dim var As System.Drawing.Graphics
       var = Me.CreateGraphics
       Dim pen1 As Pen
       pen1 = New Pen(Color.Black, 1)

       'Horizontal line

       '1st moves the line further in, towards the middle.
       '2nd And 4th must be the same number And they bring the line down
       '3rd Is the length of the line

       'var.DrawLine(pen1, 50, 90, 600, 90)
       var.DrawLine(pen1, 50, 200, 600, 200)
       var.DrawLine(pen1, 50, 230, 600, 230)

       'Vertical line

       '1st And 3rd numbers must be the same number to keep the line straight
       '2nd move the position of the line inward
       '4th Is the vertical length

       var.DrawLine(pen1, 50, 200, 50, 1000)



   End Sub


I want to set it up where I click on the AB menu item then it populates, problem is that it populates on the form before I click anything. I just highlight the menu item and it automatically draws the lines. I would like to set it up to paint on menu item click.

Side note, I also have an event that adds labels based on ABmenuitemclick. The problem is when I add both together the lines do not show up at all. Reading forums I have to have an event that is specifically made to paint the lines. So that is why I separated the code.

What I have tried:

Tried switching to a .click event, but the program crashes from doing a paint eventargs with a click.
Posted
Updated 30-Apr-17 6:39am

1 solution

Stop doing that!
You should not construct a Graphics context in your Paint event, you should use the context that is provided in the PaintEventArgs parameter.
And if you construct any graphics related objects - particularly contexts, but also pens - you are responsible for Disposing of them when you are finished. If you don't you will get out of memory exceptions when there is loads of heap memory left. The Grabge collector will not try to dispose of these items until heap space gets short, and they are a very scarce resource.

And why are you trying to create a "custom printable form"? Use a PrintDocument and you can control exactly how your printout looks, without any reference to the form itself. Printouts don't normally look like forms, if only because you cant interact with a printout so printed buttons, DDLs etc. aren't good and make your app look amateur.
 
Share this answer
 
Comments
Member 11856456 30-Apr-17 15:24pm    
Griff,

To be honest I am an amateur at coding. I know how I want things to look, just don't know all the specifics to make it more appealing or better in terms of programming. Anyways I haven't had a chance to use any print document type coding. Can you draw lines and have data stringed in to fill a form to print. All I am trying to do is make a paper form (once printed out) that will have specific information from the custom on the sheet. Will I be able to do this through print document?
OriginalGriff 30-Apr-17 15:39pm    
Yes - it's not really complicated, though it can look that way when you first try it. But, since you only want a single page, a lot of complexity goes out the window.
Basically, you decide exactly what you want on the paper and where, and it does all the work - you just work with coordinates on the paper. Install a PDF writer print driver on your system - Google will find one for you - to save paper and try the example in the link. It'll show you the basics at least.
Member 11856456 1-May-17 2:07am    
2 Last things, is there a quicker way to judge points on a print document? Also, how can I set up a print document for each type of printing I would like to do? I am making multiple different things that need to be printed, but they are each about a page.
OriginalGriff 1-May-17 2:20am    
"is there a quicker way to judge points on a print document?"
I don't understand what you mean by that - please explain in more detail.

"how can I set up a print document for each type of printing I would like to do?"
Either use a different PrintPageEventHandler for each print type, or have a method for each different print type, and call the appropriate one from your single handler. (There is a "better" way, which involves deriving classes from PrintDocument and letting the system sort it out, but that's fairly advanced stuff and I don't think as a beginner you need to get your head around that yet).
I'd probably just stick with the different PrintPageEventHandler method for each type - it's simple and easy to understand.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900