C# Add Form to running application - c#

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

Related

Multi form Projects in c# - Displaying multiple windows

My main form is this
Now my problem occurs when I click Add -> Student, which is meant to bring up a new window that, instead it does this
So as you can see it opens the new form inside the same window. It didn't use to do this, then I made the main form an MdiContainer and set it as the MdiParent of the second form.
Is there a property I can change to make this new form pop up in a new window? Becuase I need to leave the MdiContainer property as True so that I can take data from Form2 and use it in Form1
Please let me know any other information you may need to help me fix this issue, as I'm not sure what caused it so I don't know what to change in order to fix it or even where I'm meant to be looking
you need to hide form enroll
when you open add window add.show()
you need to hide enroll form enroll.hide()

C# Set startup form in another form besides main

Is it possible to set the startup form in another cs file besides program.cs from main?
Code to set a form as startup object in program.cs from main:
Application.Run(new MyForm());
So for instance, is it possible to set the startup object in Form2, to be Form2, while Form2 is not the startup object (yet hopefully)>?
TO BE SPECIFIC: I wanted to show form2 on startup programmatically, while Form1 was set as startup object. I need to place that code in form2 itself. So I couldn't just simply open Form2 via Form1.
Thanks in advance!
Instead of putting startup logic in forms, you probably would be better served by an ApplicationContext.
http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext(v=vs.110).aspx
Instead of launching a form, you create a class with a [STAThread]Main() function and you make that class your startup object. Within that class you can do some setup (for example, verify that key logging capabilities are present) and then
//ExampleContext is defined as ExampleContext : ApplicationContext
using (ExampleContext exampleContext = new ExampleContext())
{
Application.Run(exampleContext); //The context handles the login and main forms.
}
Inside the application context you can open and close forms freely and you can maintain a global state that will be able to track them. Closing forms won't close your application, allowing you to have multiple forms that appear "top level" to your users.
To exit the application, have your context call Application.Exit() instead of closing the "main" form (because there isn't such an animal).
Based on your comments, it sounds like what you want to do is just create and show a new Form. All you need to do to do this is put code like this in your Button_Click event:
Form2 newForm2 = new Form2();
newForm2.Show();

Can't close a form from other form

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.

C# Windows Form Application --> Switching Between Multiple Forms

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();

How can I create a new C# Window based on my existing Window?

I know this mite be a bit of a silly question but how do i create another window from my main window in c# windows application? I don't know where to look for this type of questions.
You can use the following to create a new form. Note that I have provided two examples.
// This example creates a new Form control. While this Form is open,
// you cannot gain focus of the parent form.
Form form = new Form();
form.ShowDialog();
// If you want to be able to use both Forms. Then this is what you want:
Form form = new Form();
form.Show();
Also, MSDN is your bestest friend: MSDN on Windows Forms.
...And Google.
What about:
YourForm newForm = new YourForm();
newForm.Show();
You have several methods of showing your form. I use YourForm as a name here, replace that with the classname of your own form.
Note that a form-class is nothing more than a regular class that can be instantiated like any other class using new and inherits all methods from it parent calls (Form in this case), which includes the methods Show and ShowDialog. You can create as many instances of your class (i.e., of your form) as you like.
I will assume you are using winforms and will walk you through a simple example:
In Solution Explorer, Right Click your project and select Add | New Item...
Select the type About Box and you will see a new AboutBox1.cs get generated.
Select View | Toolbox to get the Toolbox to appear.
On your main form, drag a Button from the ToolBox | Common Controls onto the form.
Double click the newly created button to create the Clicked Event.
the clicked event type the following code:
AboutBox1 aboutBox = new AboutBox1();
aboutBox.ShowDialog();
This code will declare a variable aboutBox of type AboutBox1 and then instantiate it ( construct it ). Then you call the ShowDialog() method to get it to appear.

Categories

Resources