Solved

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

Posted on 2012-03-20
17
233 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
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 85

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
 

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 85

Accepted Solution

by:
Mike Tomlinson earned 500 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 85

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 85

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 85

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 85

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now