Click here to Skip to main content
15,908,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void tb1_TextChanged(object sender, EventArgs e)
        {



         	 string value1 = string.Empty;
            string value2 = string.Empty;
            string value3 = string.Empty;

            string value4 = string.Empty;
            string value5 = string.Empty;


            TextBox txtFirst = (TextBox)sender;
            
switch (txtFirst)//i got error here

case: "txt0";

           
            TextBox tbval0 = (TextBox)Page.Form.FindControl(("txt0"));
            value1 = tbval0.Text.ToString();
Case: "txt2";
           


            TextBox tbval2 = (TextBox)Page.Form.FindControl(("txt2"));
            value2 = tbval2.Text.ToString();


Case: "txt4";
            TextBox tbval3 = (TextBox)Page.Form.FindControl(("txt4"));
            value3 = tbval3.Text.ToString();


case: "txt5";

            decimal sum = 0;

            sum = Convert.ToDecimal(value1) + Convert.ToDecimal(value2) + Convert.ToDecimal(value3);

            TextBox tbval5 = (TextBox)Page.Form.FindControl(("txt5"));
            tbval5.Text = Convert.ToString(sum);

break;

case; "txt1";

            TextBox tbval4 = (TextBox)Page.Form.FindControl(("txt1"));
            value4 = tbval4.Text.ToString();
case "txt3";

            TextBox tbval6 = (TextBox)Page.Form.FindControl(("txt3"));
            value5 = tbval6.Text.ToString();

case "txt6";
            decimal sum1 = 0;

            sum1 = Convert.ToDecimal(value4) + Convert.ToDecimal(value5);

            TextBox tbval7 = (TextBox)Page.Form.FindControl(("txt6"));
            tbval7.Text = Convert.ToString(sum1);

break;
                       


        }
Posted
Updated 8-Aug-12 15:49pm
v2
Comments
[no name] 8-Aug-12 21:50pm    
And yes you will. You cannot switch on a textbox. Especially when you are trying to switch on the textbox text or maybe name.
Kenneth Haugland 8-Aug-12 21:50pm    
How about txtFirst.Name?
2011999 8-Aug-12 21:57pm    
txtFirst is hear i get the run time textbox id's

Hi,
You are switching TextBox, but in the case statement you are using string to identify the TextBoxes. Try this:
C#
protected void tb1_TextChanged(object sender, EventArgs e)
{ 
    string value1 = string.Empty;
    string value2 = string.Empty;
    string value3 = string.Empty;
    string value4 = string.Empty;
    string value5 = string.Empty;
    TextBox txtFirst = (TextBox)sender;  
    //Find the textboxes here
    TextBox tbval0 = (TextBox)Page.Form.FindControl(("txt0"));  
    TextBox tbval2 = (TextBox)Page.Form.FindControl(("txt2"));
    TextBox tbval3 = (TextBox)Page.Form.FindControl(("txt4"));
    TextBox tbval5 = (TextBox)Page.Form.FindControl(("txt5"));
    TextBox tbval4 = (TextBox)Page.Form.FindControl(("txt1"));
    TextBox tbval6 = (TextBox)Page.Form.FindControl(("txt3"));
    TextBox tbval7 = (TextBox)Page.Form.FindControl(("txt6"));
    switch (txtFirst.ID.ToString()){
        case "txt0":        
            value1 = tbval0.Text.ToString();
            break;
        case "txt2":        
            value2 = tbval2.Text.ToString(); 
            break;
        case "txt4":        
            value3 = tbval3.Text.ToString();    
            break;
        //Put your all cases here..
        default:
            break;
    }
}



--Amit
 
Share this answer
 
v5
Comments
2011999 8-Aug-12 23:54pm    
Error 1 A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type
2011999 9-Aug-12 0:12am    
Error 3 The name 'txt4' does not exist in the current context, txt2,txt0
AmitGajjar 9-Aug-12 0:16am    
Correct5+ , also see my solution for minor modification.
_Amy 9-Aug-12 0:19am    
Thank you Amit. :)
2011999 9-Aug-12 0:22am    
Error 1 A constant value is expected
Hi,

Try the solution given in the solution1 by _Amy. you also need to check if your textbox tbval1,tbval2,.... all are not null. If it is null it means it is not able to find that control and you can throw error message.


Otherwise solution 1 is correct.

Thanks
-Amit Gajjar
 
Share this answer
 

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