|
-
Jul 22nd, 2007, 07:52 AM
#1
Thread Starter
Hyperactive Member
[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 ?
-
Jul 22nd, 2007, 10:03 AM
#2
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.
-
Jul 23rd, 2007, 12:57 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] How to avoid opening duplicate MDI child form ?
Where do i find the Singlton patter and how do i implement it, Sir ?
-
Jul 23rd, 2007, 01:28 AM
#4
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.
-
Jul 23rd, 2007, 03:58 AM
#5
Thread Starter
Hyperactive Member
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
-
Jul 23rd, 2007, 04:27 AM
#6
Addicted Member
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.
-
Jul 23rd, 2007, 04:34 AM
#7
Re: [2005] How to avoid opening duplicate MDI child form ?
 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.
-
Jul 26th, 2007, 02:23 AM
#8
Thread Starter
Hyperactive Member
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 ?
-
Jul 26th, 2007, 02:26 AM
#9
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.
-
Jul 26th, 2007, 03:20 AM
#10
Hyperactive Member
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
-
Jul 26th, 2007, 06:18 AM
#11
Thread Starter
Hyperactive Member
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.
-
Jul 26th, 2007, 06:43 AM
#12
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.
-
Sep 5th, 2007, 09:03 AM
#13
Thread Starter
Hyperactive Member
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.
-
Sep 5th, 2007, 09:35 AM
#14
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)
-
Sep 5th, 2007, 11:24 AM
#15
Fanatic Member
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.
-
Sep 6th, 2007, 01:19 AM
#16
Thread Starter
Hyperactive Member
Re: [2005] How to avoid opening duplicate MDI child form ?
Thanks.. That worked great!
-
Sep 6th, 2007, 01:40 AM
#17
Re: [2005] How to avoid opening duplicate MDI child form ?
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|