C# Set startup form in another form besides main - c#

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

Related

C# Add Form to running application

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

How do I keep a form I launched using the .Show() method on the screen

So I have a program which is itself a console application, but which manages a few forms. The primary form has the ability to launch a secondary form, and I want it to be able to launch more than one of it's secondary forms.
The trouble is when I use the modal command Form.ShowDialog(), I can't navigate away from the secondary form and access the primary one, therefore I can't launch a second secondary.
The trouble I'm running into with Form.Show(), is as soon as the form is launched it closes again. Sure, it runs the initial function, but then it immediately vanishes. What is the best way to approach this modeless display, but keep the forms around?
EDIT:
Here is a bit of the format I'm working in. I have a self-defined Process class which handles the forms, each process stored in a list in the console app, and each with it's own myFORM
public class Process
{
/*The process class is used to track all running processes related to GeFoss(any forms or consol apps, etc)
* Methods:
* -initialize: This has two overloads, one for a form, and one for general objects. Basically this just starts the main part of the app
* -ShowDialog: This simply calls the Form.ShowDialog method if the process controls a Form
* -_NewThread: Begins a new thread centered on Leo
* -Leo: simply launches another copy of the same Object
*/
public Form myFORM;
public Type mytype;
.
.
.
.
.
. .
.
.
public void ShowDialog()
{
//This should only be called when the type of Process is a form. If it isn't a form though, the try-catch will prevent a crash
try
{
myFORM.Show();
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex));
}
}\
\
EDIT:
I was able to find a work-around. As the project I'm working on is sort of like a simulated OS, I told it to launch the main GUI Form using Form.ShowDialog(), and all the following forms using Form.Show(), and this seems to be working for me
you need to create another form. Form form = new Form();
Right now, you declare your object but you never create it. Form myForm is like int a; it doesnt have any variables.
For each object, you will be able to do the same as the class Form : form.Show();
You are referencing to the same object. That's why it is closing.

Owner of Form which is not inside Application.Run

I'm trying to show a Form before an application's start and get its DialogResult, so I'm just creating it and using ShowDialog (because Application.Run's return value is void).
What I'm worried about is that it might get 'hijacked' by mistake by some other Form that might be shown at the time. Not by this application, obviously. See What is the meaning of Form.Show(null)? that it's not advisable to use the parameterless overload of ShowDialog.
I have tested and seen that the Form's Owner property was null. But will it always be so? Or should I create a Form and use that as the Owner without showing it? That seems a strange solution but logically it should avoid any problem. Or will that introduce new ones?
Not by this application, obviously
This is already taken care of by Windows, it enforces a strong separation between processes and windows owned by threads. A typical choice for the owner of a dialog for example is the window returned by GetActiveWindow(). The active window is a property of a thread. Which explains for example why a MessageBox.Show() call made from a worker thread is never modal to the rest of the windows.
Making a window modal against the windows of another process is technically possible but requires lots of effort. The app would have to call AttachThreadInput(), a very unsubtle winapi function that nobody ever calls by accident. Also a great source of deadlock.
Unless you are programming in a boat near the Somali coast, there is no good reason to fear your window getting hijacked.
[STAThread]
static void Main()
{
Form1 form1 = new Form1();
//here I suppose the form you want to show
Form1 form2 = new Form2();
form2.ShowDialog(form1);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(form1);
}
Basically you use ShowDialog() when there are no parents for this window. Usually this happens for the main window. If you are opening one window after another, while closing previous, then there will be multiple ShowDialog()s.
If you are showing dialog (which is also a window), then you can specify it's parent to achieve a certain behavior. To example, when alt-tabbing to that window, it's dialog will be shown in front. Think about this as making child-parent relations.
I don't know about the case, when multiple forms are the claiming same parent. But it sounds like a clear mistake, to example:
public Form1 FormMain = new Form1();
...
// show main form
FormMain.ShowDialog();
...
// somewhere in the main form - show dialog
Form2 form2 = new Form2();
form2.ShowDialog(FormMain);
...
// somewhere in form2 - show dialog
Form3 form3 = new Form3();
form3.ShowDialog(FormMain); // wrong, should be form2!
This is not tested because I couldn't recreate a case where the parent is ever anything but null (i.e. a reference to a third-party un-managed parent), but maybe you could do something like this on your form to set the parent to null if it changes:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.ParentChanged += MyParentChanged;
}
public void MyParentChanged(Object sender, EventArgs e)
{
this.Parent = null;
}
}

How to pass a value selected on an initial form to the main form?

I want to do something like the answer here:
How can I close a login form and show the main form without my application closing?
...but I want to pass a value selected on the initial form to the next (main) form. If I call an overridden constructor on the main form, where do I store the value in the meantime (between the initial form being dismissed and the main form being called)?
OTOH, if, instead of using the program.cs file to do this, I create the "initial form" inside the main form's Load() event (is there a better place), I could do something like this, but admittedly it seems rather kludgy:
0) Set the main form's size to 0,0 to hide it
1) Show the "initial" form/modal dialog, and store the value the user selects (in a button click event) in a form-global variable
2) Once the initial form/modal dialog closes, set the main form's size back to what it should be (unless modal result <> OK, in which case I close the main form and the app)
I know there's a better way to do this...
You don't have to pass a value to the main form. Just like your link explains, open your main form first. Then your main form can open the other form. This other form can place the information in a public property that the main form can access. Since the main form controls the lifetime of this other form, the main form gets the information held in the other form's public property, then closes the other form.
string myVariable;
using (OtherForm otherForm = new OtherForm())
{
otherForm.ShowDialog();
myVariable = otherForm.OtherVariable;
}
Try using ApplicationContext (System.Windows.Forms.ApplicationContext). You can show multiple forms as shown in the example in the following MSDN thread. And regarding data,you can have a common data object which is created once and the forms are instantiated with the data object passed to them before showing them.
http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext%28v=vs.100%29.aspx

Design apllication with muliple forms within one main form

I want to make an application that have one main form that have one functionality = Add new form.
I dont know how forms will be created, they are created dynamically by the user (AddForm methos from the main form).
All the subs forms are the same but some of thier prop receive differnce parameters (it can be in the ctor or later).
I want to be able to close all the forms when I close the main form.
Before new form will displayed I want to display setting form (to take the form parameters) maybe with ShowDialog method and do validating check on the form and just if the form validate the new form will displayed, if not (or if the user prees cancel) the form will disposed.
I know aboout MDI but I really perfer other way
Any ideas?
Thanks!
Closing all forms when main form is closed is somewhat easy, you just pass the main form reference in Show() method of the "child" forms; e.g. if you show child from main, you do:
child.Show(this);
This needs to be done if the your main form is not the real "main form of the program", but you want to close all "child" forms.
However, wiring all this together would preferably be done in some special class for this purpose, maybe called ScreenRepository. In this class, you would have a collection of open forms at any moment, you would deffer form creation to this class (so that this class automatically injects form parent) etc... Having this class would be easy to re-activate (give focus) the form if it is behind other forms, create new form if needed etc...
Idea is simple create a application that open a main form a the beginning and then open open other forms if needed if you close main form rest of forms are also closed. Like in GIMP.

Categories

Resources