How to prevent clicks outside the current form? - c#

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

Related

How to show new form without disabling the parent form?

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

new form doesn't allow to go back to the main form

I'm opening a new form like this:
Form formulario = new Editar_POIs());
formulario.ShowDialog();
Now, when the new form opens and I want to go back to the Main form (with the new form opened), I can't... Either if I click in the main form or if I try to switch using Alt + Tab it won't go back to the main form. I then need to close the opened form to be able to use the Main form again.
Any ideas how to avoid this? I want the user to be able to have the 2nd form opened while he does some operations in the main form, then go back to the 2nd form and enter some values the Main form displays.
Use
formulario.Show();
ShowDialog() does not let you go back and use the main form until the 'child' form is closed.

should not click on the previously form [C#]

In my form i have button to show other form.
But i want the previously form cannot be clicked before the new form is closed , How to create that ?
Because if the previously form is clicked, and i click the button again, the form is show multiple.
this my code in button click :
MDACS_AOP_CFSTL_InputActivity addProblem = new MDACS_AOP_CFSTL_InputActivity(ParameterSesi, ParameterNamaKaryawan, ParameterTanggal);
//addProblem.Close();
addProblem.Show();
You should use
addProblem.ShowDialog(this);
This will open the dialog as child of the parent (this) dialog. You cant click the parent dialog but you can still see it.
You need to use
addProblem.ShowDialog();
instead of
addProblem.Show();
so that it will open a modal dialog.
You could use
MDACS_AOP_CFSTL_InputActivity addProblem = new MDACS_AOP_CFSTL_InputActivity(ParameterSesi, ParameterNamaKaryawan, ParameterTanggal);
this.Hide;
addProblem.ShowDialog();
this.Show();
The show(); command will not be executed until the dialog is close. So it will stay hidden

get focus on first form

I have two forms form1 and form2. I navigate to form2 using a button in form1. In form2 I have a button control; on button click, I show messageBox. when messageBox comes then it also lost focus of form1 but I want that it should not lost focus of form1. I have no concern with form2.
There's no way a message box doesn't show like a dialog. Like Eliran Pe'er said, you should make a Form like a messagebox with a label and a button and use it like this.
MessageForm form = new MessageForm.Show();
If you use ShowDialog it's going to be the same thing as MessageBox.
In your form 1 you can use TopMost property = true in order to keep it in front all the time no matter what. But this is going to keep your form on top of all other open programs.
Another workaround would be after messagebox is closed by the user (this is not a bad option) you can call form 1 to BringToFront(). To do this, you can pass the instance of form1 to the form2 in the Show method. Use that parameter in your form2 constructor.
I don't think there's an easy way to prevent MessageBox from taking focus, and thats because a MessageBox is a dialog. (dialogs take focus from the program until they being closed)
The only way I can think of is creating new form that looks like a MessageBox, and using it instead.
try this
if(MessageBox.Show("something")==DialogResult.OK)
{
form1.Focus();
}
Or
if(MessageBox.Show("something")==DialogResult.OK)
{
form1.Select();
}
Are you using ShowDialog() method or Show() method to show your form2?
If you are using ShowDialog() method, modify it as Show().
Because ShowDialog() method will not allow you to change the focus to main form (form1), until you close the sub form (form2)
Make sure you are using method,
form2.Show()
to Display form2.

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

Categories