Multiple Forms

I have two forms and I am trying to write (upon a button click event handler) the text from my textbox on form1 to the textbox on form2? Any help is greatly appreciated. Suazo

Comments

  • : I have two forms and I am trying to write (upon a button click event handler) the text from my textbox on form1 to the textbox on form2? Any help is greatly appreciated. Suazo
    :
    ===================================================================
    There are several ways of doing this. I've written a set of four articles on devCity which go into some detail on all the ins and outs.
    I'd suggest that you look at Part 2 of the set, which can be found here:
    http://www.devcity.net/net/article.aspx?alias=multipleforms2
    There are links to the other articles from that one, which you can use to exand your toolbox of multiple forms tricks, but I think #2 will probably get you the answer you want for now.
    Let me know if it doesn't answer your question.

  • I have tried implementing the ideas that you have provided in the article and the ideas from the links, but I must be missing something. Either I haven't declared something or I left something out. Here is an easier example of what I am trying to do. The calc works and when the user chooses an operator (*,/,+,-), the form should load the text from textbox1 on form1 to the txtcalc.text on form2(myform).

    Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim myform As New CalcTape
    Dim myform2 As New About
    Dim operand1 As Double
    Dim operand2 As Double
    Dim operator As Char
    Dim EqButton As Boolean
    Dim SavedValue As Double


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

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
    myform.Close()
    End Sub

    Private Sub btnTape_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTape.Click
    myform.Show()
    End Sub

    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
    textbox1.Text &= "0"
    EqButton = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    textbox1.Text &= "1"
    EqButton = False
    End Sub

    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
    textbox1.Text &= "2"
    EqButton = False
    End Sub

    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
    textbox1.Text &= "3"
    EqButton = False
    End Sub

    Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
    textbox1.Text &= "4"
    EqButton = False
    End Sub

    Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
    textbox1.Text &= "5"
    EqButton = False
    End Sub

    Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
    textbox1.Text &= "6"
    EqButton = False
    End Sub

    Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
    textbox1.Text &= "7"
    EqButton = False
    End Sub

    Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
    textbox1.Text &= "8"
    EqButton = False
    End Sub

    Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
    textbox1.Text &= "9"
    EqButton = False
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    textbox1.Text = Nothing
    operand1 = Nothing
    operand2 = Nothing
    End Sub

    Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click

    Dim result As Double
    If EqButton = True Then
    operand2 = SavedValue
    Else
    operand2 = Val(textbox1.Text)
    SavedValue = Val(textbox1.Text)
    End If

    Select Case operator
    Case "+"
    result = operand1 + operand2
    Case "-"
    result = operand1 - operand2
    Case "*"
    result = operand1 * operand2
    Case "/"
    If operand2 = "0" Then
    MessageBox.Show("Cannot divide by 0")
    textbox1.Text = Nothing
    Else
    result = operand1 / operand2
    End If
    End Select
    textbox1.Text = result.ToString
    EqButton = True
    operand1 = result

    ' Me.textbox1.Text = myform.Tape

    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
    operand1 = Val(textbox1.Text)
    operator = "/"
    textbox1.Text = Nothing
    EqButton = False

    ' Me.textbox1.Text = myform.Tape
    End Sub

    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
    operand1 = Val(textbox1.Text)
    operator = "*"
    textbox1.Text = Nothing
    EqButton = False

    ' Me.textbox1.Text = myform.Tape
    End Sub

    Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
    operand1 = Val(textbox1.Text)
    operator = "-"
    textbox1.Text = Nothing
    EqButton = False

    ' Me.textbox1.Text = myform.Tape
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    operand1 = Val(textbox1.Text)
    operator = "+"
    textbox1.Text = Nothing
    EqButton = False

    ' Me.textbox1.Text = myform.Tape
    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
    myform.Show()
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
    myform.Close()
    End Sub

    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
    Me.Close()
    End Sub

    Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
    myform2.Show()
    End Sub

    Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Move

    End Sub

    Private Sub textbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.TextChanged
    ' RaiseEvent TextHasChanged()
    End Sub
    End Class



    Form2 (CalcTape)

    Public Class CalcTape
    Inherits System.Windows.Forms.Form
    ' Dim WithEvents f1 As New Form1

    Private Sub CalcTape_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    ' Public ReadOnly Property Tape() As String
    ' Get
    ' Return txtCalc.Text
    ' End Get
    ' End Property

    Private Sub txtCalc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCalc.TextChanged

    End Sub

    Private Sub CalcTape_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.TextChanged
    ' Me.txtCalc.Text = f1.textbox1.Text
    End Sub
    End Class
  • OK, there were a couple of small probs with what you were doing and I can see that you got pretty confused with the WithEvents side of things.
    I've just re-read your original post and see that you want the transfer of data between forms to be triggered by a button press. I see from your code below that you want this to happen when any of the operand buttons are clicked.
    Let's start afresh with a different way of doing it. If you get the hang of the principle of this new way you can reuse it in many different scenarios in the future.
    1. Create a Module in your project (leave it as Module1)
    2. Put this code in the Module
    [code]
    Module Module1
    ' Declare Form Variables to hold instances of your forms
    Friend F1 As Form1
    Friend CT As CalcTape
    End Module
    [/code]
    3. Put this code in the Form Load event of Form1
    [code]
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    F1 = Me
    End Sub
    [/code]
    (Your application now has a Reference to the Form1 instance and you can point to this variable just in the same way as you might with any other - eg. Your Operand1 and Operand2 variables for Doubles; the only difference being that this is a Form object, not a Double)
    4. This code also in Form1 btnTape Click event
    [code]
    Private Sub btnTape_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTape.Click
    ' If there is not a current instance of CalcTape running, create one.
    ' This limits the number of CalcTape form's to just one at any one time.
    If CT Is Nothing Then
    CT = New CalcTape
    CT.Show()
    End If
    End Sub
    [/code]
    5. You can put the same code in your menuitem. Or you could use an alternative of:
    [code]
    btnTape.PerformClick()
    [/code]
    which has the effect of telling the application that user has clicked that button.
    6. In Form1 Create a procedure that can be called from any button:
    (The comments are quite important to avoid problems if you reuse
    the principles of this in other projects)
    [code]
    Private Sub UpdateCalcTape()
    ' Update the other form's textbox
    ' Very important: If the CalcTape instance does not
    ' exist then your app will crash.
    ' Therefore you could put your code in a Try/Catch block or
    ' (as I've done here) test for the existence
    ' of the instance of CalcTape
    If Not CT Is Nothing Then
    CT.txtCalc.Text = Me.TextBox1.Text
    End If
    End Sub
    [/code]
    It's not that important in the project as it stands, but it is good to get into the habit of creating Subs and Functions for all activities that may be called multiple times from different events.
    7. In the click event of the add, subtract, etc buttons* that you want to use to effect an update of CalcTape's textbox, insert the first line as shown in the example below:
    [code]
    Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
    UpdateCalcTape()
    operand1 = Val(TextBox1.Text)
    operator = "-"
    TextBox1.Text = ""
    EqButton = False
    End Sub
    [/code]
    Two important points here:
    a. The line MUST go before the one that makes TextBox.Text = "". I know it sounds obvious when I say it, but you'd be surprised how often it's the "obvious" things that catch you out!
    b. * In general, it would be better coding practice to put the handling of button clicks into a separate procedure which would handle multiple buttons - eg "Handles btnDivide.Click, btnAdd.Click," etc. But in this relatively simple example it doesn't make much difference. (Worth remembering for the future though)
    8. The Equals button is slightly different, because you don't want to update CalcTape until the TextBox1 on Form shows the result. Therefore you should not put it in as the first line in that particular event, but put it right after the textbox1 has been updated with the result, ie:
    [code]
    TextBox1.Text = result.ToString
    UpdateCalcTape()
    [/code]
    It runs fine for me, so unless I've left any instruction out, it should go OK for you too.
    Let me know if it doesn't.



    ==================================================================

    : I have tried implementing the ideas that you have provided in the article and the ideas from the links, but I must be missing something. Either I haven't declared something or I left something out. Here is an easier example of what I am trying to do. The calc works and when the user chooses an operator (*,/,+,-), the form should load the text from textbox1 on form1 to the txtcalc.text on form2(myform).
    :
  • I have textbox1.text updating the calctape, but it doesn't open the calctape when an operator is clicked, which is fine. Actually, I prefer the user to have the option with or without the tape. However, the result is the only thing updating in the calctape. I cannot see the values that were inserted to receive the value. For example(calctape):
    8
    +8
    16

    It would even better if the operator were shown, but I don't need to go crazy here. I would like it to show the value(on a separate line) before the operator was clicked. Any suggestions would be greatly appreciated.

    Module Module1
    Friend F1 As Form1
    Friend CT As CalcTape
    End Module
    Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim myform As New CalcTape
    Dim myform2 As New About
    Dim operand1 As Double
    Dim operand2 As Double
    Dim operator As Char
    Dim EqButton As Boolean
    Dim SavedValue As Double
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    F1 = Me
    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
    myform.Close()
    CT.Close()
    End Sub
    Private Sub btnTape_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTape.Click
    myform.Show()
    If CT Is Nothing Then
    CT = New CalcTape
    CT.Show()
    End If
    End Sub
    Private Sub UpdateCalcTape()
    If Not CT Is Nothing Then
    CT.txtCalc.Text = Me.textbox1.Text
    End If
    End Sub
    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
    textbox1.Text &= "0"
    EqButton = False
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    textbox1.Text &= "1"
    EqButton = False
    End Sub
    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
    textbox1.Text &= "2"
    EqButton = False
    End Sub
    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
    textbox1.Text &= "3"
    EqButton = False
    End Sub
    Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
    textbox1.Text &= "4"
    EqButton = False
    End Sub
    Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
    textbox1.Text &= "5"
    EqButton = False
    End Sub
    Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
    textbox1.Text &= "6"
    EqButton = False
    End Sub
    Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
    textbox1.Text &= "7"
    EqButton = False
    End Sub
    Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
    textbox1.Text &= "8"
    EqButton = False
    End Sub
    Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
    textbox1.Text &= "9"
    EqButton = False
    End Sub
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
    End Sub
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    textbox1.Text = Nothing
    operand1 = Nothing
    operand2 = Nothing
    End Sub
    Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
    Dim result As Double
    If EqButton = True Then
    operand2 = SavedValue
    Else
    operand2 = Val(textbox1.Text)
    SavedValue = Val(textbox1.Text)
    End If
    Select Case operator
    Case "+"
    result = operand1 + operand2
    Case "-"
    result = operand1 - operand2
    Case "*"
    result = operand1 * operand2
    Case "/"
    If operand2 = "0" Then
    MessageBox.Show("Cannot divide by 0")
    textbox1.Text = Nothing
    Else
    result = operand1 / operand2
    End If
    End Select
    textbox1.Text = result.ToString
    EqButton = True
    operand1 = result
    UpdateCalcTape()
    End Sub
    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
    UpdateCalcTape()
    operand1 = Val(textbox1.Text)
    operator = "/"
    textbox1.Text = Nothing
    EqButton = False
    End Sub
    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
    UpdateCalcTape()
    operand1 = Val(textbox1.Text)
    operator = "*"
    textbox1.Text = Nothing
    EqButton = False
    End Sub
    Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
    UpdateCalcTape()
    operand1 = Val(textbox1.Text)
    operator = "-"
    textbox1.Text = Nothing
    EqButton = False
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    UpdateCalcTape()
    operand1 = Val(textbox1.Text)
    operator = "+"
    textbox1.Text = Nothing
    EqButton = False
    End Sub
    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
    myform.Show()
    End Sub
    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
    myform.Close()
    CT.Close()
    End Sub
    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
    Me.Close()
    End Sub
    Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
    myform2.Show()
    End Sub
    End Class



    : OK, there were a couple of small probs with what you were doing and I can see that you got pretty confused with the WithEvents side of things.
    : I've just re-read your original post and see that you want the transfer of data between forms to be triggered by a button press. I see from your code below that you want this to happen when any of the operand buttons are clicked.
    : Let's start afresh with a different way of doing it. If you get the hang of the principle of this new way you can reuse it in many different scenarios in the future.
    : 1. Create a Module in your project (leave it as Module1)
    : 2. Put this code in the Module
    : [code]
    : Module Module1
    : ' Declare Form Variables to hold instances of your forms
    : Friend F1 As Form1
    : Friend CT As CalcTape
    : End Module
    : [/code]
    : 3. Put this code in the Form Load event of Form1
    : [code]
    : Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    : F1 = Me
    : End Sub
    : [/code]
    : (Your application now has a Reference to the Form1 instance and you can point to this variable just in the same way as you might with any other - eg. Your Operand1 and Operand2 variables for Doubles; the only difference being that this is a Form object, not a Double)
    : 4. This code also in Form1 btnTape Click event
    : [code]
    : Private Sub btnTape_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTape.Click
    : ' If there is not a current instance of CalcTape running, create one.
    : ' This limits the number of CalcTape form's to just one at any one time.
    : If CT Is Nothing Then
    : CT = New CalcTape
    : CT.Show()
    : End If
    : End Sub
    : [/code]
    : 5. You can put the same code in your menuitem. Or you could use an alternative of:
    : [code]
    : btnTape.PerformClick()
    : [/code]
    : which has the effect of telling the application that user has clicked that button.
    : 6. In Form1 Create a procedure that can be called from any button:
    : (The comments are quite important to avoid problems if you reuse
    : the principles of this in other projects)
    : [code]
    : Private Sub UpdateCalcTape()
    : ' Update the other form's textbox
    : ' Very important: If the CalcTape instance does not
    : ' exist then your app will crash.
    : ' Therefore you could put your code in a Try/Catch block or
    : ' (as I've done here) test for the existence
    : ' of the instance of CalcTape
    : If Not CT Is Nothing Then
    : CT.txtCalc.Text = Me.TextBox1.Text
    : End If
    : End Sub
    : [/code]
    : It's not that important in the project as it stands, but it is good to get into the habit of creating Subs and Functions for all activities that may be called multiple times from different events.
    : 7. In the click event of the add, subtract, etc buttons* that you want to use to effect an update of CalcTape's textbox, insert the first line as shown in the example below:
    : [code]
    : Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
    : UpdateCalcTape()
    : operand1 = Val(TextBox1.Text)
    : operator = "-"
    : TextBox1.Text = ""
    : EqButton = False
    : End Sub
    : [/code]
    : Two important points here:
    : a. The line MUST go before the one that makes TextBox.Text = "". I know it sounds obvious when I say it, but you'd be surprised how often it's the "obvious" things that catch you out!
    : b. * In general, it would be better coding practice to put the handling of button clicks into a separate procedure which would handle multiple buttons - eg "Handles btnDivide.Click, btnAdd.Click," etc. But in this relatively simple example it doesn't make much difference. (Worth remembering for the future though)
    : 8. The Equals button is slightly different, because you don't want to update CalcTape until the TextBox1 on Form shows the result. Therefore you should not put it in as the first line in that particular event, but put it right after the textbox1 has been updated with the result, ie:
    : [code]
    : TextBox1.Text = result.ToString
    : UpdateCalcTape()
    : [/code]
    : It runs fine for me, so unless I've left any instruction out, it should go OK for you too.
    : Let me know if it doesn't.

  • Aha, I hear the familiar sound of goalposts being moved :-)
    "...when the user chooses an operator (*,/,+,-), the form should load the text from textbox1 on form1 to the txtcalc.text on form2(myform). "
    When the user clicks [italic] on one of those buttons [/italic] then
    [italic] the current contents of textbox1 [/italic] are copied to the CalcTape form. I'm pretty sure you'll find that that's what my code sample does. ;-)
    It actually sounds like what you really wanted to do was to update CalcTape whenever the text in TextBox1 changed. That way, what you see in the first form, you also see in the CalcTape form.
    The way to do that would have been to put code in the TextChanged event of your TextBox1 on Form1. I'll leave you to work on that one if you want to follow it up, as the code logic is very similar to the sample I posted.
    Re the new spec of having TapeCalc now also show the operands and displaying on separate, this is fairly easy, but with one massive gotcha which willcatch yoou out if you're not careful. First, to transfer all button presses and textbox1 values to CalcTape, ensure that your CalcTape textbox MultiLine property is set to True.
    Then add code similar to this to the buttons you want to have an effect on CalcTape's display.
    [code]
    CT.txtCalc.Text &= operator & vbCrLf
    [/code]
    You'll need to write a slight variation on the theme for the equals button.
    The gotcha however is this. What if the user want to enter the value 12 or 342 or 65 - any value which takes more than one button press? You will get the digits shown on CalcTape, but they will be on separate lines, one below the other. Try the suggestions so far and you will see the problem
    One solution for you to consider would be to store the values of the numeric buttons key presses in a variable and only update the CalcTape display when the equals button is pressed. The logic is similar to what we did when we created tht "SavedValue" variable a while back to resolve a different problem. (You will have to consider whether it is better to update this variable at every press of a numeric button or just when the equals button is pressed).
    Give those suggestions a go and see if you can crack it. If you get stuck, just post a follow up and I'll be happy to help out.
    =================================================================








    : I have textbox1.text updating the calctape, but it doesn't open the calctape when an operator is clicked, which is fine. Actually, I prefer the user to have the option with or without the tape. However, the result is the only thing updating in the calctape. I cannot see the values that were inserted to receive the value. For example(calctape):
    : 8
    : +8
    : 16
    :
    : It would even better if the operator were shown, but I don't need to go crazy here. I would like it to show the value(on a separate line) before the operator was clicked. Any suggestions would be greatly appreciated.
    :
    : Module Module1
    : Friend F1 As Form1
    : Friend CT As CalcTape
    : End Module
    : Public Class Form1
    : Inherits System.Windows.Forms.Form
    :
    : Dim myform As New CalcTape
    : Dim myform2 As New About
    : Dim operand1 As Double
    : Dim operand2 As Double
    : Dim operator As Char
    : Dim EqButton As Boolean
    : Dim SavedValue As Double
    : Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    : F1 = Me
    : End Sub
    : Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
    : myform.Close()
    : CT.Close()
    : End Sub
    : Private Sub btnTape_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTape.Click
    : myform.Show()
    : If CT Is Nothing Then
    : CT = New CalcTape
    : CT.Show()
    : End If
    : End Sub
    : Private Sub UpdateCalcTape()
    : If Not CT Is Nothing Then
    : CT.txtCalc.Text = Me.textbox1.Text
    : End If
    : End Sub
    : Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
    : textbox1.Text &= "0"
    : EqButton = False
    : End Sub
    : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    : textbox1.Text &= "1"
    : EqButton = False
    : End Sub
    : Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
    : textbox1.Text &= "2"
    : EqButton = False
    : End Sub
    : Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
    : textbox1.Text &= "3"
    : EqButton = False
    : End Sub
    : Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
    : textbox1.Text &= "4"
    : EqButton = False
    : End Sub
    : Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
    : textbox1.Text &= "5"
    : EqButton = False
    : End Sub
    : Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
    : textbox1.Text &= "6"
    : EqButton = False
    : End Sub
    : Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
    : textbox1.Text &= "7"
    : EqButton = False
    : End Sub
    : Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
    : textbox1.Text &= "8"
    : EqButton = False
    : End Sub
    : Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
    : textbox1.Text &= "9"
    : EqButton = False
    : End Sub
    : Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    : Me.Close()
    : End Sub
    : Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    : textbox1.Text = Nothing
    : operand1 = Nothing
    : operand2 = Nothing
    : End Sub
    : Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
    : Dim result As Double
    : If EqButton = True Then
    : operand2 = SavedValue
    : Else
    : operand2 = Val(textbox1.Text)
    : SavedValue = Val(textbox1.Text)
    : End If
    : Select Case operator
    : Case "+"
    : result = operand1 + operand2
    : Case "-"
    : result = operand1 - operand2
    : Case "*"
    : result = operand1 * operand2
    : Case "/"
    : If operand2 = "0" Then
    : MessageBox.Show("Cannot divide by 0")
    : textbox1.Text = Nothing
    : Else
    : result = operand1 / operand2
    : End If
    : End Select
    : textbox1.Text = result.ToString
    : EqButton = True
    : operand1 = result
    : UpdateCalcTape()
    : End Sub
    : Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
    : UpdateCalcTape()
    : operand1 = Val(textbox1.Text)
    : operator = "/"
    : textbox1.Text = Nothing
    : EqButton = False
    : End Sub
    : Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
    : UpdateCalcTape()
    : operand1 = Val(textbox1.Text)
    : operator = "*"
    : textbox1.Text = Nothing
    : EqButton = False
    : End Sub
    : Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
    : UpdateCalcTape()
    : operand1 = Val(textbox1.Text)
    : operator = "-"
    : textbox1.Text = Nothing
    : EqButton = False
    : End Sub
    : Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    : UpdateCalcTape()
    : operand1 = Val(textbox1.Text)
    : operator = "+"
    : textbox1.Text = Nothing
    : EqButton = False
    : End Sub
    : Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
    : myform.Show()
    : End Sub
    : Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
    : myform.Close()
    : CT.Close()
    : End Sub
    : Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
    : Me.Close()
    : End Sub
    : Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
    : myform2.Show()
    : End Sub
    : End Class
    :
    :
    :
    : : OK, there were a couple of small probs with what you were doing and I can see that you got pretty confused with the WithEvents side of things.
    : : I've just re-read your original post and see that you want the transfer of data between forms to be triggered by a button press. I see from your code below that you want this to happen when any of the operand buttons are clicked.
    : : Let's start afresh with a different way of doing it. If you get the hang of the principle of this new way you can reuse it in many different scenarios in the future.
    : : 1. Create a Module in your project (leave it as Module1)
    : : 2. Put this code in the Module
    : : [code]
    : : Module Module1
    : : ' Declare Form Variables to hold instances of your forms
    : : Friend F1 As Form1
    : : Friend CT As CalcTape
    : : End Module
    : : [/code]
    : : 3. Put this code in the Form Load event of Form1
    : : [code]
    : : Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    : : F1 = Me
    : : End Sub
    : : [/code]
    : : (Your application now has a Reference to the Form1 instance and you can point to this variable just in the same way as you might with any other - eg. Your Operand1 and Operand2 variables for Doubles; the only difference being that this is a Form object, not a Double)
    : : 4. This code also in Form1 btnTape Click event
    : : [code]
    : : Private Sub btnTape_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTape.Click
    : : ' If there is not a current instance of CalcTape running, create one.
    : : ' This limits the number of CalcTape form's to just one at any one time.
    : : If CT Is Nothing Then
    : : CT = New CalcTape
    : : CT.Show()
    : : End If
    : : End Sub
    : : [/code]
    : : 5. You can put the same code in your menuitem. Or you could use an alternative of:
    : : [code]
    : : btnTape.PerformClick()
    : : [/code]
    : : which has the effect of telling the application that user has clicked that button.
    : : 6. In Form1 Create a procedure that can be called from any button:
    : : (The comments are quite important to avoid problems if you reuse
    : : the principles of this in other projects)
    : : [code]
    : : Private Sub UpdateCalcTape()
    : : ' Update the other form's textbox
    : : ' Very important: If the CalcTape instance does not
    : : ' exist then your app will crash.
    : : ' Therefore you could put your code in a Try/Catch block or
    : : ' (as I've done here) test for the existence
    : : ' of the instance of CalcTape
    : : If Not CT Is Nothing Then
    : : CT.txtCalc.Text = Me.TextBox1.Text
    : : End If
    : : End Sub
    : : [/code]
    : : It's not that important in the project as it stands, but it is good to get into the habit of creating Subs and Functions for all activities that may be called multiple times from different events.
    : : 7. In the click event of the add, subtract, etc buttons* that you want to use to effect an update of CalcTape's textbox, insert the first line as shown in the example below:
    : : [code]
    : : Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
    : : UpdateCalcTape()
    : : operand1 = Val(TextBox1.Text)
    : : operator = "-"
    : : TextBox1.Text = ""
    : : EqButton = False
    : : End Sub
    : : [/code]
    : : Two important points here:
    : : a. The line MUST go before the one that makes TextBox.Text = "". I know it sounds obvious when I say it, but you'd be surprised how often it's the "obvious" things that catch you out!
    : : b. * In general, it would be better coding practice to put the handling of button clicks into a separate procedure which would handle multiple buttons - eg "Handles btnDivide.Click, btnAdd.Click," etc. But in this relatively simple example it doesn't make much difference. (Worth remembering for the future though)
    : : 8. The Equals button is slightly different, because you don't want to update CalcTape until the TextBox1 on Form shows the result. Therefore you should not put it in as the first line in that particular event, but put it right after the textbox1 has been updated with the result, ie:
    : : [code]
    : : TextBox1.Text = result.ToString
    : : UpdateCalcTape()
    : : [/code]
    : : It runs fine for me, so unless I've left any instruction out, it should go OK for you too.
    : : Let me know if it doesn't.
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories