Today I will tell you about how to use additional form into C# source code without using Form Design.
First, you need to open under Visual Studio 2015 a new project Windows Form Application.
This project will come with a windows form.
Press Ctr+Alt+X keys and will see the Toolbox with all Windows controls.
Add one button to your window form with double left click.
Use double left click to your button and will see the source code of your form.
The C# script will be Form1.cs and for your button will have this:
1 2 3 | private void button1_Click(object sender, EventArgs e) { } |
If you want to open a new form with your button then you need to add source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | private void button2_Click(object sender, EventArgs e) { Form fInfo = new Form(); fInfo.Text = "Info and about !"; fInfo.Size = new Size(350, 200); fInfo.StartPosition = FormStartPosition.CenterScreen; Label myText = new Label(); myText.Location = new Point(15, 15); myText.Width = 300; myText.Height = 15; myText.Text = "This software is created by the website: free-tutorials.org!"; myText.Visible = true; fInfo.Controls.Add(myText); fInfo.ShowDialog(this); } |
This source code will make a new form named fInfo with this text: Info and about !.
Into this form will have a label named myText with this text: This software is created by the website: free-tutorials.org!.
The form fInfo add the label myText and finally show this form.
The source code comes with size, height, weight, and location to show the form and label to design form.
The StartPosition is used to put the form into the center of your screen.