Click here to Skip to main content
14,698,687 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how to multiply value of two textboxs into the third text box at runtime
Posted
Updated 22-Mar-11 14:46pm
v2
Comments
pankajupadhyay29 21-Mar-11 13:47pm
   
try to specify maximum things in question like you are doing this in web application and you want to do this without postback etc.

Taken this is WinForms: For example with decimal values use Decimal.Parse Method[^]. For example:
textBox3.Text = (Decimal.Parse(textBox1.Text) * Decimal.Parse(textBox2.Text)).ToString();

Of course you need to add checks, use try catch, possibly use TryParse instead and so on.

Addition:
You can put the code where anywhere you like. For example you can use the text boxes Validating event, KeyUp, KeyDown etc, whatever is feasible.
   
v3
Comments
Olivier Levrey 21-Mar-11 11:50am
   
I voted 5. I just added the missing ToString.
Wendelius 21-Mar-11 11:56am
   
Thanks :)
AAHLONE 21-Mar-11 12:00pm
   
i mean to say that as i write the value in textboxes the multiply should automaticaly came third column .
Wendelius 21-Mar-11 12:03pm
   
Did you read the addition I wrote, use a feasible event and drop the code into it.
Olivier Levrey 21-Mar-11 11:55am
   
OP said: i want it without pressing any thing
Wendelius 21-Mar-11 12:02pm
   
Yes, thanks, I noticed. I made an addition to the solution. Hopefully the OP will clarify the requirements a bit more if this approach isn't suitable.
AAHLONE 21-Mar-11 12:49pm
   
i want it without postback
Ed Nutting 21-Mar-11 13:21pm
   
Aahh.. you mean to say you are using web based application not Windows Forms Application. In that case, in your Page Load event (presumably where you fill the two texboxes with the values) or wherever you fill the two texboxes place Mika's code as well. This will fill in all three without postback. If you want the user to fill in the boxes and then have the third box filled in without using postback you will have to use javascript. Try using W3Schools.com to help you, they are very good with very good tutorials to get you started and they list all the features/standards of html, javascript, css and the rest :) Hope this helps, Ed
Wendelius 21-Mar-11 15:26pm
   
So it's ASP, not WinForms. In that case as EdMan196 already stated if you need to do the calculation while user enters the input, you'll need a small jJavaScript to handle that on client side.
To do this without postback you should use javascript--
function updateValue()
{
  var val1=document.getElementByID("<%=textbox1.ClientID%>").value;
  var val2=document.getElementByID("<%=textbox2.ClientID%>").value;
  document.getElementByID("<%=textbox3.ClientID%>").value=parseFloat(val1)*parseFloat(val2);
}


now you can call this at any javascript event of your need i am calling this at blur-

write this on pageload
textbox1.Attribute.Add("onblur","updateValue()");
textbox2.Attribute.Add("onblur","updateValue()");



--Pankaj
   
Comments
fjdiewornncalwe 22-Mar-11 20:49pm
   
+5. Just what is asked for. (I Missed the web comment earlier.)
jaimik patel 21-Dec-12 8:12am
   
How to use same concept in Repeater Control
To update the textbox when the user changes the other ones, you just need to copy the code provided by Mika and paste it into a TextChanged event handler:

public YourForm()
{
   InitializeComponent();
   //add the handler for the two input boxes
   textBox1.TextChanged += textbox_TextChanged;
   textBox2.TextChanged += textbox_TextChanged;
}

private void textBox_TextChanged(object sender, EventArgs e)
{
   try
   {
       textBox3.Text = (Decimal.Parse(textBox1.Text) * Decimal.Parse(textBox2.Text)).ToString();
   }
   catch
   {
       //an error occured...
   }
}
   
Comments
pankajupadhyay29 22-Mar-11 0:29am
   
using TextChanged event will cause to postback the page, i think javascript is solution for achiving this without postback(OP says in comment for solution1 that he want it with out postback)
Olivier Levrey 22-Mar-11 4:48am
   
OK thanks for your comment.

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