10 Replies - 3708 Views - Last Post: 14 May 2016 - 01:27 AM Rate Topic: -----

#1 retched  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 25-January 16

Problem passing data back and forth between two windows.

Posted 16 April 2016 - 12:36 AM

Currently in my windows form, I have a few Windows. One WinForm acts as a main menu and is supposed to call another form as a secondary window on its own.

    Private Sub btnMainGame_Click(sender As Object, e As EventArgs) Handles btnMainGame.Click
        ' This is the button to call up the main game controller. So simply hide this form aned then open the new form.

        Dim frmController As New frmControllerScreen

        frmController.Show()
        Me.Hide() ' Happens on .Close as well

    End Sub


The above code invokes another WinForm which is used to handle more options. When the user clicks on a particular button, a sub form is created again.

        Dim OpenNewGameWindow As New frmGameConfig

        OpenNewGamewindow.ShowDialog(Me)
        Me.DialogResult = DialogResult.None ' Used to prevent the subform from closing the main form when it catches a dialog result.



Now in the frmGameConfig, the program is supposed to take data and pass it back to the form that called it.

    Private Sub btnNewGameStartGame_Click(sender As Object, e As EventArgs) Handles btnNewGameStartGame.Click
... Skipped code...
        frmControllerScreen.MasterQuestionList = QuestionList
        frmControllerScreen.blnBankedTime = cbBankedTime.Checked

... Skipped code...
    End Sub



However, when the frmController tries to reference MasterQuestionList... it returns a nullreference error as if it was not set.

Here's where things get funny...

When I made this code, frmControllerScreen was actually the startup form. Now when I change this form back to frmMainMenu, I get NullReference Errors constantly. My question: How am I supposed to pass information from one form to the next form if it was instantiated from a parent form. (Note I even moved the declartion to Public as a "module-wide" variable... and nothing happens.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem passing data back and forth between two windows.

#2 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,891
  • Joined: 12-December 12

Re: Problem passing data back and forth between two windows.

Posted 16 April 2016 - 01:43 AM

Passing Data Between Forms
Was This Post Helpful? 1
  • +
  • -

#3 retched  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 25-January 16

Re: Problem passing data back and forth between two windows.

Posted 17 April 2016 - 08:16 PM

Okay so I was a little afraid that it might come to that. I just don't understand why it works when I change the default start up form back to the original one. (That's where my question takes off and was hoping for a different answer than the link to the thread, which still helps don't get me a bit don't get wrong.)

So my question would be, should I instead send the data that I'm trying to pass (an array of a class object) to a database of some kind instead and then call on that? Or is this all doomed from the word go. From the sound of that tutorial, this is all that I can do from the get go instead of trying to pass one object belonging to a form back to the other to receive. (Which again kind of doesn't explain why when I switch the start up form, this all works.)
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,891
  • Joined: 12-December 12

Re: Problem passing data back and forth between two windows.

Posted 18 April 2016 - 01:38 AM

frmControllerScreen.MasterQuestionList = QuestionList

frmControllerScreen is the name of the original, static instance, of the form, it is not referring to the currently running instance.

However, I've already written my tutorial, so I'll leave it to anyone else who wants to contribute ;). Good luck.
Was This Post Helpful? 1
  • +
  • -

#5 PeterH  Icon User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 280
  • Joined: 03-September 09

Re: Problem passing data back and forth between two windows.

Posted 18 April 2016 - 02:25 AM

You are working with an instance of the class e.g. frmController is created FROM the class template called frmControllerScreen, any methods would be therefore be applicable to the instance name NOT the class template name.
Was This Post Helpful? 0
  • +
  • -

#6 retched  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 25-January 16

Re: Problem passing data back and forth between two windows.

Posted 18 April 2016 - 12:53 PM

View PostPeterH, on 18 April 2016 - 05:25 AM, said:

You are working with an instance of the class e.g. frmController is created FROM the class template called frmControllerScreen, any methods would be therefore be applicable to the instance name NOT the class template name.


Okay I see that... But I'm still drawing a blank between connecting the two points made.

So if I instantiate frmController as a New Instance of frmControllerScreen, then frmController instantiates an instance of frmGameConfig... how would frmGameConfig modify the variable(s) of frmController.

I went ahead and tried the Property method by making a QuestionStack property... but Visual Studio stops me and tells me that's invalid and that it can't see it due to the protection form. (That is frmNewGameConfig can't see frmController's property) (This goes into the separation of concerns andrews mentioned, I guess.) But at this point it's still lost to how do I proceed. I feel like I'm making a mountain out of this but I can't see where I should be heading.

This post has been edited by retched: 18 April 2016 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#7 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1458
  • View blog
  • Posts: 3,700
  • Joined: 01-February 13

Re: Problem passing data back and forth between two windows.

Posted 18 April 2016 - 04:02 PM

Without understanding how your application is actually coded, how it is intended to function, and what data is being passed, it is kind of hard to say which method is best for you to use. However, i will take a guess.

