• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

VB.NET sub re-execution for Open process.....

Experts,

I have the following code which works great.  I can open forms and re-open them again after they are closed...
        frmMain.m_bFlagPanelSettings = False
        Call frmMain.subUpdateProjectSettings()
        'Setup/Initialize Form(s)
        Select Case My.Settings.sPubFormAction
            Case "Edit"
                'Form: DataGridViewDesigns Editor
                If IsNothing(frmEditr) OrElse frmEditr.IsDisposed Then
                    frmEditr = New frmDataGridViewDesigns_Editor
                End If
                frmEditr.TopLevel = False
                frmEditr.MdiParent = frmMain
                frmEditr.Top = 50
                frmEditr.Left = 50
                frmEditr.WindowState = FormWindowState.Normal
                'Form: DataGridViewDesigns Catalog
                If IsNothing(frmCatlg) OrElse frmCatlg.IsDisposed Then
                    frmCatlg = New frmDataGridViewDesigns_Catalog
                End If
                frmCatlg.TopLevel = False
                frmCatlg.MdiParent = frmMain
                frmCatlg.WindowState = FormWindowState.Minimized
                frmCatlg.Anchor = AnchorStyles.Bottom
                'Show Form(s)
                frmEditr.Show()
                frmCatlg.Show()
            Case "Select"
                'Form: DataGridViewDesigns Catalog
                If IsNothing(frmCatlg) OrElse frmCatlg.IsDisposed Then
                    frmCatlg = New frmDataGridViewDesigns_Catalog
                End If
                frmCatlg.TopLevel = False
                frmCatlg.MdiParent = frmMain
                frmCatlg.Top = 50
                frmCatlg.Left = 50
                frmCatlg.WindowState = FormWindowState.Normal
                'Show Form(s)
                frmCatlg.Show()
        End Select

Open in new window


The My.Settings.sPubFormAction string variable can hold either "Edit", "Select" or "Reload".

Edit         Means that both the Editor and Catalog forms are displayed
Select      Means that the Catalog form is displayed
Reload    Means that both the Editor and Catalog forms are displayed again

In the application on a  Designs menu I have "New" and "Open".

"New" assigns a value of "Edit" to the My.Settings.sPubFormAction and opens both the Editor and Catalog forms.  Once the user closes the Editor the Catalog form closes and they go about their business doing something else in the application.....

"Open" assigns a value of "Select" to the My.Settings.sPubFormAction and opens only the Catalog form.  The user selects the desired entry and then clicks the "Select" button.  The form should close and the My.Settings.sPubFormAction variable is now set to "Reload".  The Editor and Catalog forms are re-opened with the desired entry highlighted for editing.  The Catalog is displayed which shows all entries with the specific one highlighted as well.

So to be able to perform the above actions would I have a separate sub that handles the processing and checks the value of My.Settings.sPubFormAction?

I want to use Show instead of ShowDialog because the user may be doing something else as well.  Or put it another way I want to give as much flexability as possible in the application.
0
Peter Allen
Asked:
Peter Allen
  • 4
  • 2
  • 2
5 Solutions
 
CodeCruiserCommented:
I would create a sub which I would call when opening forms so it performs all the relevant logic and loads correct forms. I would also have another sub which handlers the form close event of those forms and updates the setting accordingly.
0
 
Peter AllenAuthor Commented:
CodeCruiser,

The code you see currently is in its own sub, subSetupReferenceObjects.  I was thinking of creating a sub called subFormsProcessing.  This sub would call subSetupReferenceObjects and handle checking for the value of My.Settings.sPubFormAction.  If this variable returned "Reload" then the sub would set the variable to "Edit" and call subSetupReferenceObjects again.  Something like

sub subFormsProcessing
       call subSetupReferenceObjects
       .....
       .....
       if My.Settings
End Sub

My concern is if I want to open the forms using .Show() this allows the code to be continually executed.  So how would I use the above sub in conjunction with the closing events of the forms to re-execute the sub subSetupReferenceObjects?

I can set the My.Settings.sPubFormAction variable to "Reload" or whatever I want but will  I need a loop?

Can you give me an example of how this would work?
0
 
CodeCruiserCommented:
You would need to subscribe to close events of your forms when creating these forms

Dim f as new Form1
AddHandler f.FormClosing, Addressof Form_Closing

where Form_Closing is a sub in your module etc

Private Sub Form_Closing(sender as Object, e as FormClosingEventArgs)

In this sub, you can reset your setting.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Peter AllenAuthor Commented:
CodeCruiser,

I do already have an event in a Button that changes the setting.  I am not quite clear why I would use AddHandler to the Catalog form.

