Solved

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

Posted on 2012-03-20
17
266 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 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
 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

697 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