You must Sign In to post a response.
  • Category: .NET

    How Convert String to integer- vb.net

    Hi,
    please I need your help.
    I want to convert a string that is in textBox1.text, which is= "1+2+3" into integer value given result 6.
    I tried to convert it to integer but what actually happened is that it's only taking the first value, which is 1 and convert it to integer.
    can you please help me?

    Private Sub Sum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sum.Click
    TextBox1.Text = TextBox1.Text & "+"

    Answer = Val(TextBox1.Text)
    Answer += Val(TextBox1.Text)
    FinalResult = Answer

    thnx,
  • #194858
    COnvert.ToInt32(TextBox1.Text)

  • #194860
    Hi,
    First declare a string and from that take the contents one by one and assign to another string and then you carry on your process.
    By,
    Dharma

    All is well.
    Thanks,
    Dharma
    Editor,Mentor,MVM
    Try and fail but don't fail and try

  • #194864
    hi,

    use Convert.ToInt()

    -Mohan

  • #194867
    I you are calculating sum using single text box by giving multiple value, then first you need to split the string and then you have convert it to int then calculate, you do like this. before doing calculation you need to validate the string which is entered in the textbox. check below code


    private void button2_Click(object sender, EventArgs e)
    {
    int sum = 0;
    string[] tempValue = textBox3.Text.Split('+');
    for( int i = 0; i < tempValue.Length; i++ ) {
    sum += int.Parse(tempValue[i].ToString());
    }
    MessageBox.Show(sum.ToString());
    }

    --Giri
    http://giribabu.wordpress.com

  • #194871
    Thanks for your reply, but it didn't work because I think I have an addition sign within the string. This is my first time dealing with VB.Net n I'm tryin to build a calculator.. Is there other solution plz

  • #194876
    Thanks for your reply, but it didn't work because I think I have an addition sign within the string. This is my first time dealing with VB.Net n I'm tryin to build a calculator.. Is there other solution plz

  • #194879
    Dim i, x As Integer
    Dim y As Integer
    Dim Str As String = TextBox1.Text

    For y = 0 To Str.Length - 1

    Dim sum As String = Str.Substring(y, 1)
    x = Convert.ToInt32(sum)
    i = i + x


    Next

  • #194889
    Dim s As String
    s = TextBox1.Text
    Dim str(10) As String
    Dim number(10) As Integer
    Dim sum As Integer
    sum = 0


    str = Split(s, "+")
    Dim i As Integer
    For i = 0 To str.Length - 1
    number(i) = CInt(str(i))
    sum += number(i)


    Next
    MessageBox.Show(sum)


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.