?
Solved

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

Posted on 2012-03-20
17
Medium Priority
?
276 Views
Last Modified: 2012-03-22
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
Comment
Question by:developer2012
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
17 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37743909
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
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 37744063
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37744181
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:developer2012
ID: 37744248
@ 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
 

Author Comment

by:developer2012
ID: 37744253
Thanks Code Cruiser & Uni Field I will try your code.
0
 

Author Comment

by:developer2012
ID: 37744281
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1500 total points
ID: 37744398
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
 

Author Comment

by:developer2012
ID: 37744567
@ Idle_Mind I tried your code. But It does not work :(. It still shows the last window.

Thanks.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37744579
Can you show your code please?...    (simple test project worked for me)
0
 

Author Comment

by:developer2012
ID: 37744601
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37744647
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
 

Author Comment

by:developer2012
ID: 37744986
@ 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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37745089
Show me your new code...   =)
0
 

Author Comment

by:developer2012
ID: 37745103
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37745115
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
 

Author Comment

by:developer2012
ID: 37745151
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
 

Author Comment

by:developer2012
ID: 37745156
Sorry LastWindow.Close()
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question