Click here to Skip to main content
15,850,298 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.
 
Share this answer
 
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
To do this without postback you should use javascript--
Java
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
C#
textbox1.Attribute.Add("onblur","updateValue()");
textbox2.Attribute.Add("onblur","updateValue()");



--Pankaj
 
Share this answer
 
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:

C#
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...
   }
}
 
Share this answer
 
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