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();
Related
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.
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 two windows forms Form1 and Form2 and a c# class library CLProduct.
Now, Form1 is already open and a method in CLProduct class is called which shows Form2 and at this point I would like to restrict access to Form1 until Form2 is closed.
I have the below code in my method in CLProduct which load
var _frm2= new Form2();
_frm2.ShowDialog();
I know that ShowDialog loads a modal which by default restricts the access to Form1 when ShowDialog is called from Form1 but in my scenario ShowDialog is actually called from a Class which for obvious reasons doesn't restrict Form1 as it is not called from Form1
Form2 is running on a worker thread that's created in Form1 as follows
if (pPBackgroundWorker.IsBusy == false)
{
// Start the asynchronous operation.
pPBackgroundWorker.RunWorkerAsync();
}
Is there a way to handle this from Form1_Load()?
You should simply be able to call ShowDialog for From2 and pass Form1 as an argument and get the desired behavior. (_frm2.ShowDialog(_frm1))
But as HansPassant points out in the comments, if Form2 does not find the appropriate owner itself it most likely means that it is running on a worker thread which can result in some pretty nasty bugs/issues.
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.
In my application... to navigate between winforms what i do is that i make an object of the form that needs to be shown and i use
Register reg = new Register()
reg.show();
this thing has two problems
if i do it with a button, more than
one instance of same form could be
opened.
if i close through which the instance
was created, the child form stays
opend.
what is the solution....
have the child form take as a parameter the parent form:
Form2 f2 = new Form2(this);
this.hide();
f2.show();
then when you wish to close the new form you just close it and show the parent form again.
code from Form2:
private Form Fatherform;
Form2(Form father){
Fatherform = father;
}
Form2_closeevent( ... )
{
if(Fatherform != null)
Fatherform.show();
Take a look at this code sample from MSDN code gallery. If you go through the code in detail, you should be good to go