Results 1 to 6 of 6

Thread: [RESOLVED] Unloading a form from popupmenu that is on another form.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Resolved [RESOLVED] Unloading a form from popupmenu that is on another form.

    There are two forms. The first one, let's call it form1 is a simple form. The second one, form2, is a hidden one with a menu on it.
    When you right-click on the form1, it calls a popupmenu from form2
    In the popupmenu that is located in form2, there is a menu "Unload this form".
    There also is a module with code:
    Code:
    Public formFrom as string
    The code for showing up the popupmenu when you right click in the form1 is like this:
    Code:
    Private Sub Form1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = 2 Then
    Me.PopupMenu form2.menu
    formFrom =  me.name
    Else
    End If
    End Sub
    The code in the "Unload" menu on the form2 is like this:
    Code:
    Private Sub unload_Click()
    Unload formFrom
    End Sub
    This doesn't work. How can I solve it? Thanks in advance.

  2. #2
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Unloading a form from popupmenu that is on another form.

    Try this, you could add this to a module to make it public.
    Code:
    Public formFrom As Form
    But I suggest you do it like this (or similar)

    Form1 Code
    Code:
    Private Sub Form1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = 2 Then
    Me.PopupMenu form2.menu
    Form2.SetFormName = Me
    Else
    End If
    End Sub
    Note: What i have highlighted in red doesn't look right, I have not played with this but I would not be surprise if it does not work.

    Form2 Code
    Code:
    Private FormName As Form
    
    Private Sub Unload_Click()
      Unload FormName
    End Sub
    
    Public Property Let SetFormName(Form_Name As Form)
      Set FormName = Form_Name
    End Property

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Unloading a form from popupmenu that is on another form.

    In addition to dimming as form you would need to use a set statement to assign the form
    Code:
    Set formFrom =  me

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Unloading a form from popupmenu that is on another form.

    Why not exploit the ActiveForm property of the Screen object instead of dimensioning a public variable? Sort of like this:

    Code:
    'In Form1
    
    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbRightButton Then PopupMenu Form2.menu
    End Sub
    Note that there's no 1 in Form_MouseUp.

    Code:
    'In Form2
    
    Private Sub unload_Click()
        Unload Screen.ActiveForm
    End Sub
    BTW, unloading Forms this way isn't the usual way it's done. Perhaps your program needs to be redesigned?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Unloading a form from popupmenu that is on another form.

    Try this, you could add this to a module to make it public.
    Code:
    Public formFrom As Form
    But I suggest you do it like this (or similar)

    Form1 Code
    Code:
    Private Sub Form1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = 2 Then
    Me.PopupMenu form2.menu
    Form2.SetFormName = Me
    Else
    End If
    End Sub
    Note: What i have highlighted in red doesn't look right, I have not played with this but I would not be surprise if it does not work.

    Form2 Code
    Code:
    Private FormName As Form

    Private Sub Unload_Click()
    Unload FormName
    End Sub

    Public Property Let SetFormName(Form_Name As Form)
    Set FormName = Form_Name
    End Property
    This method didn't work. It kept showing the same error that showed with my code "runtime error 91 object variable or with block not set"

    Why not exploit the ActiveForm property of the Screen object instead of dimensioning a public variable? Sort of like this:

    Code:
    'In Form1

    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then PopupMenu Form2.menu
    End Sub
    Note that there's no 1 in Form_MouseUp.

    Code:
    'In Form2

    Private Sub unload_Click()
    Unload Screen.ActiveForm
    End Sub
    BTW, unloading Forms this way isn't the usual way it's done. Perhaps your program needs to be redesigned?
    Thank you so much. It works. And it didn't create any other problem, because I am using multiple copies of the same form. And no, I can't redesign it because I have already redesigned it a lot of times, but every time a problem would pop up. And this time I decided to just face the problem and solve it.

  6. #6
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: [RESOLVED] Unloading a form from popupmenu that is on another form.

    I'm glad its working now. I just want to add, it won't work if you only copy and past sometimes! My code did work I normally always try my code before posting and it was working 100% no errors. Bonnie's way is more simple to copy and paste I agree
    But still if you can't get a form to close as you need, please study people's code before copy paste and thinking that it will always fit perfect into your code. You will never go foward in life if you copy and paste. Just my opinion.

Tags for this Thread

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