Answered by:
Form with dynamic controls

Question
-
hai friends,
I have to create a form with dynamic control means I have to change the position of textbox and apply colours to controls at run time and I have to draw rectangle,circle etc at run time , work something like mspaint.Please help me with the code or links..
with regards
Lince- Moved by eryang Thursday, December 3, 2009 6:38 AM not bcl issue. (From:.NET Base Class Library)
Wednesday, December 2, 2009 11:19 AM
Answers
-
Hi Lins,
To add controls automatically, we can create control instances and add them to the Controls collection of the Form. To draw some shapes, we can override the OnPaint method of the form and call some methods of the Graphic class. This is a code snippet:
public class PaintForm : Form { public PaintForm() { } //This method shows how to add controls automatically private void AddControlsAutomatically() { //Creata a button control and set its properties. Button bt = new Button(); bt.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; bt.Name = "ok"; bt.Text = "OK"; //Add the button control to the form this.Controls.Add(bt); //Add other controls here. } //This method shows how to draw some shapes on a form protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); //Do some custom drawing here //Draw a rectange e.Graphics.DrawRectangle(Pens.Red, new Rectangle()); //Draw a string e.Graphics.DrawString("Hell0", this.Font, Brushes.Green, new PointF(100, 50)); //Draw other shapes here } }
If you want to draw like in mspaint, you also need to handle some mouse events, such as MouseMove event, to update some states and call the Invalidate method to repaint. Of course, in the OnPaint method, you need to paint the shapes using the updated states.
These are some links:
Graphics class: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx
Mouse events: http://msdn.microsoft.com/en-us/library/ms171542.aspx
Let me know if this does not help.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.- Proposed as answer by syntaxeater Tuesday, December 8, 2009 7:42 AM
- Marked as answer by Aland Li Wednesday, December 9, 2009 11:52 AM
Tuesday, December 8, 2009 7:01 AM
All replies
-
Hi.
Please use one account and one account only. There's no reason to post the exact same post under a different user id. I have deleted the duplicate post posted by Elby Paul.
-David
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowserWednesday, December 2, 2009 1:09 PM -
Hi Lins,
To add controls automatically, we can create control instances and add them to the Controls collection of the Form. To draw some shapes, we can override the OnPaint method of the form and call some methods of the Graphic class. This is a code snippet:
public class PaintForm : Form { public PaintForm() { } //This method shows how to add controls automatically private void AddControlsAutomatically() { //Creata a button control and set its properties. Button bt = new Button(); bt.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; bt.Name = "ok"; bt.Text = "OK"; //Add the button control to the form this.Controls.Add(bt); //Add other controls here. } //This method shows how to draw some shapes on a form protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); //Do some custom drawing here //Draw a rectange e.Graphics.DrawRectangle(Pens.Red, new Rectangle()); //Draw a string e.Graphics.DrawString("Hell0", this.Font, Brushes.Green, new PointF(100, 50)); //Draw other shapes here } }
If you want to draw like in mspaint, you also need to handle some mouse events, such as MouseMove event, to update some states and call the Invalidate method to repaint. Of course, in the OnPaint method, you need to paint the shapes using the updated states.
These are some links:
Graphics class: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx
Mouse events: http://msdn.microsoft.com/en-us/library/ms171542.aspx
Let me know if this does not help.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.- Proposed as answer by syntaxeater Tuesday, December 8, 2009 7:42 AM
- Marked as answer by Aland Li Wednesday, December 9, 2009 11:52 AM
Tuesday, December 8, 2009 7:01 AM