Using a controling sub to handle the forms processing is a great point.  Here are the two subs and following is the list of actions that I want to occur.  In the process of testing I found that I could not use ShowDialog.  I tried it and got an error.

a) Forms Processing
    Public Shared Sub subFormsProcessing()

        Dim bLoop As Boolean = True
        Do While bLoop = True
            If My.Settings.sPubFormAction = "Edit" Or _
                   My.Settings.sPubFormAction = "Select" Then
                Call subSetupReferenceObjects()
            End If
            If My.Settings.sPubFormAction = "Reload" And _
               IsNothing(frmCatlg) OrElse frmCatlg.IsDisposed Then
                frmCatlg = New frmDataGridViewDesigns_Catalog
                My.Settings.sPubFormAction = "Edit"
                My.Settings.Save()
                bLoop = False
                Call subSetupReferenceObjects()
            End If
        Loop

    End Sub

Open in new window


b) Opened Form(s) sub
    Public Shared Sub subSetupReferenceObjects()

        frmMain.m_bFlagPanelSettings = False
        Call frmMain.subUpdateProjectSettings()
        'Setup/Initialize Form(s)
        Select Case My.Settings.sPubFormAction
            Case "Edit"
                'Form: DataGridViewDesigns Editor
                If IsNothing(frmEditr) OrElse frmEditr.IsDisposed Then
                    frmEditr = New frmDataGridViewDesigns_Editor
                End If
                frmEditr.TopLevel = False
                frmEditr.MdiParent = frmMain
                frmEditr.Top = 50
                frmEditr.Left = 50
                frmEditr.WindowState = FormWindowState.Normal
                'Form: DataGridViewDesigns Catalog
                If IsNothing(frmCatlg) OrElse frmCatlg.IsDisposed Then
                    frmCatlg = New frmDataGridViewDesigns_Catalog
                End If
                frmCatlg.TopLevel = False
                frmCatlg.MdiParent = frmMain
                frmCatlg.WindowState = FormWindowState.Minimized
                frmCatlg.Anchor = AnchorStyles.Bottom
                'Show Form(s)
                frmEditr.Show()
                frmCatlg.Show()
            Case "Select"
                'Form: DataGridViewDesigns Catalog
                If IsNothing(frmCatlg) OrElse frmCatlg.IsDisposed Then
                    frmCatlg = New frmDataGridViewDesigns_Catalog
                End If
                frmCatlg.TopLevel = False
                frmCatlg.MdiParent = frmMain
                frmCatlg.Top = 50
                frmCatlg.Left = 50
                frmCatlg.WindowState = FormWindowState.Normal
                'Show Form(s)
                frmCatlg.Show()
        End Select

    End Sub

Open in new window


If the user selects "New" both Editor and Catalog are shown with the My.Settings.sPubFormAction variable set to "Edit".  This is no problem what-so-ever.

If the user selects "Open" the Catalog is opened and a button is shows called "Select".  If the button is clicked the My.Settings.sPubFormAction variable is set to "Reload".  This should close the Catalog form and re-open both the Editor and Catalog with the desired entry selected.

The problem I am seeing is that with Show() background processing continues so when the Catalog form is shown for selecting the desired entry and is closed by the user clicking "Select" the form closes OK, but the Editor/Catalog forms are not re-opened as I want.

Using ShowDialog as indicated above gives me an error.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"The problem I am seeing is that with Show() background processing continues so when the Catalog form is shown for selecting the desired entry and is closed by the user clicking "Select" the form closes OK, but the Editor/Catalog forms are not re-opened as I want."

So you want something to occur when the Form is closed.  As CodeCruiser stated, you need to use AddHandler() to wire up the FormClosed() event.  This should be done in the same block that re-creates the form if necessary:

               If IsNothing(frmCatlg) OrElse frmCatlg.IsDisposed Then
                    frmCatlg = New frmDataGridViewDesigns_Catalog
                   AddHandler frmCatlg.FormClosed, AddressOf someMethodNameHere
               End If

"Using ShowDialog as indicated above gives me an error."

This is a common design issue.  You can't display an MdiChild with ShowDialog().  To use ShowDialog(), the form must be displayed normally, which means it won't be contained within the MdiParent.  If you absolutely must keep the "dialog" form as an MdiChild then you have to simulate the behavior.  This can be achieved by disabling all other elements and MdiChildren so that the "dialog" MdiChild is the only thing that can be interacted with.  When it is closed you re-enable everything else again.
0
 
Peter AllenAuthor Commented:
Idle_Mind,

On the line with AddHandler...

"AddHandler frmCatlg.FormClosed, AddressOf someMethodNameHere"

