How to show new form without disabling the parent form? - c#

I have shown a new form in button click on parent form like below,
Here the background parent form gets disabled when a new child form is activated.
Is there any available options to show the child form without disabling the parent form?
Regards,

The Show function shows the form in a non modal form. This means that you can click on the parent form.
ShowDialog shows the form modally, meaning you cannot go to the parent form
Application.Run() runs the main parent form, and makes that form the main form. Application.Run() is usually found in main.

If it is fully disabled (no interaction possible), you are using .ShowDialog() on the child form instead of .Show(). If you use .Show() you will be able to use both forms
Example:
ChildForm childForm = new ChildForm();
childForm.Show();

after you open the new form and call this.Activate(); it will refocus on the parent window, but this will cause it to loose focus for a fraction of a second

Related

How to control parent form from modally called child form

Is it possible to control the parent form from a modal child form?
My only idea is to close the parent form and show child form via ChildForm.Show() and not ChildForm.ShowDialog(parentform), can I control parent form from modally displayed child form?
How to refresh a parent form automatically upon saving the update on the child form?
You can access the parent (Owner) form by using Owner property. If you show the dialog using:
childForm.ShowDialog(this);
(this will be the form from where you are showing the dialog)
then in the dialog:
this.Owner.BackColor=System.Drawing.Color.Coral;
This will change the background colour of the parent form immediately.
ParentForm is available in the context of MDIForm context.

Minimizing child modal form without minimizing main form and recover child modal form

I have a modal form which will do some long operation, during that operation user wants to minimize the modal form and do some other activity in Main form and the progress bar for modal form is shown in Main forms status bar
When user clicks on the status bar then modal form should appear to the user.
Summarize:
We have Main Winform and Child Modal Winform
The child modal form has some long operation which is shown by Progress Bar
If user minimizes the Child Form. It completely minimizes the Main From
But user want to minimize only the Child Form and do some operation in Main form
I tried two approaches
Hiding the modal from on minimize event. The Child form is hidden
then i am unable to recover the child form instance show it back from
the main form since the object of modal form is disposed
using (Form1 reportForm = new Form1(dbConnection))
{
reportForm.ShowDialog();
}
If i do the minimize of modal form it minimizes the main form also.
In one stackoverflow it is mentioned that modal form is minimized then main form also will minimize Main form, this is right behaviour by design.
Please give me an approach for this issue.
The whole point of a modal form is that the user can't do something else in the parent form.
Assuming your long running operation depends on input from the modal form you should do this:
Keep the form modal
Move the long operation into a backgroundworker
start the background worker upon closing of the modal form
open new non-modal form to show the progress from the background worker.
Try this link...
https://msdn.microsoft.com/en-us/library/7aw8zc76(v=vs.110).aspx
protected void MDIChildNew_Click(object sender, System.EventArgs e){
Form2 newMDIChild = new Form2();
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
// Display the new form.
newMDIChild.Show();
}

How to prevent clicks outside the current form?

I want the form which will open when the user click a button, I don't want the user to click anywhere else outside this form(preventing him from clicking any other buttons on the parent form), I want to restrict his actions inside this form until he close it (For sure this form is top most), and I want the form to make alert(flicker) to any clicks outside it so the user will understand that he have to close it first.
When opening the form instead of
Form form = new Form();
form.Show();
use
Form form = new Form();
form.ShowDialog()
Super easy fix!
I assume your code to show the form right now is
form.Show();
To create/show an instance of the form that is modal (restricts clicking to that form) all you need to change is that line to:
form.ShowDialog();
Liam

Focus textbox on main form after closing a second form

How do I focus a TextBox element on the main form after closing a second form which is opened by calling ShowDialog()?
I have tried to call MainForm.TextBox.Focus() in the closing and closed event of the second form, but this won't focus the textbox.
I am using System.Windows.Forms on the compact framework.
Thanks.
From your second form, make a button (or another control) return a DialogResult by going into Properties. When you want the second form to close (ie after you press a button) make it return a specific DialogResult. In your main form you can do this:
if(secondform.ShowDialog() == DialogResult.OK)
{
textBox.Focus();
...
}
Calling ShowDialog() will block until it is closed so you could simply do:
secondform.ShowDialog();
textbox.Focus()
However the first example is for when you only want to make the textbox have focus after you press a certain button or do an action on the second form.
ShowDialog means, it's a modal window and the focus won't go back to main form until you close second form. You can set focus back in the same code which you used to open the second form.
SecondFrm.ShowDialog();
Textbox.Focus();
ShowDialog() will only return when the second form is closed, so you can write MyTextBox.Select() right after the call.
SomeForm form1 = new SomeForm();
form1.ShowDialog();
here you're showing the new form.
When you close it, you will execute methods after that, so add
yourTextbox.Focus();
so, its:
SomeForm form1 = new SomeForm();
form1.ShowDialog(); // do what you want in your form, then close it
yourTextbox.Focus();

how to enable focus in the main frame, when a new window was opened

C#
how to enable focus in the main frame, when a new window was opened and there is no way to activate actions in the main frame.
Thanks,
If you are opening the new window from within the main window, then you can make your new window modeless and set this.Focus() to set focus.
From MSDN:
... A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application
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.

Categories