Good Day!.
Im, a new programmer.
I want that the value of my textbox6 in form1 will add to the value of the textbox6 in form2. can anyone help me. :(

Dani AI

Generated

Short answer: treat the textbox values as typed data, not as UI strings. As pointed out, first decide whether you want string concatenation or numeric addition. Rather than making controls public or using globals (approaches suggested by and others), keep controls private (good point by ) and expose a typed property plus an event so other forms can read a safe numeric value and react when it changes.

Example pattern for Form1: expose a Decimal-typed read-only property and raise an event on TextChanged.

Public Class Form1

    Public ReadOnly Property Value6 As Decimal
        Get
            Dim parsed As Decimal
            If Decimal.TryParse(TextBox6.Text, parsed) Then
                Return parsed
            End If
            Return Decimal.Zero
        End Get
    End Property

    Public Event Value6Changed As EventHandler

    Private Sub TextBox6_TextChanged(sender As Object, e As EventArgs) Handles TextBox6.TextChanged
        RaiseEvent Value6Changed(Me, EventArgs.Empty)
    End Sub

End Class

Consume that safely from Form2 by passing the actual Form1 instance (avoid VB default-instance pitfalls), subscribing to the event, and computing the sum with TryParse on Form2’s textbox:

Public Class Form2

    Private ReadOnly _owner As Form1

    Public Sub New(owner As Form1)
        InitializeComponent()
        _owner = owner
        AddHandler _owner.Value6Changed, AddressOf Owner_Value6Changed
        UpdateSum()
    End Sub

    Private Sub Owner_Value6Changed(sender As Object, e As EventArgs)
        UpdateSum()
    End Sub

    Private Sub UpdateSum()
        Dim localVal As Decimal
        Decimal.TryParse(TextBox6.Text, localVal)
        LabelResult.Text = (_owner.Value6 + localVal).ToString()
    End Sub

End Class

Extra tips: prefer Decimal (or the numeric type you actually need), always use TryParse to avoid exceptions, consider ShowDialog and reading the typed property if Form1 is a modal input form, or use a small shared model / INotifyPropertyChanged for more complex UI updates.

Recommended Answers

All 7 Replies

What do you mean by "Value"?
Are you concatenating strings or adding numbers?
If you are adding numbers, have you converted the form2.textbox6.Text to a numeric value?

If you mean string + string, did you?: form1.textbox6.Text += form2.textbox6.Text;

well bernardz26! there are two simple ways to perform this , one is to declare a global or public variable and just access it at any part of your prog , and second way is to call the form then the call the control , here are both ways to perform this operation ,

'declare the public variable
public mynumber as int
 'now assign the value of first textbox of form1 to it .
'use this at the text change event of the textbox
mynumber = val(textbox6.text)
'now place this code at the your form two at any event where you want to sum both values of textbox6 of form1 and textbox6 of form2
textboxResult.text = val(textbox6.text)+mynumber
'this will show the result and sum both textbox values

now there is another way to perform same operation .

'use this code where you want to sum both values
textboxResult.text= val(form1.textbox6.text)+val(textbox6.text)

this will also perform same operation , hope this will solve your prob ,if your prob is solve then please dont forget to mark this thread solve ,

Regards

well bernardz26! there are two simple ways to perform this , one is to declare a global or public variable and just access it at any part of your prog , and second way is to call the form then the call the control , here are both ways to perform this operation ,

'declare the public variable
public mynumber as int
 'now assign the value of first textbox of form1 to it .
'use this at the text change event of the textbox
mynumber = val(textbox6.text)
'now place this code at the your form two at any event where you want to sum both values of textbox6 of form1 and textbox6 of form2
textboxResult.text = val(textbox6.text)+mynumber
'this will show the result and sum both textbox values

now there is another way to perform same operation .

'use this code where you want to sum both values
textboxResult.text= val(form1.textbox6.text)+val(textbox6.text)

this will also perform same operation , hope this will solve your prob ,if your prob is solve then please dont forget to mark this thread solve ,

Regards

we can also use the CInt instead of Val or the Integer.Parse method

commented: Right. Val is vb6 function. +14

try this

textbox1.text = val(form1.textbox6.text) + val(textbox6.text)

put this code in an onclick event,, this will add the value of text6 from form1 and text6 from form2 and it will display its answer on the text1

Form2.TextBox6.Text = Form2.TextBox6.Text & Form1.TextBox6.Text

LOL

...You could also make a function to do this.

...I saw the horses head move and decided to continue the beating.

1. Do not create controls public - keep them private.
2. Use form1`s reference on form2, so you can access memebers of form1 from form2.

Then simply use this reference on form2 to get the value (you want to) from form1.

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.