Order Break Between Model And Main Forms c# - c#

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.

Related

How to close Parent form without closing child form in Windows form in c#

I am trying to make one windows application which contains two forms. In form1(Parent) i created one button for open second form. On that button click event i want to close form1(parent) form and open form2(child) without closing form2, but when i am press that button both forms are closed so how can I do it?
Rather than using .Close() consider using .Hide() on your parent form, this shouldn't dispose of it but rather have it hidden.
Note that exiting out of your Child form means your application won't exit. To circumvent this you should consider subscribing to the OnFormClosed event to handle the form closure.
If you look in "Program.cs" there is a form to start with.
When this form is closed, the application is terminated.
enter image description here
It should be used as a way to hide the startup form.
Even if the child form is closed with the start form hidden, the program does not end.
As #Ae774 said, you need to handle it separately.
If you want to close form1(parent) form and open form2(child) form without closing form2 by click the button, you can refer to the following code:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
Hide();
f2.ShowDialog();
Close();
}
If you want to open the parent form after the child form is closed, you can refer to this code:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
Hide();
f2.ShowDialog();
Show();
}
Here is the test result

How to close C# program, from the second form (not hide) C# Multiform issue

Problem: I have created a program that has two forms. When a button is pressed on Form1, the program "hides" Form1 and "shows" Form2. The issue is, when I close Form2, the program continues to run in the background, I believe this is because the program needs Form1 to be closed, not hidden in order for the program to end.
My Question is how do I override the Second Form to close the program and not hide?
private void btnCreate_Click(object sender, EventArgs e)
{
Form2.Show();
Form1.Hide();
}
I know I could show Form1 again from Form2, and then close, but I would really like to avoid that.
You have few options to do that, according to how your application is.
First and intuitive solution is to call Close() instead of Hide() for Form1. This works only if Form1 is not the main form of your application (the one inside main message loop started with Application.Run()).
Second solution is to add an event handler for the FormClosed event of Form2 and to close first form when second one is closed:
private void btnCreate_Click(object sender, EventArgs e)
{
Form2.Show();
Form2.FormClosed += new FormClosedEventHandler(delegate { Close(); });
Form1.Hide();
}
You can directly close Form1 or call Application.Exit() (in case you did open multiple forms you want to close all together).
Last solution is to make second form the owner of first one:
private void btnCreate_Click(object sender, EventArgs e)
{
Form2.Show();
Form1.Owner = Form2;
Form1.Hide();
}
This will close automatically the owned form when owner is closed. This is the solution I prefer but it works only for one form (an owner can own only one form).
You can use the FormClosed event:
private void btnCreate_Click(object sender, EventArgs e)
{
Form2.Show();
Form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
Form1.Hide();
}
void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1.Close();
}
In application development especially for winforms for the sake of your users, always have a main form (whether an mdiparent or a single parent) that will always be the main form that all other forms show from.
Giving an answer to the question will be encouraging bad programming practice.
Let the mainform close and the application will end. Hiding form1 will not end the application.

Control.Focus() bug

I have some problems with Form control focusing.
On form1 I click a button and run the code below:
private void btnTest_Click(object sender, System.EventArgs e)
{
form2 = new Form2();
Application.Idle += new EventHandler(Application_Idle);
form2.Show();
form2.Activate();
form2.textBox1.Focus();
Form3 form3 = new Form3();
form3.ShowDialog();
}
Then, after this CLR I run the event Application_Idle on which I add a method that must focus on the textBox2 control..
private void Application_Idle(object sender, EventArgs e)
{
form2.textBox2.Focus();
form2.textBox2.Select();
form2.textBox2.Focus();
Application.Idle -= new EventHandler(Application_Idle);
}
But when I click the button on form1, I see Form2 showing, Form3 showing and then Application_Idle method raise, but form2.textBox2 control doesn't get focused...
If I comment out the form3.ShowDialog(); line it's works fine, but how do I focus a form element with another form activation?(form3.ShowDialog()) ?
Remark added:
Problem in also is I have a strict architecture and all I can change is Application_Idle method.
The issue you are having is with modality:
Forms and dialog boxes are either modal or modeless. A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application.
Dialog boxes that display important messages should always be modal. The About dialog box in Visual Studio is an example of a modal dialog box. MessageBox is a modal form you can use.
Modeless forms let you shift the focus between the form and another form without having to close the initial form. The user can continue to work elsewhere in any application while the form is displayed.
When you use ShowDialog, the form that is shown prevents the caller from returning control until the dialog box is closed. If this is not the desired effect, you can use the Show method.
You could focus the textfeld, when the form itself got the focus:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.GotFocus += (s, e) =>
{
this.textBox2.Focus();
};
}
}
As John Koerner stated, you cannot set focus to Form 2 while Form 3 is open because of modality.
Since you stated that a user input in Form 3 is necessary to proceed, you should change your approach. You can place a listener watch for Form 3's closing. Only then can you set the focus somewhere else
form3.FormClosed += Application_Idle

How to hide a stack of modal forms in .NET Windows Forms

I have the following problem:
I open multiple modal forms in a stack (for example, form1 opens modal form form2 which in turn opens modal form form3, etc.). I would like to hide the entire stack.
I tried calling the Hide method or setting the Visible property on the parent, but this only hides the parent. I also tried hiding every form individually, but then I have to call ShowDialog on each of the forms which locks the thread in which I call the aforementioned method.
Is there be a way to set the modal dialogs so that they inherit the status of the parent and get hidden in a cascade just by setting the property on the first form?
I'm also open to other suggestions.
To re-show a form you hid by setting obj.Visible = false just set obj.Visible = true, not ShowDialog.
ShowDialog initiates a message loop, which will cause confusion since the dialog is already running a message loop.
Since you're talking about modal dialogs, it would be the last one opened that would commence this action. Open every form as in the following example, and then Hide() that last one.
public partial class Form1 : Form
{
Form2 frm2 = new Form2();
public Form1()
{
InitializeComponent();
frm2.VisibleChanged += frm2_VisibleChanged;
Shown += Form1_Shown;
}
void Form1_Shown(object sender, EventArgs e)
{
frm2.ShowDialog();
}
void frm2_VisibleChanged(object sender, EventArgs e)
{
if (frm2.Visible == false) Hide();
}
}

C#: Getting a button to display a preset form

I (currently) have two forms, one that needs to call the other. In other words, by clicking a certain button in Form1, Form2 needs to "pop up", like a dialog box. I know I have to get a reference in both forms, but I'm not entirely sure how to do this. What is a decent tutorial/beginner site to learn about GUIs in C#?
You can try this:
protected void Button1_Click(object sender, EventArgs e)
{
Form2 myForm = new Form2();
myForm.ShowDialog();
}
ShowDialog forces the user to close that window before s/he can access the rest of the application.

Categories