问题描述
嗨
我正在尝试创建一个程序,单击确定按钮将移动到下一个文本框。我认为我可以使用if语句和第一个if语句来实现这一点,但是当添加第二个和第三个if语句时解决方案崩溃了。
我需要的是一种模仿休息的方法。在switch语句中或更好地将我的if转换为switch。我试图这样做,但我不知道我应该在开关支架中添加什么变量,因为Textbox是一种类型,因此在我需要的上下文中无效。
如果有人对如何纠正或解决这个问题有任何想法,我将非常感谢您提供的任何帮助。
谢谢。
我的尝试:
当前代码:
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;
}
推荐答案
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;
}
因为如果第一种情况通过,它会设置一个条件,使第二种情况通过(通过启用txt_Minutes),它设置一个使第三种情况通过的条件 - 所以无论哪一种条件它开始,它总是结束相同:所有禁用。
你可以做到这一点,否则
条件:
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:
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...
}
但是你可以使用开关
可以轻松实现,因为switch表达式只能用于sbyte,byte,short,ushort,int,uint,long,ulong,char,string或enum值和case语句仅适用于常量值。
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.
这篇关于你能创建用于更改文本框的switch语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!