[RESOLVED] [2.0] Problem drawing on a form that's been spawned dynamically
Not sure how else to explain it.
Here's the problem:
I have a class library.
I call one of the functions in the class library from my regular app.
It creates a new form (which is part of the class library).
The function that handles spawning the window initiates a method on that class library form that "should" draw on the picture box that's inside of the class library form, but it doesn't.
For testing purposes, I tried switching the background color of the picture box when it loads. This DID work. However when I use the Graphics object, it doesn't get triggered.
Here's the code I'm using to do the actual drawing:
Code:
public void DrawText()
{
Graphics g = CreateGraphics();
SolidBrush textBrush = new SolidBrush(Color.White);
Font textFont = new Font("Arial", 12F);
g.DrawString(defaultValue, textFont, textBrush, 0f, 0f);
g.Dispose();
textBrush.Dispose();
textFont.Dispose();
}
I tried every variant I could think of.
ie. Graphics g = picBox.CreateGraphics()
etc..
Why isn't this working?
Re: [2.0] Problem drawing on a form that's been spawned dynamically
You draw on controls in their Paint event handler because anything you draw using GDI+ is lost the next time the control is repainted. Your text is probably being drawn OK but then the control, or possibly the entire form, is being repainted straight after. Your DrawText method should assign appropriate values to some class level variables that are then read in the Paint event handler where the actual drawing takes place.
Re: [2.0] Problem drawing on a form that's been spawned dynamically
Hmm I dropped the drawing code into the picture box's paint event handler and the program crashes. The entry point to my main app says "invalid parameter".
I tried overriding the onPaint event, it doesn't crash then, instead nothing happens.
The drawtext was just a sample. Basically that class library form serves the purpose of 2 forms, depending on how I call the form.
To make it easy, let's say the class that handles spawning the form has 2 functions.
ShowA()
ShowB()
When I call ShowA, I'd like the picturebox on the form to draw different lines than when I ShowB.
Re: [2.0] Problem drawing on a form that's been spawned dynamically
Then you need to set your printing parameters in the ShowA and ShowB methods and then use those parameters in the Paint event handler of the appropriate control, e.g.
VB Code:
Private textToDraw As String
Private locationToDraw As Point
Public Sub ShowA()
Me.textToDraw = "Hello World"
Me.locationToDraw = New Point(100, 100)
End Sub
Public Sub ShowB()
Me.textToDraw = "Goodbye Cruel World"
Me.locationToDraw = New Point(50, 150)
End Sub
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
e.Graphics.DrawString(Me.textToDraw, Me.Font, Brushes.Black, Me.locationToDraw)
End Sub
Re: [2.0] Problem drawing on a form that's been spawned dynamically
Yeah that's what I'm doing for the most part.
Maybe you can clear this up for me, the error is being caused from something I do not understand, but it's resolved now.
You see my code above right?
When I change the part that inits the graphics object to:
Graphics g = e.Graphics;
Isn't that telling my new "g" object to be equal to e.Graphics?
Somehow this was making my entire project explode...
When I do it your way...
e.Graphics.DrawString(...);
It magically works, but I would like to know how I can draw/manipulate multiple things. I will be drawing numerous amounts of lines, some text, etc..
I guess:
e.Graphics.DrawString(...);
e.Graphics.DrawLine(...);
e.Graphics.DrawLine(...);
But how would I rotate the first line alone without having them as their own objects?
Re: [2.0] Problem drawing on a form that's been spawned dynamically
Oops. I forgot we were in C# land and posted VB code. Oh well, you seem to have got the idea anyway.
I'm not quite sure why creating a second reference to the Graphics object would be a problem. In theory it shouldn't matter what reference you use as long as it's still the same object.
I'm not quite sure what you're asking with that last question. Are you saying that you may want to draw a line using a different orientation depending on whether you come through ShowA or ShowB?
Re: [2.0] Problem drawing on a form that's been spawned dynamically
The last question was in reference to more than 1 graphic output on each form. I wasn't sure how the graphics object worked technically.
If I draw 1 line, then under that do something that manipulates the way the graphics object looks (like a rotatetransform), I assumed it would apply that to all the outputs, not just the outputs AFTER the rotatetransform/etc..
It's settled now. Setting e.Graphics to my own graphic object was actually ok, the error was being caused by disposing my graphics object. I'm still not sure why that would crash the program, but not disposing it fixes it.
Re: [RESOLVED] [2.0] Problem drawing on a form that's been spawned dynamically
Ah yes, the Graphics object is provided for you so you shouldn't be disposing it. It is probably used again after that method completes so if it has already been disposed that's why your app would crash. You should only be disposing Graphics objects that you create. If the creator does the disposing then there's no confusion.