calling a function located in the calling form from the called form

I have a form called frmGraphDesigner that allows users to specify the the multiple data series for a chart on that form as well as to specify the chart style, i.e Pie, line. or columns.  I've now created a second form called frmCustomGraph which is called from the form frmGraphicDesigner via a button on that form .  The purpose of the form is to allow users to specify a  differest chart style for each dataseries that is beiing desplayed in the calling form . The custom graph form has a button called set.  after the user makes selections from the list box, and presses the set button, I want the chart in the calling form to immediately reflect the new chart style that has been assigned to each data series on the chart
GraphDesigner.JPG
Juan VelasquezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
You simply show frmCustomGraph with ShowDialog() which causes execution to stop in frmGraphicDesigner until it is closed.  Then you can query the dialog to get the necessary info from it and update the graph.

Here is a simplified example with Form1 and Form2:
Public Class Form1

    Private Sub btnCustomize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustomize.Click
        Dim customize As New Form2
        If customize.ShowDialog = Windows.Forms.DialogResult.OK Then
            ' grab something from Form2:
            Dim index As Integer = customize.ListBox1.SelectedIndex
            If index <> -1 Then
                Label1.Text = customize.ListBox1.Items(index)
            End If
        End If
    End Sub

End Class

Public Class Form2

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

End Class

Open in new window

0
Juan VelasquezAuthor Commented:
I wanted the graph to be updated on the calling form while the form "frmCustomGraph" was still open.  I want the user to see the changes made to the data series on the chart once the use click the Set button on the form frmCustomGraph
0
Juan VelasquezAuthor Commented:
I think what I am trying to do may not be possible.  I will implement your suggestion Idle_Mind:
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Mike TomlinsonMiddle School Assistant TeacherCommented:
No, no...it is possible!  Hold on....
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here's an example where Form2 raises a custom event to pass data back to Form1 whenever the Button on Form2 is pressed.  You can pass back anything in your event (even multiple parameters or the form itself) so it can be used to update Form1:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f2 As New Form2
        AddHandler f2.NewSetting, AddressOf f2_NewSetting
        f2.Show()
    End Sub

    Private Sub f2_NewSetting(ByVal value As String)
        Label1.Text = value
    End Sub

End Class

Public Class Form2

    Public Event NewSetting(ByVal value As String)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim index As Integer = ListBox1.SelectedIndex
        If index <> -1 Then
            RaiseEvent NewSetting(ListBox1.Items(index))
        End If
    End Sub

End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Juan VelasquezAuthor Commented:
Hello Idle_Mind:,
It works great.  However, I'm having a little difficulty understanding the code flow.  It's important to me that I understand how the code work.  So here goes
First I defined a public event along with the parameters that will be passed.  In my case, since I was passing a list object, I put the following code in frmCustomizeGraph (Form2 in your example)
Public Event NewSetting(ByVal value As List(Of GraphReport))
 To cause the event to be raised, I added the following code to the click event of the settings button on frmCustomizeGraph
RaiseEvent NewSetting(SelectedDataSeries)
In the click event of the calling form (Form frmGraphDesigner) I added the following code
AddHandler CustomizeGraphForm.NewSetting, AddressOf f2_NewSetting
I also created a private subprocedure in the calling form called f2_NewSetting
Private Sub f2_NewSetting(ByVal value As List(Of GraphReport))
        Dim strMessage As String = ""
        SelectedDataSeries = value
        For Each g As GraphReport In SelectedDataSeries
            strMessage &= g.DataVersionId.ToString & vbTab & g.GraphType.ToString & vbCrLf
        Next
        MessageBox.Show(strMessage)
    End Sub

So what is happening is that when the customizegraph button is clicked on the calling form, the private sub f2_NewSetting is hooked up to  Public Event NewSetting(ByVal value As List(Of GraphReport)).  Consequently when this event is raised in the second form, the parameters passed to it via
RaiseEvent NewSetting(SelectedDataSeries) are then processed by the private sub f2_NewSetting which had previously been hooked up to the public event NewSetting.  Am I correct.  I just want to be sure what is going on rather than blindly copying and pasting code
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
That's perfect!  You've taken the example and applied it to your scenario.  =)

This is known as a "loosely coupled" solution because frmCustomGraph has no knowledge of frmGraphDesigner.  It simply raises an event and whoever has subscribed to it will get notified.  This is the most flexible solution since frmCustomGraph could theoretically be re-used with little or no change at all with a different form than frmGraphDesigner.

A "tightly coupled" solution could also have been used.  This would involve passing a reference to frmGraphDesigner into frmCustomGraph where it is stored locally.  Then, frmCustomGraph would use its reference and DIRECLTY update the graph from within itself without broadcasting any events.  Since a custom type (frmGraphDesigner) is being passed in, this approach is theoretically less re-usable.

Both approaches are valid and have pros and cons.  The loosely coupled solution is slightly harder to follow since the "flow" is broken between two forms but can re-used easily when the problem must be extended somehow.  The tightly coupled solution becomes specific to the two forms involved (unless you do some extra work involving Interfaces) but is easier to read as all the action occurs in one place.

Hope that helps.
0
Juan VelasquezAuthor Commented:
Great solution and I appreciate the additional information regarding loosely coupled solutions
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.