Link to home
Start Free TrialLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on

take away digits..

hello,
I have inside a textbox a number "10"

I have two string one with "16523"
and another one with "1188"

based on the textbox how can I take away 10 from the first string to display "16513"
and take away 10 from second string displaying like this "1187.9"

maybe with String.Format ?
Avatar of kaufmed
kaufmed
Flag of United States of America image

e.g.

int x, y, z;

x = Convert.ToInt32(textBox1.Text);
y = Convert.ToInt32(textBox2.Text);
z = Convert.ToInt32(textBox3.Text);

x -= z;
y -= z;

textBox1.Text = x.ToString();
textBox2.Text = y.ToString();

Open in new window

If it possible that either string is not a number then do not forget error handling when converting. Look at using TryParse

for example

int  x
bool result = Int32.TryParse(textBox1.Text, out x)
if (!result) {
   // do something to handle error
}

Open in new window


For more info look at
http://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx

Michael
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XK8ER

ASKER

thanks a lot!
using the onchange event
using System;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

   
        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            double x, y, z;
            x = Convert.ToDouble(textBox1.Text);
            y = Convert.ToDouble(textBox2.Text);
            z = Convert.ToDouble(textBox3.Text);
            x -= z;
            y -= (z / 100);
            textBox1.Text = x.ToString();
            textBox2.Text = y.ToString();
        }
    }
}

Open in new window

https://drive.google.com/file/d/0B542q8KsuQuEVjdpdHEzaF9fQ1E/edit?usp=sharing
Interesting answer selection considering it's effectively a clone of my answer...
Avatar of XK8ER

ASKER

(z / 100);

is what I was looking for..
XK8ER do not forget that if a non-numeric entry is made in the textbox then the code will throw an error
Well, since you wrote:

and take away 10 from second string displaying like this "1187.9"

...instead of:

and take away .10 from second string displaying like this "1187.9"

...I'm sure you can appreciate why it was unclear what you wanted. Oh well.