Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Homework
how to multiply value of two textboxs into the third text box at runtime
Posted 21-Mar-11 6:43am
AAHLONE200
Edited 22-Mar-11 14:46pm
v2
Comments
code in play at 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
good
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.
 
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.
  Permalink  
v3
Comments
Olivier Levrey at 21-Mar-11 11:50am
   
I voted 5. I just added the missing ToString.
Mika Wendelius at 21-Mar-11 11:56am
   
Thanks :)
Aijaz0787 at 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 at 21-Mar-11 12:03pm
   
Did you read the addition I wrote, use a feasible event and drop the code into it.
Olivier Levrey at 21-Mar-11 11:55am
   
OP said: i want it without pressing any thing
Mika Wendelius at 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 at 21-Mar-11 12:49pm
   
i want it without postback
EdMan196 at 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
Mika Wendelius at 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
good
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;
  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
  Permalink  
Comments
Marcus Kramer at 22-Mar-11 20:49pm
   
+5. Just what is asked for. (I Missed the web comment earlier.)
jaimik patel at 21-Dec-12 8:12am
   
How to use same concept in Repeater Control
Rate this: bad
good
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()
{
   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...
   }
}
  Permalink  
Comments
code in play at 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 at 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
0 Maciej Los 315
1 OriginalGriff 273
2 Aajmot Sk 224
3 Marcin Kozub 210
4 Richard MacCutchan 200
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 22 Mar 2011
Copyright © CodeProject, 1999-2014
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