I want to open a new form in C#, so I write:
Form2.Show()
But Visual Studio tells me this error:
An object reference is required for the non-static field, method, or
property Control.Show()
The error is generated because you need to call the Show method on an instance of Form2 class, which means that you need to create an object and call that method on that object:
Form2 form2 = new Form2();
form2.Show();
I really suggest you to learn the theory of Object Oriented Programming in deep, so that you can actually understand what's going on whenever you might encounter such errors.
When a method is not static, you can only use it on an instance of the class.
Form2 form = new Form2();
form.Show();
Firstly: Reply to the the main questionnaire.
I think you are from Visual Basic .NET background so you are trying to open the form same like Visual Basic .NET way to open form.
In C# you can not open a form like that - you have to make an object of the form first.
Secondly: Reply to answering persons, myForm.show() is used when we are loading the form in any form container.
Form2 myForm = new Form2();
myForm.ShowDialog();
with the code the form will be opened.
Related
Simple question:
I used Application.Run to start my application because I need it this way.
Now I want to add a form later in the code. But it doesn't open the form if I use new Form1. Instead it runs everything else in the constructor of the class Form1. So somehow it ignores to open the form.
Just newing up a form makes it exist in memory but does not display anything.
You have to show it:
// create instance of Form1, does not show it
var myForm = new Form1();
// show the form.
myForm.Show();
see msdn documentation
I have a program that's working pretty well. It's a character sheet program for a game I'm running. Right now, since it only supports 1 active window, I have to save the existing sheet and open a new one. Thus, I can only work with one sheet at a time.
Is there a way in VS2013 C# winforms that I can work with multiple copies of Form1?
This is definitely a noob question, but I'm having trouble finding help on it.
Just create a new instance of your class and call Show()
var newForm = new SomeForm();
newForm.Show();
Yes, just new up another form:
Form1 form = new Form1();
form.Show();
Maybe this is a really dumb problem, but I can't close a form.
This is what I'm trying to do:
Start Main form -> Open second form -> Open third form and close the second form..
I use this code to open the second form:
this.Hide();
System.Threading.Thread.Sleep(200);
pauzescreen p = new pauzescreen();
p.Show();
And I use this code to open the third form:
this.WindowState = FormWindowState.Maximized;
Form1 form1 = new Form1();
form1.TopMost = true;
form1.Show();
form1.Activate();
And then I close the second form with this code: (Here is the problem, this doesn't work..)
pauzescreen pauze = new pauzescreen();
pauze.Hide();
Can't explain it very well, but what it does it creates an fullscreen screen capture at the second form (Pauzeform) and at the third form you can select an region.
The second form and third form have no border and are maximized.
You are creating two separate references to two separate forms: p and pauze. To close the original form you would need to retain the reference and call Close() or Hide() on that:
pauzescreen p = new pauzescreen();
p.Show();
// other stuff
p.Hide();
I have a multiform application, and I'm quite fond of using the Program class.
static class Program
{
// declare the forms in the program member space
static Form1 firstForm;
static Form2 secondForm;
static Form3 thirdForm;
}
What I do is in the Main method, initialize the forms.
static void Main()
{
firstForm = new Form1()
// ... so on and so forth
}
Then, whenever you want to show or hide those forms, use
// To hide a form, use its Hide method
Program.firstForm.Hide()
// To show a form, use its Show method
Program.secondForm.Show()
in your code. It's worked in my applications just fine like that. :)
As Sid pointed out, the main problem is that you're creating a new instance of your second form when writing
pauzescreen p = new pauzescreen();
As you're trying to close it in a part of the code different from where your form's instantiation is, one fittable solution, instead of handling events, is to keep reference of your object by passing it as a parameter to the class or method attempting the closure.
Here is an example of how you could work with the same object in two different classes.
I am developing Windows Form Application using Microsoft Visual C# 2010
I have two forms I made through Visual Studio GUI now I want to switch to next form on Button Click event this.hide() works but when I write next form name followed by dot than show() method does not show it seems show method is not available for it.
How can I show my new Form as It is not dynamically created I have already created it through Visual Studio GUI
When I Create It Dynamically As
Form myform = new Form();
than
myform.show();
show function exists but in my case this does not exists
Kindly tell me what's the problem
You need to create an object of the Form2 and call the show method on instance of Form2
Form2 form = new Form2();
form.Show();
EDIT: Since you edited your question and added the part Form myform = new Form();. You are actually creating an instance of base class Form. You need to create the instance of your Form which is inheriting Form class.
public partial class Form2 : Form
You need to create instance of Form2 class in this case. But your myForm should have Show method. Are you sure you have System.Windows.Forms; in using statement and Form class is from the System.Windows.Forms;. Try
System.Windows.Forms.Form myform = new Form(); //Although this is wrong but it should have show method
myform.Show();
Make an object of your second form then call Show() method
SecondForm frmsecond = new SecondForm();
frmsecond.Show();
Form myform = new Form(); is not needed here because you are trying to make myform as an object of Form Class
well.. 'you have two forms (and you are not creating them dynamically), and you want to switch from one to another on a click event', isn't ?
For example,consider you have created two forms namely Form1 and Form2 through VS GUI. And if you like to do something like
Form2.Show();
then you can't. Have you looked at your application' entry point, ie. Main() method of your application(is in 'Program.cs' most of the cases). Though you have created your Form1 statically, you will find
Application.Run(new Form1);
You got it now. You cant use a 'type'. You should use a variable.For that reason only we are creating the instance of your second Form and showing it.
Form2 form=new Form2();
form.Show();
I made 2 forms in C# visual studio.
How can I make one form invisible and another visible (Have done this in visual basic only before)
I guess Syntax should be similar.
Use the Form.Visible property, or the Form.Show() method.
To hide and show a form, use the Form.Visible property:
Form.Visible = true;
Form.Visible = false;
There's also methods that do the same thing (these are designed to be used with the MethodInvoker delegate):
Form.Show();
Form.Hide();
If I remember correctly, VB.NET will pre-create the forms for you, and you only have to call Show(). In C#, you will have to create all but the MainForm.
// in a buttonClick on Form1
Form2 f2 = new Form2();
f2.Show();
This will create a new instance each time you click the button.
Use the property Visible of the class Form.