[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 ?
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.
Re: [2005] How to avoid opening duplicate MDI child form ?
Where do i find the Singlton patter and how do i implement it, Sir ?
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.
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:
Public Class SingletonForm
Private Shared newcust As SingletonForm
Public Shared ReadOnly Property Instance() As SingletonForm
Get
If newcust Is Nothing OrElse newcust.IsDisposed Then
newcust = New SingletonForm
End If
Return newcust
End Get
End Property
Private Sub New()
InitializeComponent()
End Sub
End Class
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
SingletonForm.Instance.MdiParent = Me
SingletonForm.Instance.Show()
SingletonForm.Instance.Activate()
'Dim childform As New newCust
'childform.MdiParent = Me
'childform.Show()
End Sub
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.
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.
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 ?
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.
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
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:
Dim childform As newCust
If MenuStrip1.MdiWindowListItem.IsMdiWindowListEntry.Equals(newCust) = True Then
newCust.Activate()
Else
childform = New newCust
childform.MdiParent = Me
childform.Show()
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.
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.
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:
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Customer.MdiParent = Me
If Customer IsNot Nothing Then
Customer.Show()
ElseIf Customer IsNot ActiveMdiChild Then
Customer.BringToFront()
End If
End Sub
Please suggest what's wrong.
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)
Re: [2005] How to avoid opening duplicate MDI child form ?
Quote:
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
Re: [2005] How to avoid opening duplicate MDI child form ?
Thanks.. That worked great!
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! :)