0

I’m making programs which will separate a two digit number from a textbox to two different labels. Later I want to take those two separate numbers and square them indiviudally and add them together. It would be great if someone can get me some help. This is what I have so far:

Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Label1.Text = "First digit:" & TextBox1.Text.Substring(0, 1)
        Label2.Text = "Second digit:" & TextBox1.Text.Substring(1, 1)

        Label3.Text = (Label1.Text) ^ 2
    End Sub
End Class

Edited by pritaeas: Not a snippet, changed to regular thread.

3
Contributors
2
Replies
20
Views
3 Years
Discussion Span
Last Post by Jx_Man
0

You are almost there.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Try
        Dim val1 as Integer = Cint(TextBox1.Text.SubString(0,1))
        Dim val2 as Integer = Cint(TextBox1.Text.SubString(1,1))

        Label1.Text = "First digit:" & TextBox1.Text.Substring(0, 1)
        Label2.Text = "Second digit:" & TextBox1.Text.Substring(1, 1)

        Label3.Text = "Sum is:" & ((val1 *= val1) + (val2 *= val2))    
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
0

When write a code you must know what the value of variables.

Label1.Text = "First digit:" & TextBox1.Text.Substring(0, 1)
Label2.Text = "Second digit:" & TextBox1.Text.Substring(1, 1)

Label3.Text = (Label1.Text) ^ 2

Ex : TextBox1 = 12

Now Label1.Text will contain "First digit:1"
So you can't square what in label1 cause it not contain numbers but string.
As Begginnerdev suggest to put value in variables it makes you more easy to handle math operation.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.