I am confused about how to navigate between Forms in c#
I want to do the following:
from Form1 opening the Form2 and make Form2 on the top of the original one Form1 and then get back to Form1 and user are not allowed to use Form1 untill they close the child Form2 to get back to Form1
with the same scenario but I may pass parameter from Form2 to Form1
I searched but think are not clear in my mind I found that there is something called MDI Parent And Child like the answer here but
I do not want the child from to be inside the original one
I do not the original from style to be change and be in that gray one
that way I think what I need to use is NOT MDI Parent And Child
please help I appreciate long explanation description with example
This assumes that the data you are interested in on Form2 can be accessed by read/write properties. In my example, I show that as a simple string property called UsefulData. Here's the normal way to do what you are asking:
private void button1_Click(object sender, EventArgs e)
{
var dialog = new Form2(); //create an instance of Form2
dialog.UsefulData = "Some Useful Data"; //set one or more properties of that form
string result = "No Result Yet"; //a place to store the eventual data from Form2
if (dialog.ShowDialog(this) == DialogResult.OK) //passing "this" properly parents the dialog to your Form1
{ //note that I only get the data if the user pressed OK
result = dialog.UsefulData; //get the data
}
//do something with that result
}
There you go!
Related
I'm trying to work with multiple forms, what I want is change the way the form is depending on the selected index of a combobox, the only way I could think of is hide form1 and show form2, but the problem is when you close form2, the process does not end...I tried the code below
private void Form2_FormClosing(object sender, FormClosedEventArgs e)
{
foreach (var process in Process.GetProcessesByName("Process Name.exe"))
{
process.Kill();
}
}
if there isn't, is there a way the form can change on combobox selected index?
Try Application.Exit();
It exits your entire application and closes all your forms and threads.
Simply pass an instance of Form1 to the constructor of Form2, keep a reference to it in a form1 member
public class Form2 : Form{
private Form _form1;
public Form2(Form form1):this()
{
_form1 = form1;
InitializeComponent();
}
}
later you can simply use that reference :
_form1.Close();
This is a cleaner way to do it.
Other mechanisms are also ok, like implementing an eventhandler on form1 for an event in form2.
based on your pastebin code change this:
Form2 HeadquarterForm = new Form2(this);
you also only need the closed eventhandler and call close on the _form1 only once. So you don't really need the closing event handler.
The process is still running because form1 is still alive but hidden.
Try using Environmental.exit() to kill the process
Looking at your code in pastebin. Problem is, you're not passing Form1 in constructor of your Form2 when creating it. Change the part of your switch-case (4) to:
Form2 HeadquarterForm = new Form2(this);
I'm stuck at a problem and the old answers regarding the same problem are not quite recent enough so i thought it'd be okay to ask again.
My question is: How can i dock one form inside another form? Would it be more appropriate to use a Panel and a Form instead? Is the first option even possible?
Thanks in advance.
Create 2 forms, Form1 and Form2. Set TopLevel property of Form2 to false. In form load for Form1 add code
private void Form1_Load(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
this.Controls.Add(frm2);
}
This will include form2 in form1, you then have to set properties on form2 if you want to remove the title bar to make the form more like a Panel.
If you want to have some reusable panel that may be used on forms I think User Control will be what you are looking for.
I want to use a textBox, which is on my main form Form1 from other class. In class Form1 I can use:
this.Invoke(new EventHandler(displayText));
and then
private void displayAccFields(object o, EventArgs e)
{
tbAccRoll.AppendText(packParameters.getPackage(3) + "");
}
and it works fine.
How can I access this textbox for displaying something from a different class?
For sending values between two forms, you may
Send the values in the constructor of the second form. You may create a paramterized constructor and send the values when you initialize the form.
You may take a reference in to your first form in the second form.
In second form,
public Form1 objForm1;
and in First Form,
Form2 objForm2=new Form2();
Form2.objForm1=this;
and then you can use Form2's objForm1 to refer to Form1's textbox.
I have two forms form1 is main form and form two is model form I want to set the forms as below:
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show(this);
}
The above would set the form1 owner of form2 and form2 would be shown but the problem is that this will break the order of forms on press of Alt+Tab keys hence I have tried it with another way as below.
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
This would be works but the problem is that the dialogue forms will not allow me to maximise/minimise and close
My form2 is borderless form and it is set to show on specific location as to fit with main form1. My aim to do not shows the form2 in Alt+Tab list and as I close the form2 then form1 will show immediately without break order of form.
When I press Alt+Tab keys on first condition and try to close form2 then the other application shown instead of form1 which is I do not want.
Is there any solution of this problem?.
It really sounds like you could do the second form as a custom control.
See Microsoft's documentation and this set of examples.
Think of it as a standard control, like a Button, DataGridView, TextBox, or the like, except that you have total control over it. You can show or hide it, you don't have to worry about where it is positioned, it won't take focus away from the parent form, and so on. And you can put whatever other controls you want in it, encapsulate all their logic, etc.
A possible hack is to keep your parent form active after opening child form as a modal form, so that you could do maximize/minimize your parent as well. An extension method:
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool EnableWindow(IntPtr hWnd, bool enable);
public static DialogResult ShowDialogSpecial(this Form formToBeShown, Form parent)
{
parent.BeginInvoke(new Action(() => EnableWindow(parent.Handle, true)));
formToBeShown.ShowDialog(parent);
return formToBeShown.DialogResult;
}
You can call:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//additionally do f2.ShowInTaskbar = false to make sense.
f2.ShowDialogSpecial(this);
}
This wont let child form truly act as non-modal form, since child form can cover over parent form.
When my program runs it closes form1 after a few seconds. Depending on what happened during form1's lifespan I may want to open form2. Normally I would just make a new instance of form2 and use the show() method. But form2 is then a child of form1 and then also closes. Does any body have an idea on how to get this to work? thanks.
For multi-form applications I tend to have one form that is the "main" form, which opens up the sub forms.
The main form is the one that gets started with Application.Run(...)
In your case you might want to have a blank form that can be the controller, and have Application.Run call that.
That form can then start instantiate your Form1 and run it.
e.g.
public ControlForm : public Form
{
Form1 form1;
Form2 form2;
public ControlForm()
{
form1 = new Form1();
form2 = new Form2();
}
public void Start() // or something similar
{
form1.ShowDialog(); // will block showing the form, or you can do other tricks
// to show the form here
if(form1.someFlag) form2.ShowDialog();
}
}
This is just "psudo-C#" code, but hopefully the concept makes sense
Then your main function can just run "ControlForm"
Its just a concept you might want to try
You can open a new form in your application's bootstrapper (main method). You will want to call Application.Run(yourFormHere). You would have two of these in a row in the order you want to show the forms. You could store the results of the first form in some static location and check that before showing the second form.
I ended up doing this:
Auth f = new Form1();
Application.Run(f);
if (f.authed)
{
Application.Run(new Form2());
}
I don't think that your problem is that the Form2 instance is a child form of the Form1 instance, but rather that the Form1 instance is your applications main form. That will make your application quit whenever Form1 closed. One way to prevent this is to alter the main method to not set Form1 as the main form (see here for details on that).