How to place user controls dynamically on a windows form
-
Hi, In my project I have to place the existing user controls dynamically on a form at the given location. That is I will give the X-axis and Y-axis values in the text boxes dynamically then the existing user control should be dynamically created at that location. If any one have any idea to solve this, please reply me as soon as possible. Thanks in advance.
-
Hi, In my project I have to place the existing user controls dynamically on a form at the given location. That is I will give the X-axis and Y-axis values in the text boxes dynamically then the existing user control should be dynamically created at that location. If any one have any idea to solve this, please reply me as soon as possible. Thanks in advance.
This is quite trivial, you should buy a book and read it. The Petzold book is good in this regard. You add the control to the form's controls collection and you set it's position.
Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
-
Hi, In my project I have to place the existing user controls dynamically on a form at the given location. That is I will give the X-axis and Y-axis values in the text boxes dynamically then the existing user control should be dynamically created at that location. If any one have any idea to solve this, please reply me as soon as possible. Thanks in advance.
If using 2.0 or above you can use a generic function such as this in your form.
private void AddControl<T>(string text, Point location, Size size) where T : Control, new()
{
T control = new T();
control.Text = text;
control.Location = location;
control.Size = size;
this.Controls.Add(control);
}and call it like
AddControl<Button>("New Button", new Point(12, 12), new Size(75, 23));
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Hi, In my project I have to place the existing user controls dynamically on a form at the given location. That is I will give the X-axis and Y-axis values in the text boxes dynamically then the existing user control should be dynamically created at that location. If any one have any idea to solve this, please reply me as soon as possible. Thanks in advance.
unless I'm missing the point entirely wouldnt it be something along these lines:
yourControl.Left = Convert.ToInt32(yourXPosTextBox.Text);
yourControl.Top = Convert.ToInt32(yourYPosTextBox.Text);or at least thats how I position controls within a form, if you mean something else ginore me :)
-
If using 2.0 or above you can use a generic function such as this in your form.
private void AddControl<T>(string text, Point location, Size size) where T : Control, new()
{
T control = new T();
control.Text = text;
control.Location = location;
control.Size = size;
this.Controls.Add(control);
}and call it like
AddControl<Button>("New Button", new Point(12, 12), new Size(75, 23));
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)nice solution
-
If using 2.0 or above you can use a generic function such as this in your form.
private void AddControl<T>(string text, Point location, Size size) where T : Control, new()
{
T control = new T();
control.Text = text;
control.Location = location;
control.Size = size;
this.Controls.Add(control);
}and call it like
AddControl<Button>("New Button", new Point(12, 12), new Size(75, 23));
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus):cool::thumbsup:
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
If using 2.0 or above you can use a generic function such as this in your form.
private void AddControl<T>(string text, Point location, Size size) where T : Control, new()
{
T control = new T();
control.Text = text;
control.Location = location;
control.Size = size;
this.Controls.Add(control);
}and call it like
AddControl<Button>("New Button", new Point(12, 12), new Size(75, 23));
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)Well blinking 'eck. I never knew that Control had a text property. Just assumed it was lower down in the class hierarchy on certain controls. That's what I've learnt today. :)
Regards, Rob Philpott.
-
Well blinking 'eck. I never knew that Control had a text property. Just assumed it was lower down in the class hierarchy on certain controls. That's what I've learnt today. :)
Regards, Rob Philpott.
Control has got way too many properties IMO. Deriving from it to create a new control is a PITA as you have to try and hide all the non relevant ones (and methods too) which is not only messy/time consuming but very non OOP. If they had a IControl interface that just had the basics required, and a form's Controls property was a list of IControl it would be way better. Something like
interface IControl : ISynchronizeInvoke, IWin32Window
{
bool Enabled { get; set; }Point Location { get; set; } Size Size { get; set; } bool Visible { get; set; }
}
public abstract ControlBase: IControl
{
// Implement derived stuff here
...
//
}It'll never happen though :( [Edit] Made a mess of Interface with ControlBase stuff in it :doh: [/Edit] Actually, now I've thought about it a few minutes - just the ControlBase (would need a little more than I provided so we can have OnPaint etc to override) would be enough. Give me ten minutes and I'll probably change my mind again!
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)modified on Tuesday, May 12, 2009 11:20 AM