csharp winform modal window, able to click on main window - c#

language c#, winform
how do i create a modal window or such a thing that when it still showing
i would still be able to click or interact with the main window..
thanks.
put some code please or links..

Make the dialog non-modal (use Show instead of ShowDialog), and make it top-most (TopMost = true)

Just use the overload of Form.Show() that takes a form as a parameter, like this:
Form f = new Form();
f.Show(this);
This will keep the form always on top of the form that calls it, but still let you click and access the calling form.

Some confusion here I think;
Modal is when the window blocks the underlying window, and must be closed to enable the underlying window to regain control. Form.ShowDialog(owner) is used to accomplish this.
Non-Modal is a window that is opened "in parallell" to the underlying window. Both windows can be accessed and respond to mouse and key events. Form.Show(owner) to accomplish this.

Modality by definition means that you are not able to click anywhere else. You can create another form and show it with Show() method.

Show() Method allows you to click anywhere while ShowDialog() won't

Related

Focus lost when IPleaseWaitService is called before a modal dialog using Catel

In summary, I do have a modal dialog in Catel, invoked with:
_uiVisualizerService.ShowDialog(viewModel)
Inside that dialog, I do a long process showing a Wait Service:
_pleasewaitservice.Show();
// HARD WORK here
_pleasewaitservice.Hide();
And Then I invoke another modal dialog.
_uiVisualizerService.ShowDialog(configureViewModel)
However, when I click outside of the application while it is doing the hard work (when the pleasewaitservice is shown), the second modal dialog is displayed behind the main application, so I cannot focus the Window because it is behind and it is modal. I have to close the app from the task killer.
After checking it carefully, I realized that origin is the pleaseWaitService. If I don't show it, the second modal dialog is always displayed correctly.
Does anyone have any hint about how to solve it?
I was googling about how to force to set focus in any Window, but I didn't find anything.
Thanks
Regards
Saul Hidalgo.
You might want to try the BringWindowToTop extension method inside the code-behind of the window:
https://github.com/Catel/Catel/blob/46fcc69575e533eb9e02669ebaa2246894dc98d8/src/Catel.MVVM/Catel.MVVM.Shared/Windows/Extensions/WindowExtensions.cs#L237

How to disable going from current form to others

I'd like to disable going to other - same project - forms when I am working in a specific form.
I have seen that in some Windows application, when you click on other form it play a sound and nothing happens.
I have done this but didn't work :
public form2()
{
form1.Enable = false;
}
You're looking for a modal form which is a window that needs to be closed before you can use the other forms. Use the Form.ShowDialog method when you want to create a modal window otherwise use Form.Show.
(More information on Modal and Modeless windows here on MSDN).
In your case, you'd open Form2 like this:
form2.ShowDialog();
yourForm.ShowDialog(); will do the trick!
Documentation

How do I make a custom modal popup window in WPF and have it return a value?

I'm taking a whack at WPF and trying to learn as I go. I'd appreciate any advice offered.
I've got a Window that has a Page attached to it (through a Frame on the Window). When you press a button on the Page, I want a custom window to pop up to present several custom options and be displayed in a manner of my choosing (I'm thinking right now I want it to be a grid but that may change as I go on). When selected, the modal window will disappear and return to the calling method (button press from the Page) the value of the selected choice.
I don't want the standard windows dialog box with the options of yes, no, okay, cancel, or anything like that. This is truly just a custom popup that returns a value to the caller when the user makes their selection on the popup.
Create a new Window subclass, which you can layout however you like. Then in your button click event handler, display it modally using myModalWindow.ShowDialog();. You can then have a property on the window class which you can access after it closes in order to access result data, i.e.:
myModalWindow.ShowDialog();
var data = myModalWindow.SomeResultProperty;
If you really want to have something returned from a method, I suppose you could create your own public method on your window class which internally calls ShowDialog() and then returns a value.

Showing a form via Form.Show is not given focus?

