WPF vb.net ShowModelessWindow prevent multiple instances

WPF vb.net ShowModelessWindow prevent multiple instances

budinsky
Contributor Contributor
1,143 Views
3 Replies
Message 1 of 4

WPF vb.net ShowModelessWindow prevent multiple instances

budinsky
Contributor
Contributor

I would like to have a MainWindow open as modeless so it can stay open, but calling the command again results in multiple modeless windows. How can I check if the MainWindow is already open to prevent multiples?

 

<CommandMethod("BatchProcess")> Public Sub cmdBatchProcess()
  Dim winBatPro As New MainWindow
  Application.ShowModelessWindow(winBatPro)
End Sub
0 Likes
Accepted solutions (1)
1,144 Views
3 Replies
Replies (3)
Message 2 of 4

jabowabo
Mentor
Mentor

Try this (you'll have to translate to VB):

            // check if form is already open
            Form_MyForm myForm;
            bool isFormOpen = false;
            FormCollection fc = System.Windows.Forms.Application.OpenForms;
            foreach (Form frm in fc)
            {
                if (frm.Name == "Form_MyForm")
                {
                    isFormOpen = true;
                    if (frm.WindowState == FormWindowState.Minimized)
                        frm.WindowState = FormWindowState.Normal;

                    myForm = (Form_MyForm)frm;
                    break;
                }
            }

            if (!isFormOpen)
            {
                myForm = new Form_MyForm();
                myForm.Show();
            }

  

0 Likes
Message 3 of 4

norman.yuan
Mentor
Mentor
Accepted solution

Firstly, you need to be careful to use modeless form/window: in most cases one only use it for showing information in application context; if the information shown on the form/window is specific document (for example, the active drawing in AutoCAD) related, because user can switch to another document while this form/window shows. In this case, you need to either refresh the content on the form with newly activated document, or you need to have the form/window closed automatically.

 

In general, for modeless form/window/paletteset, only a static/Shared instance is created, something like:

 

<CommandClass> _

Public MyCommandClass

  Private Shared _floatWindow As MainWindow=Nothing

  <CommandMethod("DoSomthing", CommandFlags.Session>

  Public Shared Sub DoSomethingWithFloatForm()

    If _floatWindow Is Nothing Then

        _floatWindow=New MainWindow

    End If

    Application.ShowModelessWindow(_floatWidnow)

  End Sub

End Class

 

You also need to handle the window's Closing event, where you cancel the closing, and set the window's Visible/Visibility property to False/Hide, so that when user clicks the "x" on the window, it is not closed, but becomes hidden instead.

 

You can also use multiple floating forms/windows with each related to specific open drawing. This article might be helpful on this regard:

 

https://drive-cad-with-code.blogspot.com/2014/02/showing-and-closing-modeless-formdialog.html

 

HTH

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 4

budinsky
Contributor
Contributor

Thanks so much! I appreciate both of your replies. You guys rock!

 

MainWindow is not drawing specific so no need to refresh anything.

Had not considered minimising the window, now it keeps my data - very awesome!

I'm still learning so this took me all night to work out but I'm better for it.

0 Likes