If you need to open the config form, choose a list of strings, and then have the user press an OK button to accept the changes, then you could do something similar to this example.

You can test it out in a new Form project so that you can study it and experiment with it until you understand how it works.

Create a new project and name the first form as frmControllerScreen and add 1 Button and 1 RichTextBox to it. Now add this code to it.
Public Class frmControllerScreen

    'This is a class scoped List that will be used to hold the Question List returned from the frmGameConfig
    Private MasterQuestionList As List(Of String)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Using fgc As New frmGameConfig 'Creates a temporary new instance of frmGameConfig

            'Show the new instance of frmGameConfig and check the DialogResult to see if the person pressed the OK button on the frmGameConfig
            'before trying to access the QuestionList Property that was added to the frmGameConfig.
            If fgc.ShowDialog = DialogResult.OK Then

                MasterQuestionList = fgc.QuestionList 'Set the class scoped MasterQuestionList to the QuestionList that was filled in the frmGameConfig

            End If

        End Using 'this will now Dispose of the new temporary instance of the frmGameConfig

        'this is just to show the List of Strings in a RichTextBox so you can see them. This is only for this example.
        RichTextBox1.Lines = MasterQuestionList.ToArray

    End Sub

End Class



Now add a second form to the project and name it frmGameConfig. Add 1 Button to it and name the button btnOk. Use this code in this form.
Public Class frmGameConfig

    'Add a Public Property that is of the type you want to return to the frmControllerScreen
    Public Property QuestionList As New List(Of String)

    Private Sub frmGameConfig_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'I am just filling the List (QuestionList Property) with a few strings in the Load event for this example. You would fill the List
        'however you need to in your frmGameConfig form.
        Me.QuestionList.Add("A")
        Me.QuestionList.Add("B")
        Me.QuestionList.Add("C")
    End Sub

    Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
        'when the user clicks the OK button, set this forms DialogResult to OK
        Me.DialogResult = DialogResult.OK
    End Sub
End Class


When you run the application, you press the Button to open the frmGameConfig. When it opens, press the Ok button on it. That will close the frmGameConfig by setting its DialogResult to OK. The frmControllerScreen form will detect that the OK button was pressed and will then get the List (Property) from the frmGameConfig and then show it in the RichTextBox.

There are other ways depending on how you need the forms to pass the data but, just from guessing, i think this may be the way you want to use.
Was This Post Helpful? 1
  • +
  • -

#8 retched  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 25-January 16

Re: Problem passing data back and forth between two windows.

Posted 18 April 2016 - 10:05 PM

View PostIronRazer, on 18 April 2016 - 07:02 PM, said:

... SNIP ...



Okay this actually puts me in the right direction at least. I went ahead and created a third form with nothing more than a Button and an .Click event and set that one to be the start up using similar coding

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using frm1 As New frmControllerScreen
            Me.Hide()
            frm1.ShowDialog()
            If frm1.DialogResult = DialogResult.OK Then
                Me.Show()
            Else
                Me.Close()
            End If
        End Using

    End Sub



So this works as a whole. (I'll trace over the form and figure out where to make my changes. It's over 20K lines of code, something I REALLY don't think I can easily share here).

EDIT: I found the answer to my question. It's not "ideal" I'll guess but it does what I want it to do mainly.

This post has been edited by retched: 18 April 2016 - 10:05 PM

Was This Post Helpful? 0
  • +
  • -

#9 retched  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 25-January 16

Re: Problem passing data back and forth between two windows.

Posted 13 May 2016 - 10:02 PM

My question is relating to this method is how do I get the child object form to see the parent object form that invoked it.

Say I have a formA that spawned formB as a .ShowDialog with formA as the parent. I want formB to be able to interact with a control on formA while keeping formB open. Is that possible using this method?
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,891
  • Joined: 12-December 12

Re: Problem passing data back and forth between two windows.

Posted 14 May 2016 - 12:55 AM

Quote

how do I get the child object form to see the parent object form that invoked it.

(my italics) You don't. That is not how this should work.

Quote

I want formB to be able to interact with a control on formA

You definitely don't, this is the whole purpose of my linked tutorial, to move away from this kind of thinking and behaviour. Did you study the tutorial?



Although the second version of ShowDialog allows you to specify the owner of the dialog, this detail is not to be used in the way you are suggesting either. That is, not to directly interact with any Control of another form.

A modal dialog (produced with ShowDialog) commands attention, it is usually to obtain a value or confirmation that is necessary before the application can proceed. A modal dialog, by its nature, should not need to cause any other changes until it has been dismissed, and the application can proceed.

So, firstly, in my opinion, you wouldn't use a dialog form if you need some "interaction" while the modal form is active. Then my tutorial describes ways that two forms can share or pass information (whether you continue with the modal version or not).
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,891
  • Joined: 12-December 12

Re: Problem passing data back and forth between two windows.

Posted 14 May 2016 - 01:27 AM

Quote

Okay so I was a little afraid that it might come to that. ..From the sound of that tutorial..

It sounds like you haven't read it and don't want to.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1