Question

I have an MDI parent form that may open a child form called "Order". Order forms have a button that allows the user to print the order. The Order form has a print size variable defined at the beginning:

Public Class Order

Public psize As String 

    Private Sub button_order_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles process_order.Click
  ' Code to handle the order and then print, etc 

Now the parent form has a psize variable as well, which is set to a default of "A4". Only when someone clicks on one of the menu items on the Parent window's menu strip will this happen:

psize = "A6"

By default, whenever the parent window opens up a new Order form, I need it to set the child form's psize variable to its own psize value. Something like this:

    Dim f As Form
    f = New Order
    f.MdiParent = Me
    f.psize = Me.psize ' BUT THIS LINE DOESN'T WORK
    f.Show()

I get the error that f.psize is not a member of the form. I know that passing variables to and from the MDI parent and child is quite common but despite trying out a few options I saw here, it doesn't seem to be working. Is this the wrong approach?

Was it helpful?

Solution

The reason the property is not available is because you are using the wrong type for the variable. The base Form type does not define that property. Instead, your derived Order type does. You could do something like this:

Dim f As Order
f = New Order
f.MdiParent = Me
f.psize = Me.psize
f.Show()

UPDATE

As you have said in comments below, what you really need to do is to be able to share a dynamic setting between all your forms so that you can change the setting at any time and have it affect all your forms that have already been displayed. The best way to do that, would be to create a new class that stores all your shared settings, for instance:

Public Class Settings
    Public PaperSize As String = "A6"
End Class

As you can see, by doing so, you can easily centralize all your default settings in your settings class, which is an added benefit. Then, you need to change the public property in your Order form to the new Settings type, for instance:

Public Class Order
    Inherits Form

    Public Settings As Settings
End Class

Then, you need to create your shared settings object in your MDI Parent form, and then pass it it to each of your Order forms as they are created:

Public Class MyParentForm
    Private _settings As New Settings()

    Private Sub ShowNewOrderForm()
        Dim f As New Order()
        f.MdiParent = Me
        f.Settings = _settings
        f.Show()
    End Sub

    Private Sub ChangePaperSize(size As String)
        _settings.PaperSize = size
    End Sub
End Class

Then, since the parent form and all the child Order forms share the same Settings object, and change made to that Settings object will be seen immediately by all the forms.

OTHER TIPS

Change this:

Dim f As Form

to the actual implementation of your form:

Dim f As Order

or just the shortcut:

Dim f As New Order
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top