Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prevent multiple instances of MdiChild form? 1

Status
Not open for further replies.

ashishraj14

Programmer
Mar 1, 2005
92
AU
How can I prevent multiple instances of MdiChild form? I have a MdiParent form with a DataGrid showing all the clients in the database. User clicks on a row to open MdiChild form which display details of the client. User can have multiple clients open at the same time, but how can I prevent them from opening multiple instances of the client?
 
On the parent form, create an instance of the child

Public myChild as new Form1

On the child for create a sub which you can use to set the value

Public Sub setTheValue as integer
'Code to populate dataset here
End sub

Now on the parent form, you can put code similar to the following on the datagrid click event

myChild.setTheValue(AnyValueHere)
myChild.Show()


 
Here's what I would recommend:

First, you need a way to identify the clients that are open. The easiest way is with a public readonly property on your client form:

Code:
  Private m_ClientID As String

  Public ReadOnly Property ClientID()
    Get
      Return m_ClientID
    End Get
  End Property

Next, to populate that ClientID we can use the constructor:
Code:
  Public Sub New(ByVal Client_ID)
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    m_ClientID = Client_ID
  End Sub

Then on the MDI Parent form, we add the code to load a new window. This code takes the Client ID as a parameter. It then looks through all of the MDI Children for that value and brings that window to the front if it's open. If it's not found, a new window is opened.
Code:
Public Sub LoadNewWindow(ByVal Client_ID As String)
    Dim frmClient As New Form4("")
    Dim bFound As Boolean = False
    For Each frmClient In Me.MdiChildren()
      If frmClient.ClientID = Client_ID Then
        frmClient.Select()
        bFound = True
        Exit For
      End If
    Next

    If Not bFound Then
      frmClient = New Form4(Client_ID)
      frmClient.MdiParent = Me
      frmClient.Show()
    End If
  End Sub

Then from your client list form you can use this code to open a new form or highlight the existing one:
Code:
CType(Me.MDIParent,Form5).LoadNewWindow("1")

You'll have to go through that code and replace the Form4 (Client window class) and Form5(MDI Parent window class). But other then that, it's tested and works.

-Rick

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Rick has the right idea. This topic has been covered a few times before, so do a search in this forum to get more ideas.
 
You can also create a singleton to control your form.

A singleton keeps a private reference to itself, and you gain access to this reference via a shared property (usually called Instance).

So whenever want to create an instance of your form, you access the shared Instance property, which behind the scenes creates an instance (if there wasn't already one), or gives you a reference to an existing one (if this is a subsequent call).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I've done it this way:

Code:
Friend WithEvents childForm as MyChildForm

Private Sub MenuItem1_Click( _
                        ByVal sender As Object, _
                        ByVal e As System.EventArgs) Handles MenuItem1.Click

        'If the childForm is disposed or nothing then initialise a new form
        If Me.childForm Is Nothing OrElse Me.childForm.IsDisposed Then
            Me.childForm = New MyChildForm
        End If

        'Open the form
        Me.OpenChildForm(Me.childForm)

End Sub 

Private Sub OpenChildForm(ByVal childForm As MyChildForm)

        'Set the parent for the child form
        childForm.MdiParent = Me

        'If the childForm is isn't visible then show it otherwise make
        'it visible
        If childForm.Visible = True Then
            childForm.WindowState = FormWindowState.Normal
        Else
            childForm.Show()
        End If

End Sub

It is fired from a menu on an MDI form (MenuItem1).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top