Results 1 to 9 of 9

Thread: restricting multiple instances of child form?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    restricting multiple instances of child form?

    I am using an MDI form with menu items.

    I am using the following code to open a form when appropriate menu item is chosen. But I want to make sure if the user clicks on the same menu item multiple times, only one instance of the MDI child should be shown rather than mulitiple instance of the same (MDI child) form.

    Dim NewMDIChild As New frmRateType
    NewMDIChild.MdiParent = Me
    NewMDIChild.StartPosition = FormStartPosition.CenterScreen
    NewMDIChild.Show()

    How can I restrict?

    thanks
    nath

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Have a shared variable that determines if the form has already been instantiated (maybe a boolean or an integer if you want more than 1 but less than x instances).

    In the constructor of the childform, query this variable and throw an exception if you already have enough instances.

    Catch the exception in the calling sub.
    I don't live here any more.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    is there any alternate way to do this?

    is there any alternate way to do this?

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    I have not yet tested this but see if the following helps:

    In the Parent form load event
    Public NewMDIChild as Form

    Then in the Menu

    NewMDIChild=Nothing
    NewMDIChild = New frmRateType
    NewMDIChild.MdiParent = Me
    NewMDIChild.StartPosition = FormStartPosition.CenterScreen
    NewMDIChild.Show()
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    doesn't work.

    thanks though.

    nath

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Sorry, I should have said put the following in the General section of the Parent form:

    Public NewMDIChild as Form
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  7. #7
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    i agree with wossname. with that method, you can allow as many as you like if you ever feel the need later.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    What about wossname's method isn't suitable for your program? It's the first thing I could think of when reading your question.

  9. #9
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    Try this:

    VB Code:
    1. Dim f As frmRateType
    2. Dim fTest As Form
    3.  
    4. For Each fTest In MDIForm.MdiChildren
    5.     If TypeOf fTest Is frmRateType Then
    6.         f = DirectCast(fTest, frmRateType)
    7.         'f now holds a reference to the first instance of
    8.         'frmRateType found in the MdiChildren collection
    9.         Exit For
    10.     End If
    11. Next
    12.  
    13. If f Is Nothing Then
    14.     f = New frmRateType()
    15.     f.MdiParent = MDIForm
    16. End If
    17.  
    18. f.Show()
    19. f.Activate()

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