I two forms, Form1, and a UserControl which hosts Form2. Within that UserControl on Form1 I call Form2.Show();. I have also tried Form2.Show(this);. Either way, the focus is not given to the form. I have to click once within the new form to give it focus, and then I can click items within that form.
I figured that control is passing back to my main control/form, and thus the focus is getting lost. So I am waiting until Form2 is closed via:
while (form2.Visible == true)
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
This seems to work. However after I close the form, now the reverse holds true. Form1 is not given focus (even if I call this.Focus()) until I click once within the main form window.
Any ideas how to handle this properly. I want to show a child form (modeless) and immediatley be able to click on it, and when that form is closed, immediately be able to take action back on the parent form.
You should probably use .ShowDialog(), this can also be extended to give your response on if the user performed Form2's operation properly or aborted early.
This does make the form locked in focus up front and will halt your code execution on the first form till that form is closed.
use this.Activate(); in place of this.Focus();
Not sure I follow completely, but from your UC try opening Form2 like this:
form2.Show(Parent);
This should specify the UC's Parent form as form2's owner.
This came from the fact that I was overriding WndProc in order to display the form. When I received the CBN_DROPDOWN message, I would display the form. I fixed this by instead Invoke'ing the method that shows the form and it fixed it.
case CBN_DROPDOWN:
Invoke(new MethodInvoker(Show_DropDown));
return;

What's the difference between Application.Run() and Form.ShowDialog()?

In my application I want to show a login form first and then the main form if the login has been successful. Currently I'm doing it something like this:
var A = new LoginForm();
if ( A.ShowDialog() == DialogResult.OK )
Application.Run(new MainForm());
But then I started wondering - what's the point of the Application.Run()? Why not just do (new MainForm()).ShowDialog() as well? What's the difference? And what would be the correct way to achieve what I want?
Application.Run(Form) starts a message loop on the current thread and displays the specified form. The message loop enables the form to receive Windows messages (eg, key presses, mouse clicks, paint invalidations) to allow it to appear responsive and have interaction with the user. When you call ShowDialog() on a Form instance, it actually does a similar thing and creates a modal message loop for the form on which ShowDialog has been called.
There is not much difference between the two calls. Application.Run does add some extra event handling enabling you to do some tidying up of resources when the main form is closed (see Application.ThreadExit).
The recommended way to start WinForms applications is using Application.Run, but I suspect this is more of a convention than a rule. The biggest reason to use Application.Run is if you want to open multiple non-modal forms. You can do this using:
new Form().Show();
new Form().Show();
Application.Run();
You could not achieve this using the ShowDialog() method as one of the forms would have to be modal.
As for your question of how to show a login form and then the main form if the login is successful, I think what you have is fine:
if (new LoginForm().ShowDialog() == DialogResult.OK)
{
Application.Run(new MainForm());
}
The alternative is to do the plumbing yourself and open an instance of MainForm in the closing event of the LoginForm if the login was successful.
From MSDN:
This method adds an event handler to
the mainForm parameter for the Closed
event. The event handler calls
ExitThread to clean up the
application.
http://msdn.microsoft.com/en-us/library/ms157902.aspx
From my testing, I noticed this main difference:
When Application.Run is used, the form's Close button (red X) returns DialogResult.None; however, when ShowDialog is used, the Close button produces DialogResult.Cancel.
Does this matter to you? In my code, I was testing for DialogResult.Cancel to determine the exit code of my application. That was broken when the red X was used to close the form. I now test for DialogResult.OK to indicate a successful exit.
return myForm.DialogResult == DialogResult.OK ? 0 : 1;
One key difference is that ShowDialog is usually a modal Dialog. If you wanted to create a user-friendly toolset, you would not want it to be comprised of modal dialog boxes.
Also, Application.Run() accepts more than just a form. It has a few overloads.
As for your application, I do not think it matters much. Application.Run makes sense to me because it denotes the start of your actual Application.
The documentation of the overload
public static void Run(
ApplicationContext context );
has a neat example with a different approach that involves two forms as well.
For a more concerete example of a difference:
If your main form is an MDI form, then the behavior on clicking the close button (the 'x' in the upper right, or Alt-F4) is different depending on which method you use to show the form.
With Application.Run(mainForm), the closing event of the child forms run, then the main form's closing event runs.
With mainForm.ShowDialog, the closing event of the main form runs, and the closing event of the child forms do not run.
Application.Run() is for the start of application while MainForm is part of the application and MainForm()).ShowDialog() used to display it only.
Application.Run() is the entry point for your Application. same as Main() method is for some class or ApplicationStart() for a WebApplication
Application.Run() has different overloads, one of which is without parameters. That Method starts application without an initial form.
From my testing I notice that using Application.Run buttons with DialogResult does not close the form (OnFormClosing is not hit) compare to ShowDialog in which the buttons with DialogResult hit OnFormClosing and the close the form.

Categories