Calling Form Buttonclick event from another file
-
Hi, In windows form application I added one button "Draw Shape" and four radio button "Line, Circle, Rectangle, Triangle" I have an abstract class Shape i inherited that Abstract class and created four class Line, Circle, Rectangle, Triangle On clicking the button "Draw shape" i need to draw the selected shape on the page. Eg: In the case of circle i have function Draw() From the main form i am calling Draw() function of the Circle class In the Draw() function i am Drawing the circle override public void Draw() { Graphics mg = CreateGraphics(); mg.DrawArc(new Pen(COLOR, THICKNESS), 100, 100, 45, 40, 0, 360); } I tryed like this.. But getting error How can i resolve this issue? How can i paint the Form class from my class Circle? Please Help me. Regards, Ratheesh.
-
Hi, In windows form application I added one button "Draw Shape" and four radio button "Line, Circle, Rectangle, Triangle" I have an abstract class Shape i inherited that Abstract class and created four class Line, Circle, Rectangle, Triangle On clicking the button "Draw shape" i need to draw the selected shape on the page. Eg: In the case of circle i have function Draw() From the main form i am calling Draw() function of the Circle class In the Draw() function i am Drawing the circle override public void Draw() { Graphics mg = CreateGraphics(); mg.DrawArc(new Pen(COLOR, THICKNESS), 100, 100, 45, 40, 0, 360); } I tryed like this.. But getting error How can i resolve this issue? How can i paint the Form class from my class Circle? Please Help me. Regards, Ratheesh.
ratheeshnair123 wrote:
getting error
That is zero information. Be specific if you want specific help.
ratheeshnair123 wrote:
Graphics mg = CreateGraphics();
Unless you are doing highly advanced stuff, this is bound to be wrong, unnecessary and a big waste. Here is one of my standard texts, I suggest you read it carefully: there are several steps to correctly draw something; it does not matter how complex the paint job is: from a single line, to a complex drawing, or a real work of art. To make sure it all becomes visible on the screen and gets repainted automatically when moving, resizing, minimizing/maximizing/restoring or uncovering your Form, one should follow these steps: 1. decide upon what object you want to draw; it normally is a Control (e.g. a Panel) or a Form itself. I prefer to add a Panel to a Form, then draw on the Panel. And I do not like PictureBoxes, they are pretty useless. 2. create some variables (Rectangle, struct, class, whatever) that hold the parameters of your drawing. For a rectangle that could be top and left coordinate, and width+height, or just a Rectangle. etc. For a complex drawing, it could be a List of objects that derive of a common type, each having its own PaintMe() method. 3. create a Paint handler (either add your own paint handler to the Paint event, or override the OnPaint method) for that Panel, and do all your drawing in there, using the Graphics object inside the PaintEventArgs, and your variables. Do not call CreateGraphics! 4. if and when you want to change things, modify the variables and call Panel.Invalidate() or one of its overloads (for selective invalidation). 5. If you want to animate things, perform the move (step 4) inside the Tick handler of a Windows.Forms.Timer which ticks on the GUI thread, so you are allowed to call Invalidate() from there too. BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them alive in class members (hence create them only once); or create them inside the Paint handler and don't forget to call Dispose() on them. C# example:
private Panel panel;
private bool paintRectFlag=true;
private Rectangle rect=new Rectangle(20, 20, 300, 200);
private Pen rectPen=Pens.Black;public Form1() {
InitializeComponents();
panel=new Panel();
panel.Bounds=new Rectangle(…);
panel.Paint+=panelPaintHandler;
Controls.Add(panel);