Click here to Skip to main content
11,928,078 members (50,248 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
how to multiply value of two textboxs into the third text box at runtime
Posted 21-Mar-11 6:43am
Edited 22-Mar-11 14:46pm
code in play 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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.
Olivier Levrey 21-Mar-11 11:50am
I voted 5. I just added the missing ToString.
Mika Wendelius 21-Mar-11 11:56am
Thanks :)
Aijaz0787 21-Mar-11 12:00pm
i mean to say that as i write the value in textboxes the multiply should automaticaly came third column .
Mika 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
Mika 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.
Aijaz0787 21-Mar-11 12:49pm
i want it without postback
EdMan196 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 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
Mika 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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;

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

write this on pageload

Marcus Kramer 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
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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()
   //add the handler for the two input boxes
   textBox1.TextChanged += textbox_TextChanged;
   textBox2.TextChanged += textbox_TextChanged;
private void textBox_TextChanged(object sender, EventArgs e)
       textBox3.Text = (Decimal.Parse(textBox1.Text) * Decimal.Parse(textBox2.Text)).ToString();
       //an error occured...
code in play 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 22 Mar 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100