Where someMethodNameHere --- is this where I would reference the sub that I want to execute or some Event on the Form Catalog?

If this refers to an event on the Catalog form so I would have that event call the subFormProcessing code...

I am just re-confirming my understanding here.  I haven't used AddHandler before.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
someMethodNameHere() would be the method that is executed when frmCatlg is closed.  In this case, that method would exist in the same class/form that the code is currently running in (I'm not sure where that is...but it's NOT frmCatlg).

If that method were in frmCatlg, then someMethodNameHere would be preceded by "frmCatlg." like this:

    AddHandler frmCatlg.FormClosed, AddressOf frmCatlg.someMethodNameHere

So that method can be in the current form, or in frmCatlg...it's up to you.  I'm guessing that the current form is what you really want and would make coding it much easier!

Note that the someMethodNameHere() method should have the same signature specified for the FormClosed() event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed.aspx

So something like:

    Private Sub someMethodNameHere(sender as Object, e as FormClosedEventArgs)

One way to do this is to declare a temporary variable as "WithEvents" at the  top of the class:

    Public Class xxx

        Private WithEvents frmCatlg As Form

Now you can use the two dropdowns across the top of the code editor to create a stub method for the FormClosed() event.  Select frmCatlg in the left dropdown, and FormClosed in the right dropdown:

    Private Sub frmCatlg_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles frmCatlg.FormClosed

    End Sub

Note that it has "Handles frmCatlg.FormClosed" at the end of the line.  The "WithEvents" and "Handles" keywords work together to allow the handling of that event without ever using AddHandler().  This is how the IDE handles events for controls that you add to the Form (like TextBoxes, etc.).  When you add a control to the form a line declaring that controls as WithEvents is added to the hidden designer file.

Once that stub method has been created for you, delete the Handles clause at the end and remove the temporary WithEvents declaration line.  Now you've got a method with the correct parameter signature that can be used with your AddHandler() line.
0
 
Peter AllenAuthor Commented:
I did use portions of both Idle_Mind and CodeCruiser.  Thank you guys.  Here is the complete clode blocks:

Forms Processing:
    Public Shared Sub subFormsProcessing()

        Select Case My.Settings.sPubFormAction
            Case "Edit", "Select"
                Call subSetupReferenceObjects()
            Case "Reload"
                My.Settings.sPubFormAction = "Edit"
                My.Settings.Save()
                Call subSetupReferenceObjects()
        End Select

    End Sub

SetupReferenceObjects:
    Public Shared Sub subSetupReferenceObjects()

        frmMain.m_bFlagPanelSettings = False
        Call frmMain.subUpdateProjectSettings()
        'Setup/Initialize Form(s)
        Select Case My.Settings.sPubFormAction
            Case "Edit"
                'Form: DataGridViewDesigns Editor
                If IsNothing(frmEditr) OrElse frmEditr.IsDisposed Then
                    frmEditr = New frmDataGridViewDesigns_Editor
                End If
                frmEditr.TopLevel = False
                frmEditr.MdiParent = frmMain
                frmEditr.Top = 50
                frmEditr.Left = 50
                frmEditr.WindowState = FormWindowState.Normal
                'Form: DataGridViewDesigns Catalog
                If IsNothing(frmCatlg) OrElse frmCatlg.IsDisposed Then
                    frmCatlg = New frmDataGridViewDesigns_Catalog
                End If
                frmCatlg.TopLevel = False
                frmCatlg.MdiParent = frmMain
                frmCatlg.WindowState = FormWindowState.Minimized
                frmCatlg.Anchor = AnchorStyles.Bottom
                'Show Form(s)
                frmEditr.Show()
                frmCatlg.Show()
            Case "Select"
                'Form: DataGridViewDesigns Catalog
                If IsNothing(frmCatlg) OrElse frmCatlg.IsDisposed Then
                    frmCatlg = New frmDataGridViewDesigns_Catalog
                End If
                frmCatlg.TopLevel = False
                frmCatlg.MdiParent = frmMain
                frmCatlg.Top = 50
                frmCatlg.Left = 50
                frmCatlg.WindowState = FormWindowState.Normal
                'Show Form(s)
                frmCatlg.Show()
        End Select

    End Sub

Button_Select sub in Catalog Form:
    Private Sub Button_Select_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
            Handles Button_Select.Click

        My.Settings.sPubFormAction = "Reload"
        My.Settings.Save()
        Me.Close()

    End Sub

New FormClosed sub in Catalog Form:
    Private Sub frmDataGridViewDesigns_Catalog_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) _
            Handles Me.FormClosed

        Me.Dispose()
        Call clsDataGridViewDesigns.subFormsProcessing()

    End Sub


This works GREAT!.....
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now