Reputation: 11
OK, I am about 1 week into programming in C# ever...
I am writing a GUI application that has two forms.
Form1
is the main form, and have quite a bit of codes that are working nicely.
Form2
is supposed to be launched, when Form1 is handling exception..
Form1
and Form2
are both created in VS Solution Explorer, so they are not code created on the fly.
In Form1.cs ...
namespace Launcher
{
public partial class Form1: Form
{
// ...
private void button2_Click(object sender, EventArgs e)
{
try
{
//some codes
}
catch (SomeException)
{
Form Form2 = new Form();
Form2.Show();
}
}
}
}
In addition .. in Program.cs ..
using System.Windows.Forms;
namespace LauncherCSharp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Problem is .. Form2.Show()
will pop up a brand new Form2
, and not the one I've defined in Solution Explorer. I think this is related to the new Form()
line above, but if I don't have this new Form()
line, the compiler complained about Form2.Show()
..Cannot access non-static method "show" in static context.
Any ideas?
Upvotes: 0
Views: 1118
Reputation: 2522
You aren't actually creating Form2
Your code
Form Form2 = new Form();
Should probably be
Form2 form2 = new Form2();
or replace Form2 with the actual name of your form2.
In C#, we create an instances of an object (in your case the object is the form) by the following syntax
ObjectName yourVariableName = new ObjectName();
We can then refer to this object by using the "yourVariableName". e.g.
yourVariableName.Show(); // Show the form
Upvotes: 1