Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
Hello All,

I want to calculate two text box values in 3rd text box like below...

MRP : 10000 (USER WILL ENTER THIS VALUE)
LESS: 10% (THIS WILL BE DROP DOWN MENU WHERE USER CAN SELECT THE %)
20%
30%
40%
45%
50%

PRICE: 9000 (IN THIS TEXT BOX PRICE AUTOMATIC CALCULATE ON ABOVE SELECTED % )
8000
7000
6000
5500
5000

for eg: IF I HAVE SELECTED 20% THAN IN PRICE TEXT BOX 8000 SHOULD BE DISPLAYED.
IF I HAVE SELECTED 30% THAN IN PRICE TEXT BOX 7000 SHOULD BE DISPLAYED.

Please ant one give me idea how to code this?????
Posted

Try This:-

Create variable in your form. I wrote double because price may in point for eg. 27.50
C#
double a, b,c;

then I dropped 1 combobox, and 2 textbox controls.
then write following code:-
1. First in Combo box selected Index event:-
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedValue != "")
    {
        if (textBox1.Text != "")
        {
            a = Convert.ToDouble(textBox1.Text);
            b = Convert.ToDouble(comboBox1.SelectedItem.ToString());
            c = (a * b) / 100;
            textBox3.Text = (a - c).ToString();
        }
    }
}



2. Then in textbox changedvalue event of textbox 1:-
C#
private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (textBox1.Text != "")
    {
        a = Convert.ToDouble(textBox1.Text);
        b = Convert.ToDouble(comboBox1.SelectedItem.ToString());
        c = (a * b) / 100;
        textBox3.Text = (a - c).ToString();
    }
}


I wrote the same code in Combobox selected index changed event because if the user changes the discount % then the price should be calculated again.

Any query plz reply or comment.
If You Found the answer correct then accept as solution.
 
Share this answer
 
v3
Comments
ns_27 23-Aug-12 8:18am    
Thank you so much for your code. !!!
Ank_ush 26-Aug-12 1:23am    
Welcome Dear..
Ank_ush 26-Aug-12 1:26am    
If my Answer is correct then Accept Solution.
ns_27 3-Sep-12 3:25am    
Thanks For Answer... :)
Ank_ush 3-Sep-12 14:04pm    
Welcome....
On Drop down selected index changed event

C#
textBox1.Text = (int.Parse(textBox1.Text)-((int.Parse(textBox1.Text) * Convert.ToInt32(comboBox1.SelectedValue)) / 100)).ToString();
 
Share this answer
 
Comments
ns_27 20-Aug-12 5:02am    
No calculation has done...
i want like below..
text box1 - 10% = text box2
ns_27 20-Aug-12 5:03am    
Thanks for help but its not working??
plz help me.
Renju Vinod 20-Aug-12 5:07am    
Please send the error which you got
ns_27 20-Aug-12 5:12am    
Nothing is happing no calculation..
I have i text box in which i enter 10000
then i selected % from combo box1 10
i want ans 9000 in my text box2
Renju Vinod 20-Aug-12 6:07am    
Its works fine for me.Please check it using one break point
On the dropdown changed event,

C#
TextBox2.Text=(int.Parse(textBox1.Text.ToString())-(int.Parse(textBox1.Text.ToString())*((int.Parse(comboBox1.SelectedValue.ToString()))/100)).ToString();
 
Share this answer
 
v3
Comments
ns_27 20-Aug-12 7:35am    
Showing below two errors:

Error 1: The best overloaded method match for 'int.Parse(string)' has some invalid arguments
Error 2: Argument '1': cannot convert from 'object' to 'string'
Santhosh Kumar Jayaraman 20-Aug-12 7:51am    
Just add ToString() next to textbox.Text and combobox.SelectedValue.

Check the updated solution
ns_27 23-Aug-12 8:16am    
I wrote this code...
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
txtmop.Text.ToString()=(int.Parse(txtdp.Text.ToString()))-(int.Parse(txtdp.Text.ToString())*((int.Parse(comboBox1.SelectedValue.ToString()))/100)).ToString();
}


Error 1 Operator '-' cannot be applied to operands of type 'int'

This error i am getting.. plz help me to resolve it. Thanks !!
Santhosh Kumar Jayaraman 23-Aug-12 9:43am    
On the left hand side, dont use ToString().
txtmop.Text=
ns_27 24-Aug-12 2:15am    
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
txtmop.Text=(int.Parse(txtdp.Text.ToString()))-(int.Parse(txtdp.Text.ToString())*((int.Parse(comboBox1.SelectedValue.ToString()))/100)).ToString();
}

Still getting same error !!!

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