C# 如何检查复选框是否已选中,且不带“";“如果”语句;?
因此,基本上,如果选中复选框,我想将C# 如何检查复选框是否已选中,且不带“";“如果”语句;?,c#,C#,因此,基本上,如果选中复选框,我想将标签1的字体设置为粗体,如果不选中则设置为常规,但如果没有if语句,我想不出该如何操作。 这就是我现在使用的if语句 private void checkBoxBold_CheckedChanged(object sender, EventArgs e) { if (checkBoxBold.Checked == true) label1.Font = new Font(label1.Font
标签1
的字体设置为粗体,如果不选中则设置为常规,但如果没有if
语句,我想不出该如何操作。
这就是我现在使用的if
语句
private void checkBoxBold_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxBold.Checked == true)
label1.Font = new Font(label1.Font, FontStyle.Bold);
else
label1.Font = new Font(label1.Font, FontStyle.Regular);
}
您可以使用以下选项:
label1.Font = checkBoxBold.Checked ? new Font(label1.Font, FontStyle.Bold) : new Font(label1.Font, FontStyle.Regular);
或者如果你想让它像你写的那样:
label1.Font = (checkBoxBold.Checked == true) ? new Font(label1.Font, FontStyle.Bold) : new Font(label1.Font, FontStyle.Regular);
如果之前的声明?如果为真,它将返回的左侧;否则它将返回的右侧
或者像@deadManN说的:
label1.Font = new Font(label1.Font, (checkBoxBold.Checked ? FontStyle.Bold : FontStyle.Regular));
您可以使用以下选项:
label1.Font = checkBoxBold.Checked ? new Font(label1.Font, FontStyle.Bold) : new Font(label1.Font, FontStyle.Regular);
或者如果你想让它像你写的那样:
label1.Font = (checkBoxBold.Checked == true) ? new Font(label1.Font, FontStyle.Bold) : new Font(label1.Font, FontStyle.Regular);
如果之前的声明?如果为真,它将返回的左侧;否则它将返回的右侧
或者像@deadManN说的:
label1.Font = new Font(label1.Font, (checkBoxBold.Checked ? FontStyle.Bold : FontStyle.Regular));
既然我们做事很辛苦,这个怎么样
label1.Font = new Font
(
label1.Font,
new Dictionary<bool, FontStyle>
{
{ true, FontStyle.Bold },
{ false, FontStyble.Regular}
}[checkBoxBold.Checked]
);
label1.Font=新字体
(
标签1.字体,
新词典
{
{true,FontStyle.Bold},
{false,FontStyble.Regular}
}[checkBoxBold.选中]
);
既然我们做事很辛苦,这个怎么样
label1.Font = new Font
(
label1.Font,
new Dictionary<bool, FontStyle>
{
{ true, FontStyle.Bold },
{ false, FontStyble.Regular}
}[checkBoxBold.Checked]
);
label1.Font=新字体
(
标签1.字体,
新词典
{
{true,FontStyle.Bold},
{false,FontStyble.Regular}
}[checkBoxBold.选中]
);
在一个包含如此多的if-else检查的系统中,这些检查被绑定在一起并位于不同的位置,但工作状态相同,有一种方法可以减少if的数量
为此,您可以使用策略或状态模式(我希望我没有弄错模式的名称):
状态是指当您的状态位于您正在处理的对象内部时,在策略中,您使用的系统可以是任何东西
例如:
你认为你有多种写作方式:
A.粗体,尺寸为18px,黑色,字体为xxx-我们称之为页眉
B.大小为12px的常规字体和带有yyy字体的蓝色-我们称之为文本
C.斜体,大小为14,红色,zzz字体-我们称之为引号
在这种情况下,我知道这些是固定的、定义的,并在不同的位置调用(这意味着多个if
s),我将执行以下操作:
首先创建一个接口,定义书写方式:
注意:字体数据类型在我的脑海中形成,可能无效
然后根据此合同/界面创建您的写作风格:
public class HeaderWritingStyle : IWritingStyle {
public Style Style => Style.Bold;
public int Size => 18;
public string FontName => "xxx";
public Color Color => Color.Black;
}
public class TextWritingStyle : IWritingStyle {
public Style Style => Style.None;
public int Size => 12;
public string FontName => "yyy";
public Color Color => Color.Blue;
}
public class QuoteWritingStyle : IWritingStyle {
public Style Style => Style.Italic;
public int Size => 14;
public string FontName => "zzz";
public Color Color => Color.Red;
}
现在,在您的系统中,您可以按照所需的方式设置writingStyle,或者从config或shared类中读取它,例如,一种简单的方法可以是:
Class MyApp {
private IWritingStyle _writingStyle;
public MyApp()
{
SetWritingStyle(new HeaderWritingStyle());
}
public void SetWritingStyle(IWritingStyle style){
this.WritingStyle
}
....
然后你会这样使用它:
private void On_X_Event(event ...)
{
ChangeTextColor(writingStyle.Color);
}
private void On_Y-Event(event ...)
{
ChangeTextSize(writingStyle.Size);
}
你可以通过改变写作风格来改变整个系统的写作风格,如果你需要限制它,只要你决定你的风格,你就会做一次
SetWritingStyle( textObj.IsQuote? new QuoteWritingStyle() : new TextWritingStyle() )
我知道这里的示例不是很实用,因为您希望所有这些样式都放在一起,或者不放在一起,系统中没有多少人希望这些属性一个接一个地出现,大多数情况下都是要么全有,要么什么都没有。但考虑一下,例如一个Car,一个类,它执行多个操作,每个操作系统不同,您为该操作系统创建了一次特定的所需对象,并在应用程序生命周期中开始使用它,之后不调用一个if。在一个有很多if-else检查的系统中,它们被绑定在一起,在不同的地方,但朝着相同的状态工作,有一种方法可以减少
if
s的数量
为此,您可以使用策略或状态模式(我希望我没有弄错模式的名称):
状态是指当您的状态位于您正在处理的对象内部时,在策略中,您使用的系统可以是任何东西
例如:
你认为你有多种写作方式:
A.粗体,尺寸为18px,黑色,字体为xxx-我们称之为页眉
B.大小为12px的常规字体和带有yyy字体的蓝色-我们称之为文本
C.斜体,大小为14,红色,zzz字体-我们称之为引号
在这种情况下,我知道这些是固定的、定义的,并在不同的位置调用(这意味着多个if
s),我将执行以下操作:
首先创建一个接口,定义书写方式:
注意:字体数据类型在我的脑海中形成,可能无效
然后根据此合同/界面创建您的写作风格:
public class HeaderWritingStyle : IWritingStyle {
public Style Style => Style.Bold;
public int Size => 18;
public string FontName => "xxx";
public Color Color => Color.Black;
}
public class TextWritingStyle : IWritingStyle {
public Style Style => Style.None;
public int Size => 12;
public string FontName => "yyy";
public Color Color => Color.Blue;
}
public class QuoteWritingStyle : IWritingStyle {
public Style Style => Style.Italic;
public int Size => 14;
public string FontName => "zzz";
public Color Color => Color.Red;
}
现在,在您的系统中,您可以按照所需的方式设置writingStyle,或者从config或shared类中读取它,例如,一种简单的方法可以是:
Class MyApp {
private IWritingStyle _writingStyle;
public MyApp()
{
SetWritingStyle(new HeaderWritingStyle());
}
public void SetWritingStyle(IWritingStyle style){
this.WritingStyle
}
....
然后你会这样使用它:
private void On_X_Event(event ...)
{
ChangeTextColor(writingStyle.Color);
}
private void On_Y-Event(event ...)
{
ChangeTextSize(writingStyle.Size);
}
你可以通过改变写作风格来改变整个系统的写作风格,如果你需要限制它,只要你决定你的风格,你就会做一次
SetWritingStyle( textObj.IsQuote? new QuoteWritingStyle() : new TextWritingStyle() )
我知道这里的示例不是很实用,因为您希望所有这些样式都放在一起,或者不放在一起,系统中没有多少人希望这些属性一个接一个地出现,大多数情况下都是要么全有,要么什么都没有。但考虑一下,例如一辆汽车,一个执行多种操作的类,每个操作系统的操作不同,您为该操作系统创建了一次特定的所需对象,并开始在应用程序生命周期中使用它,在思考和学习了有关C的新事物之后,我想出了一个新主意,如何在没有
if
语句的情况下做到这一点。
发送方
发送一个字符串
,其中声明了复选框
的检查状态
。
字符串如下所示:
System.Windows.Forms.Checkbox, CheckState: 1
了解了字符串和Fontstyle枚举之后,我所要做的就是获得CheckState
分配的编号,该编号显示复选框是否选中,并使用它设置标签1
的Fontstyle
。
我就是这样做的:
label1.Font=新字体(label1.Font,((FontStyle)Convert.ToInt32(sender.ToString().Substring(sender.ToString().Length-1)));
在这个地方