How to add one button onto a winForm with code in the design environment
-
I want to add some Label and Button controls on a winForm with the code(not by dragging the label and button from toolbox) in the design environment(because there are many labels and buttons need to be created ),I created a component which has a right-click menu under the design environment,when selecting one item of the menu it shows a window, and there is one button in the shown window,Now I want to add the label and button into winform that the component is located after clicling this button. Could you give some codes to domenstrate to create one button on the form by this way? Thanks in advance!
-
I want to add some Label and Button controls on a winForm with the code(not by dragging the label and button from toolbox) in the design environment(because there are many labels and buttons need to be created ),I created a component which has a right-click menu under the design environment,when selecting one item of the menu it shows a window, and there is one button in the shown window,Now I want to add the label and button into winform that the component is located after clicling this button. Could you give some codes to domenstrate to create one button on the form by this way? Thanks in advance!
Have a look in your form's designer.cs file and see how the IDE does it and replicate that in your code. Something like:
// create a Button instance
Button button = new Button();
// set required properties
button.Text = "&New Button";
button.Location = new Point(12, 12);
// add to form's controls (this)
this.Controls.Add(button);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) -
Have a look in your form's designer.cs file and see how the IDE does it and replicate that in your code. Something like:
// create a Button instance
Button button = new Button();
// set required properties
button.Text = "&New Button";
button.Location = new Point(12, 12);
// add to form's controls (this)
this.Controls.Add(button);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)Dave,thank for your help. I know if I write the code as you suggested and euecute it under the running time, It can create one button, but in the design time, If I execute the same code by running the button in the popup winform, it can not create button in the design winform. Could you give some advice? Thanks!
-
Dave,thank for your help. I know if I write the code as you suggested and euecute it under the running time, It can create one button, but in the design time, If I execute the same code by running the button in the popup winform, it can not create button in the design winform. Could you give some advice? Thanks!
In order to do what you want, you will have to write your own version of the Windows Forms Designer. A lot of work. Are you aware that if you hold down the Control key whilst selecting a component from the toolbox, you can then add multiple instances to your design surface by clicking in several places? To end this behaviour press the Escape key. So, Ctrl-Click on Button control in Toolbox, move mouse over Form/Panel/whatever and click wherever you want a button. Added all the buttons? Press Escape or click on the next component you want from the Toolbox.
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.”
-
In order to do what you want, you will have to write your own version of the Windows Forms Designer. A lot of work. Are you aware that if you hold down the Control key whilst selecting a component from the toolbox, you can then add multiple instances to your design surface by clicking in several places? To end this behaviour press the Escape key. So, Ctrl-Click on Button control in Toolbox, move mouse over Form/Panel/whatever and click wherever you want a button. Added all the buttons? Press Escape or click on the next component you want from the Toolbox.
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.”
Thank,Thank for your assist! It seems to refer the EnvDTE80.DTE2 of VS2005, Because if we can create the label and button on the winform using code dynamically create them, it will save more time in our design phase. But I try to find the way how to realize it.
-
Thank,Thank for your assist! It seems to refer the EnvDTE80.DTE2 of VS2005, Because if we can create the label and button on the winform using code dynamically create them, it will save more time in our design phase. But I try to find the way how to realize it.
If you want to add the button and a click event handler, you can do something like this: namespace Test { public partial class MosForm : Form { public MosForm() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { Button Mybutton = new Button(); Mybutton.Text = "My Button"; Mybutton.Location = new Point(12, 12); this.Controls.Add(Mybutton); Mybutton.Click += new EventHandler(Mybutton_Click); } private void Mybutton_Click(object sender, EventArgs e) { textBox1.Text = "All done"; } } }