J. A
J. A

Reputation: 31

C# how do i subtract from a value inside a textbox?

this is what i currently have at the moment. when i enter a value into the uiBasket1000TextBox i want it to subtract that value off of the uiStock1000TextBox. how would i perform such a task?

this.uiBasket1000TextBox.Location = new System.Drawing.Point(75, 55); 
this.uiBasket1000TextBox.Name = "uiBasket1000TextBox"; 
this.uiBasket1000TextBox.Size = new System.Drawing.Size(37, 20); 
this.uiBasket1000TextBox.TabIndex = 1; 
this.uiBasket1000TextBox.Text = "0"

this.uiStock1000TextBox.Enabled = false; 
this.uiStock1000TextBox.Location = new System.Drawing.Point(12, 55); 
this.uiStock1000TextBox.Name = "uiStock1000TextBox"; 
this.uiStock1000TextBox.Size = new System.Drawing.Size(46, 20); 
this.uiStock1000TextBox.TabIndex = 11; 
this.uiStock1000TextBox.TabStop = false; 
this.uiStock1000TextBox.Text = "3238"; 
this.uiStock1000TextBox.TextChanged += new System.EventHandler(this.uiStock1000Text..

Upvotes: 0

Views: 2096

Answers (3)

Brad
Brad

Reputation: 359

I prefer to use Convert rather than Parse.

int num1 = Convert.ToInt32(uiStock1000Textbox.Text);
int difference = num1 - 100;

Upvotes: 0

Alexei - check Codidact
Alexei - check Codidact

Reputation: 23078

You should provide more details about what you have in those textboxes, but the following should get you started:

 Decimal stockValue = Convert.ToDecimal(uiStock1000TextBox.Text);
 Decimal basketValue = Convert.ToDecimal(uiBasket1000TextBox.Text);
 uiStock1000TextBox.Text = (stockValue - basketValue).ToString();

Of course, you should consider value that cannot be converted by using TryParse function. Also, I would also take a look upon binding, as it is highly recommended to work with data, not parse text.

Upvotes: 0

Vitor Rigoni
Vitor Rigoni

Reputation: 573

You get the value, parse it to an Int/Double/Decimal, perform the subtraction and then set it back as the textbox text.

var value = int.Parse(uiStock1000Textbox.Text);
uiStock1000Textbox.Text = (value - 10).ToString();

Upvotes: 1

Related Questions