• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

How to allow the user to open only one window at a time?

Hello there,

I am creating one vb.net application and it has menus.(Like any other notepad window file, open). And, When the user clicks on it a new windows appears.

But, I want to allow the user to open only one window at a time. not multiple windows.

How to achieve this?

Thanks.
0
developer2012
Asked:
developer2012
1 Solution
 
CodeCruiserCommented:
Use following in the code where you open forms


For Each frm as form in my.Application.OpenForms
   if typeof(frm) is frmCustomers Then
      frm.Activate
      Exit Sub
      Exit For
   End If
Next

Dim f As New frmCustomers
f.show()
0
 
UnifiedISCommented:
Another way would be to open the form as showdialog

dim frm as new frmCustomers
frm.showdialog
frm.dispose

This make the form modal until the user closes it.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Are you working with an MDI setup?...or regular forms?

"one window at a time"

One of each type?...or only one at any given time?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
developer2012Author Commented:
@ Idle_Mind.

IT is not MDI Set up. Those are regular windows.(In WPF).

one window at a time Explanation:  I have a menu of Administrative Functions, user functions like any other window.

In my application when I click on admin functions like add user, delete user. The previous opened window stays on the page.  I don't want it.

IF i click on other tab i want it to disappear like any other note pad window.

Thanks.
0
 
developer2012Author Commented:
Thanks Code Cruiser & Uni Field I will try your code.
0
 
developer2012Author Commented:
Sorry, I forgot to mention those are menu items. If i click on  new menu item then I want the previous one to disappear.

Thanks for your help.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could do something like this:
Class MainWindow 

    Private WithEvents LastWindow As Window = Nothing

    Private Sub MenuItem1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MenuItem1.Click
        If Not IsNothing(LastWindow) Then
            If TypeOf LastWindow Is Window1 Then
                LastWindow.Focus()
            Else
                LastWindow.Close()
                LastWindow = Nothing
            End If
        End If

        If IsNothing(LastWindow) Then
            LastWindow = New Window1
            LastWindow.Show()
        End If
    End Sub

    Private Sub MenuItem2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MenuItem2.Click
        If Not IsNothing(LastWindow) Then
            If TypeOf LastWindow Is Window2 Then
                LastWindow.Focus()
            Else
                LastWindow.Close()
                LastWindow = Nothing
            End If
        End If

        If IsNothing(LastWindow) Then
            LastWindow = New Window2
            LastWindow.Show()
        End If
    End Sub

    Private Sub MenuItem3_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MenuItem3.Click
        If Not IsNothing(LastWindow) Then
            If TypeOf LastWindow Is Window3 Then
                LastWindow.Focus()
            Else
                LastWindow.Close()
                LastWindow = Nothing
            End If
        End If

        If IsNothing(LastWindow) Then
            LastWindow = New Window3
            LastWindow.Show()
        End If

    End Sub

    Private Sub LastWindow_Closed(sender As Object, e As System.EventArgs) Handles LastWindow.Closed
        LastWindow = Nothing
    End Sub

End Class

Open in new window

0
 
developer2012Author Commented:
@ Idle_Mind I tried your code. But It does not work :(. It still shows the last window.

Thanks.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can you show your code please?...    (simple test project worked for me)
0
 
developer2012Author Commented:
Sure!!


 Private Sub TerrViewMenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim DispView1 As Window = Nothing
        Dim d1 As New DispView1
        If Not IsNothing(DispView1) Then
            If TypeOf DispView1 Is Window Then
                DispView1.Focus()
            Else
                DispView1.Close()
                'DispView1 = Nothing
            End If
        End If

        If IsNothing(DispView1) Then
            d1.Show()
        End If
End Sub

P.S: I changed the LastWindow with DispView1.

Thanks for your help
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The variable that holds the "last window" needs to be at CLASS level, not a local variable within the sub.  Don't give a variable the same name as the type of that variable (don't use "DispView1" since that is the name of the window itself).

Also, this line:

    If TypeOf DispView1 Is Window Then

Should be:

    If TypeOf LastWindow Is DispView1 Then

This is checking to see if the current window is of the same type that we are going to open.  If so, then we simply give focus to it instead of creating another instance.

So it would look more like this:
Class MainWindow 

    Private WithEvents LastWindow As Window = Nothing

    Private Sub TerrViewMenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If Not IsNothing(LastWindow) Then
            If TypeOf LastWindow Is DispView1 Then
                LastWindow.Focus()
            Else
                LastWindow.Close()
                LastWindow = Nothing
            End If
        End If

        If IsNothing(LastWindow) Then
            LastWindow = New DispView1
            LastWindow.Show()
        End If
    End Sub

End Class

Open in new window

0
 
developer2012Author Commented:
@ Idle_Mind I did completely what you said Sir!

But I think I am having a bad day. It never worked for me :(
 
Thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Show me your new code...   =)
0
 
developer2012Author Commented:
Here it is Sir!!

 'If Not IsNothing(LastWindow) Then
        '    If TypeOf LastWindow Is DispView1 Then
        '        LastWindow.Focus()
        '    Else
        '        LastWindow.Close()
        '        LastWindow = Nothing
        '    End If
        'End If

        'If IsNothing(LastWindow) Then
        '    LastWindow = New DispView1
        '    LastWindow.Show()
        'End If
And, I declared the global variable.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Then either you have other code interfering, or we just don't have a clear enough picture of your setup to understand the issue.
0
 
developer2012Author Commented:
Well thanks Idle_Mind for your help.  Issue is resolved.

I declared all the windows on the public level and put the code

Close.Previous()

It works fine. (Though this is a little cheating).

Thanks
0
 
developer2012Author Commented:
Sorry LastWindow.Close()
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now