Click here to Skip to main content
15,910,787 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
I have textbox1 , textbox2 , textbox3

I need to add the number from textbox1 to number in textbox2 and sum number in textbox3 to number 1 and finally add this result to textbox4 so it would be like this
C#
textbox1    textbox2   textbox3   textbox4

    1           12         5          1126
    4           10         22         41023  
Posted
Updated 15-Dec-15 10:03am
v3
Comments
CHill60 15-Dec-15 16:03pm    
What have you tried?
Sergey Alexandrovich Kryukov 15-Dec-15 16:15pm    
This is not even a question. What have you tried so far?
—SA
[no name] 15-Dec-15 16:39pm    
I think you mix up sum/add and concatenate.

What I see from your example:
You have to concatenate textbox1/2/3, convert it to int, add "1" and convert it back to string to Show it in textbox4

Right or not?

The first thing you need to do is convert the .Text of the TextBoxes to integer[^].

To do this you might be tempted to use ConvertToInt32[^] but consider the fact that these are TextBoxes and you shouldn't rely on the User actually entering a proper number - they might type in "one" instead of 1 for example. So I suggest you use TryParse[^]

Once you have done that you can add the two numbers together then use the ToString()[^] method to put the result into TextBox3.

Now you need to concatenate the two strings from TextBox1.Text and TextBox2.Text - you can do this in a variety of ways ... e.g. the String.Concat[^] method, although I prefer the String.Format[^] method.

You then need to add 1 to the value you calculated from TextBox1 and TextBox2, convert it to a string using the ToString() method mentioned above, and concatenate that value onto the concatenated string you just produced.

Insert that value into TextBox4 (note you do not need to do ToString() again)

Give it a go ... write the code and test it. If you get stuck then come back showing the code that you have tried

Edit - I've just noticed that the presentation of your data is incorrect. It should be (based on your description of the problem):
textbox1    textbox2   textbox3   textbox4
 
    1           12         13          11214
    4           10         14         41015  

Edit 2 - thanks to @0x01AA I see I've made this a little too complicated - however, the conversion information and concatenation information still stands
 
Share this answer
 
v3
Comments
[no name] 15-Dec-15 16:45pm    
See my comment to the Q. Is it not more easy? If not what I'm missing?
CHill60 15-Dec-15 16:51pm    
I read it as having to calculate the sum of textbox1 and textbox2 and then do the concat. I've just edited my solution as the presented results don't match up with the description
[no name] 15-Dec-15 16:57pm    
*lol* I had to read the Q about 20 times and compare it with the example. My _advantage_ was I'm not native and I know therefore, how easy it is to ask something in the wrong way :)
A 5.

[EDIT1]
I let my 5.
But I think you are searchning to far away. I think the request from OP is much more easy.
[/EDIT1]
CHill60 15-Dec-15 17:02pm    
Thank you. And just for the record you prompted me to read it again a few times :)
CHill60 15-Dec-15 17:04pm    
You know what, I've just read it again and get what you mean now. Flippin' 'eck! However, the principles I've given the OP still apply as they will still need to do at least one conversion to add the 1 on (*phew*)
This example allows TextBox 4 to be written to if TextBox 3's Text can be parsed into a valid integer: it will "work" even if TextBox 1 and 2 do not have a valid integer entry.

You can modify this easily to change the behavior; you might want, for example, to check both TextBoxes 1 and 2 for valid integer format Text.

Since making sure the Text in a TextBox can be parsed as an integer, and preventing the user entering non-digit characters can get tricky, I suggest you change to using NumericUpDown Controls
C#
// TextChanged handler for TextBoxes 1~3
private void TextBoxes1To3_TextChanged(object sender, EventArgs e)
{
    TextBox tbx = sender as TextBox;

    int test;

    // only update if textBox3.Text can be parsed into a valid integer
    if(Int32.TryParse(textBox3.Text, out test))
    {
        UpdateTextBox4();
    }
}

private void UpdateTextBox4()
{
    textBox4.Text = string.Format("{0}{1}{2}", 
        textBox1.Text, 
        textBox2.Text,
        (Convert.ToInt32(textBox3.Text) + 1));
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900