Reputation: 375
I have created a new Windows Form project in Visual Studio 2017.
I opened up the code editor for Form1.cs by right-clicking Form1.cs in the Solution Explorer and then clicking view code.
I then added in some code of my own so that Form1 is now as below:
public partial class Form1 : Form
{
Button button1;
Button button2;
public Form1()
{
InitializeComponent();
Size = new Size(700, 500);
FormBorderStyle = FormBorderStyle.FixedDialog;
button1 = new Button();
button1.Size = new Size(60, 40);
button1.Location = new Point(30, 30);
button1.Text = "Button 1";
this.Controls.Add(button1);
button2 = new Button();
button2.Size = new Size(60, 40);
button2.Location = new Point(100, 30);
button2.Text = "Button 2";
this.Controls.Add(button2);
button1.Click += new EventHandler(AnyButton_Click);
button2.Click += new EventHandler(AnyButton_Click);
}
private void AnyButton_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked a button!");
}
}
When I run the project, Form1 is drawn exactly as expected and the button click events work fine.
However, the designer view (Form1.cs[Design]) is showing me a blank form. Does anyone know how to fix this problem so that I can add my own code but it will be reflected in the design view?
Upvotes: 0
Views: 2320
Reputation: 6081
There are two ways to make the buttons appear before runtime:
MyUserControl.cs
file.MyUserControl.Designer.cs
) which is located by expanding MyUserControl.cs
. I would not recommend this though, as you can quickly make a mess of things.Want to know more?
As it can be seen in the image below, a Windows Form consists of three items:
A resource file (MainWindow.resx
): Can contain resources used by that form, but this is hardly relevant in this topic.
A designer file (MainWindows.Designer.cs
): The code version of the drag and drop designer. In this you can actually code your graphics, instead of using the graphical designer. I wouldn't recommend this though. If you like the idea of coding your UI, have a look at WPF instead.
The "Master" file (MainWindow.cs
): This should be your main access to working with your GUI. Double click it, at you will open the GUI in the Visual Studio designer window. Here you can drag and drop items on to your form, and modify their properties (and events). You can also right click it and select "View Code" to go to the code behind the form. This is also where event code will be generated.
Upvotes: 3