I'm a bit stuck. I've tried googling but my google-fu appears to be lacking
I've finished almost everything I need to do in this program, I'm just stuck and don't know the formula/code for this one thing
three forms.
form1 is the parent form, form2 and form3 are child forms
form1 has a menu strip that will make form2 and form3 display when selected
form2 has three textboxes, textbox1, textbox2 and a hidden textbox3 one button that gets the average value of both textboxes and displays/saves it on textbox3
form3 has one, textbox1
the value of which should be the average of the two textboxes above

this is the code that I tried on a button in form 2 to 'save' the values. doesn't work

       Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
                textbox3.Text = Val(textbox1.Text) + Val(textbox2.Text)
                textbox3.Text = Val(textbox3.Text) / 2
                Form3.textbox1.Text = Me.textbox3.Text
        End Sub

i'm suspecting i'm not using the right code, since the above works for two non-child forms that are already displayed
i'm sorry if i can't describe the situation properly, english isn't my main language.
any assistance would be appreciated

Instead of using textbox3 you can try this

Form3.textbox1.Text = (Val(textbox1.Text) + Val(textbox2.Text))/2

@Shark_1

thanks!
the formula is much better, thanks!

however it still doesn't carry over the average/value over to Form3

Sorry, I forgot a point to write that by using this method to transfer value from form2 to form3 you must have to show form3 before clicking the button on form2 .

If you want to show form3 before / after clicking button on form3, you can do it easily by creating a class object or a module Lavel public variable. Store value into the variable and display it when you want.

oh I see.

then perhaps this method is not the one that I would need.

Form3 needs to be closed and will only open when I click a menu item to open it, complete with the values

if it's not against the rules here, this is how it basically supposed to be. ignore the operation that I used there, that can be changed later.

https://mega.nz/#!YBcgyDxb!nCiz02eicn0oZNFZFOG8ATkBfEItembtYGVEGnvC0fw

If you override the constructor for form3 you can get it to accept a string. then it's a simple matter of assigning the string to the text property on form3:

Public Class Form3
    Public Sub New(form2Data As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        TextBox1.Text = form2Data
    End Sub

End Class

Then to show form3:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim newForm3 = New Form3(TextBox3.Text)
    newForm3.MdiParent = Me.MdiParent
    newForm3.Show()
End Sub

Sorry I didn't realize you had submitted your whole code. Here's how your code would look:

Public Class frmMain

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        IsMdiContainer = True
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        Dim child As New Form2
        child.MdiParent = Me
        child.Show()
    End Sub

    Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem3.Click
        'Instead of a directCast like this, you could make a global Form2 object instead'
        Dim form2data As String = DirectCast(MdiChildren(0), Form2).data
        Dim child As New Form3(form2data)
        child.MdiParent = Me
        child.Show()
    End Sub

End Class

Public Class Form2
    'I put the data in a public variable instead of using a control'
    Public data As String = ""
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'It is a good habit to get into, to always directly cast your data to the appropriate type.  In a large
         project for instance, six months from now you might have a hard time remembering whether data is a string
         or a number, if you did not identify it as a string'
        data = (Val(Me.TextBox1.Text) + Val(Me.TextBox2.Text)).ToString
    End Sub
End Class

Public Class Form3
    'Using an overloaded constructor like this, mkaes it easy to pass data to a form'
    Public Sub New(form2Data As String)

        ' This call is required by the designer.'
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.'
        TextBox1.Text = form2Data
    End Sub

End Class
commented: Great example & lesson +7

thanks @instaafl

will test that out as soon as I can

thanks again, @instaafl

worked like a charm! now i gotta go and understand exactly how this happens and make it work with 30 other textboxes LOL :P

One suggestion to handle more data, is a custom class. Make it global in your namespace so that you can pass an instance of that to and from your forms.

I tried to add explanations in the code. If anything there stumps you let me know and I'll try to help you.

I have a couple of days to figure it out over the weekend. if you can't tell yet, i'm not very good at this...

if I can't, i'll to take you up on that offer :)

No worries, we've all been there. And I mean all. Everyone who's ever programmed has had to traverse a learning curve. The biggest stumbling many have to overcome is thinking logically. You can't usually get from a to z without going through b,c,d,etc..

Hi tinstafl
I've encountered a bug in the program you've given me that I have no clue how to fix.

http://imgur.com/PUeHUWK

This happens when opening form3 before populating the textboxes on form2.
I suspect this is because there is no data whatsoever in the boxes so a workaround that I thought of is to have an initial value of zero on those boxes. However, when I try to do this it either does nothing and spews out the same error or completely ignores the formula and just outright replaces it the value previously calculated

Also, you'll be glad to know that I've finished the whole thing and this is the only problem that I have now :)
thank you again for your assistance before

This is becuase you've tried to open form3 before form2 is created. The simplest workaround is to check if mdichildren is empty first:

Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem3.Click

    Dim form2data As String = ""
    If (Me.MdiChildren.Count > 0) Then
        form2data = DirectCast(MdiChildren(0), Form2).data
    End If
    Dim child As New Form3(form2data)
    child.MdiParent = Me
    child.Show()
End Sub

Another option, though a bit more complicated is to disable ToolStripMenuItem3 until form2 is created:

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    ToolStripMenuItem3.Enabled = False
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    IsMdiContainer = True
End Sub

Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
    Dim child As New Form2
    child.MdiParent = Me
    ToolStripMenuItem3.Enabled = True
    child.Show()
End Sub

Of course if the forms will be opened and closed, you'll have to figure out which trigger you'll use to diasble the menu item. Like I said more complicated.

Falling kind of in between those 2 options in terms of complexity, is to make the form2 object global to the frmMain object. This way it can always be a child and you just show or hide at will:

Public Class frmMain
    Dim child As New Form2()
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        child.MdiParent = Me
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        IsMdiContainer = True
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        child.Show()
    End Sub

    Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem3.Click

        Dim form2data As String = child.data
        Dim child2 As New Form3(form2data)
        child2.MdiParent = Me
        child.Show()
    End Sub

End Class

As always if this fixes your problem, please remember to reward those who helped you with an upvote and to mark this solved thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.