Results 1 to 17 of 17

Thread: [RESOLVED] [2005] How to avoid opening duplicate MDI child form ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Resolved [RESOLVED] [2005] How to avoid opening duplicate MDI child form ?

    How do i avoid opening duplicate MDI child form which is already open. I am using the following codes for opening MDI child form:

    Code:
    Private Sub EmployeeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeToolStripMenuItem.Click
            Dim childform As New Employee
            childform.MdiParent = Me
            childform.Show()
        End Sub
    What changes should be made ?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to avoid opening duplicate MDI child form ?

    I would suggest implementing the Singlton patter, so the form itself will prevent any more than one instance being created. There are numerous examples already posted.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: [2005] How to avoid opening duplicate MDI child form ?

    Where do i find the Singlton patter and how do i implement it, Sir ?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to avoid opening duplicate MDI child form ?

    Sorry for my poor typing. It's the Singleton pattern you want. Search the forum and you'll find the aforementioned examples.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: [2005] How to avoid opening duplicate MDI child form ?

    I have used the following code for Singleton... I cant figure out the problem.

    vb Code:
    1. Public Class SingletonForm
    2.         Private Shared newcust As SingletonForm
    3.  
    4.         Public Shared ReadOnly Property Instance() As SingletonForm
    5.             Get
    6.                 If newcust Is Nothing OrElse newcust.IsDisposed Then
    7.                     newcust = New SingletonForm
    8.                 End If
    9.                 Return newcust
    10.             End Get
    11.         End Property
    12.  
    13.         Private Sub New()
    14.  
    15.             InitializeComponent()
    16.  
    17.         End Sub
    18.     End Class
    19.  
    20.     Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
    21.  
    22.  
    23.         SingletonForm.Instance.MdiParent = Me
    24.         SingletonForm.Instance.Show()
    25.         SingletonForm.Instance.Activate()    
    26.  
    27.         'Dim childform As New newCust
    28.         'childform.MdiParent = Me  
    29.         'childform.Show()
    30.  
    31.     End Sub

  6. #6
    Addicted Member
    Join Date
    May 2007
    Posts
    165

    Re: [2005] How to avoid opening duplicate MDI child form ?

    Bit new to all this so I probably do things a wierd way but what I do is set global pointer to form class:-

    Public frmInfo As cfrmInfo

    Open form like this:-

    If frmInfo Is Nothing Then
    Dim info As New cfrmInfo : frmInfo = info
    End If

    And stick a frmInfo = nothing in the form closed event.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to avoid opening duplicate MDI child form ?

    Quote Originally Posted by LuxCoder
    I have used the following code for Singleton... I cant figure out the problem.[/HIGHLIGHT]
    You imply that there's a problem yet you don't give any information about it. Are you saying that that doesn't prevent more than one instance? Please be clear about what the issues are. Feel free to post more than one line in explanation.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: [2005] How to avoid opening duplicate MDI child form ?

    Sir, please help... my form name is newcust to which i want to apply singleton pattern. what changes should be made to my code in the first post ?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to avoid opening duplicate MDI child form ?

    If you ask for help and someone who is trying to help you asks a question, I suggest that you answer it. Chances are that they asked the question because they need more infromation in order to help you. I aksed you a question in my previous post.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: [2005] How to avoid opening duplicate MDI child form ?

    This is my suggest solution

    Code:
    Public Sub ChildFormLoading(ByVal ChildForm As Form, ByVal FormName As String)
            For Each f As Form In Me.MdiChildren ' Loop all childform
    
                If Not f.Name = FormName Then 
                    f.close() 'Close others form
    
                End If
            Next
            For Each f As Form In Me.MdiChildren  ' Scan all childform
    
                If f.Name = FormName Then 'if Same Name then exit sub function
                    Exit Sub
    
                End If
            Next
            ChildForm.MdiParent = Me
            ChildForm.Show()
        End Sub

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: [2005] How to avoid opening duplicate MDI child form ?

    Thanks nuflavors, but that is not what i exactly want. I want to check while opening a form, if that form is already open or not. If it is open then it should activate and if it is not then a new form in opened. i have tried the MDIwindowlist method where my code goes like this :

    vb Code:
    1. Dim childform As newCust
    2.         If MenuStrip1.MdiWindowListItem.IsMdiWindowListEntry.Equals(newCust) = True Then
    3.             newCust.Activate()
    4.         Else
    5.             childform = New newCust
    6.             childform.MdiParent = Me
    7.             childform.Show()
    8.         End If

    Here i want to check whether the MDIWindowList has newcust in it ? If it has then the already opened form (newcust) gets activated otherwise a new form (newcust) is opened. But i dont know whats wrong with my code... it's still opening the form (newcust) as many times i am clicking.

    I want to apply the singleton form pattern here (as suggested by Mr. JMC) but i do not know why my code is not working.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to avoid opening duplicate MDI child form ?

    The code in post #5 works as it's supposed and apparently you are determined not to answer my question. In that case I can't help you. If I have to post three times just to get some information then it just isn't worth it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: [2005] How to avoid opening duplicate MDI child form ?

    I am using the following code to prevent opening duplicate MDIchildform - Customer (which is working fine) but it doesn't bring the already opened MDIchildform - Customer, in case it is underlying behind some other MDIchildform.

    vb Code:
    1. Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
    2.         Customer.MdiParent = Me
    3.         If Customer IsNot Nothing Then
    4.             Customer.Show()
    5.         ElseIf Customer IsNot ActiveMdiChild Then
    6.             Customer.BringToFront()
    7.         End If
    8.     End Sub

    Please suggest what's wrong.

  14. #14
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] How to avoid opening duplicate MDI child form ?

    i use this function to load mdi children
    Code:
        Private Sub LoadMdiChild(ByVal f As Form)
            If Me.MdiChildren.Length > 0 Then
                For i As Integer = 0 To Me.MdiChildren.Length - 1
                    If Me.MdiChildren(i).Name = f.Name Then
                        Me.MdiChildren(i).BringToFront()
                        f.Dispose()
                        Exit Sub
                    End If
                Next
                f.MdiParent = Me
                f.Show()
            Else
                f.MdiParent = Me
                f.Show()
            End If
        End Sub
    To call it I simply call
    Code:
    LoadMdiChild(New AdminForm)
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  15. #15
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] How to avoid opening duplicate MDI child form ?

    Private Sub EmployeeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeToolStripMenuItem.Click
    Dim childform As New Employee
    childform.MdiParent = Me
    childform.Show()
    End Sub
    Just do this, let's say it's your frmEmployee
    Code:
    frmEmployee.Show()
    frmEmployee.Focus()
    frmEmployee.MdiParent = Me
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: [2005] How to avoid opening duplicate MDI child form ?

    Thanks.. That worked great!

  17. #17
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] How to avoid opening duplicate MDI child form ?

    Quote Originally Posted by LuxCoder
    Thanks.. That worked great!
    Please mark your thread RESOLVED if sloved!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width