Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am trying to create a program that when the OK button is clicked will move onto the next text box. I thought I could do this using if statements and the first if statement worked however, when adding the second and third if statements the solution fell apart.

What I need is a way to emulate "break;" in a switch statement or better yet convert my "if's" into "switches". I have attempted to do so, but I don't know what variable I should be putting in the switch bracket since "Textbox" is a type and so not valid in the context I require.

If anyone has any ideas on how I could correct this or work around it I would be grateful for any help you could offer.

Thank you.

What I have tried:

current code:
private void btn_OK_Click(object sender, EventArgs e)
        {
            if (txt_Hours.Enabled == true)
            {
                txt_Hours.Enabled = false;
                txt_Minutes.Enabled = true;
                txt_Seconds.Enabled = false;
            } 
                                   
            if (txt_Minutes.Enabled == true)
            {
                txt_Hours.Enabled = false;
                txt_Minutes.Enabled = false;
                txt_Seconds.Enabled = true;
            }

            if (txt_Seconds.Enabled == true)
            {
                txt_Hours.Enabled = false;
                txt_Minutes.Enabled = false;
                txt_Seconds.Enabled = false;
            }


attempted code:

private void btn_OK_Click(object sender, EventArgs e)
        {
            switch (TextBox)
            {
                case txt_Hours.Enabled == true:
                    txt_Hours.Enabled = false;
                    txt_Minutes.Enabled = true;
                    txt_Seconds.Enabled = false;
                    break;

                case txt_Minutes.Enabled = true:
                    txt_Hours.Enabled = false;
                    txt_Minutes.Enabled = false;
                    txt_Seconds.Enabled = true;
                    break;

                case txt_Seconds.Enabled = true:
                    txt_Hours.Enabled = false;
                    txt_Minutes.Enabled = false;
                    txt_Seconds.Enabled = false;
                    break;
            } 
Posted
Updated 17-Apr-17 3:00am
Comments
Richard MacCutchan 17-Apr-17 9:07am    
Why are you messing around with multiple text boxes? Use a single box for the complete time, or better still use a standard control: How to: Display Time with the DateTimePicker Control[^].

1 solution

Start by thinking about what you want to do. This won't work:
C#
if (txt_Hours.Enabled == true)
            {
                txt_Hours.Enabled = false;
                txt_Minutes.Enabled = true;
                txt_Seconds.Enabled = false;
            } 
            if (txt_Minutes.Enabled == true)
            {
                txt_Hours.Enabled = false;
                txt_Minutes.Enabled = false;
                txt_Seconds.Enabled = true;
            }
            if (txt_Seconds.Enabled == true)
            {
                txt_Hours.Enabled = false;
                txt_Minutes.Enabled = false;
                txt_Seconds.Enabled = false;
            }
Because if the first case passes, it sets a condition which makes the second case pass (by enabling txt_Minutes), which sets a condition which makes the third case pass - so regardless of which condition it start in, it always ends up teh same: All disabled.

You can do it, with else conditions:
C#
if (txt_Hours.Enabled == true)
{
    txt_Hours.Enabled = false;
    txt_Minutes.Enabled = true;
    txt_Seconds.Enabled = false;
}
else if (txt_Minutes.Enabled == true)
{
    txt_Hours.Enabled = false;
    txt_Minutes.Enabled = false;
    txt_Seconds.Enabled = true;
}
else if (txt_Seconds.Enabled == true)
{
    txt_Hours.Enabled = false;
    txt_Minutes.Enabled = false;
    txt_Seconds.Enabled = false;
}
else
{
    // Dunno what you want to do here...
}

But you can't do that easily with a switch because switch expressions only ever work with sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or enum values, and case statements only work with constant values.
 
Share this answer
 
v2

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