我有一些代码表明我的 WinForm 上的字段何时发生了变化。
如果Control是TextBox,我会更改ForeColor和BackColor,具体取决于控件的文本是否等于默认(或初始)值
private void LastName_Changed(object sender, EventArgs e) {
if (sender.Equals(txtLastName)) {
if (emp0.LastName != txtLastName.Text) {
txtLastName.ForeColor = changedTxtFont;
txtLastName.BackColor = changedTxtBack;
btnOK.Enabled = true;
} else {
txtLastName.ForeColor = systemTxtFont;
txtLastName.BackColor = systemTxtBack;
}
AuthenticationReset();
}
}
如果控件是DateTimePicker或ComboBox,我实际上无法执行此操作(没有可见的ForeColor或BackColor),因此我更改了与Control关联的Label。 / p>
private void TrainDate_Changed(object sender, EventArgs e) {
if (sender.Equals(dtTrainDate)) {
DateTime trainDate = Global.SqlDate(emp0.TrainDate);
if (trainDate != dtTrainDate.Value) {
lblTrainDate.ForeColor = changedLblFont;
lblTrainDate.BackColor = changedLblBack;
btnOK.Enabled = true;
} else {
lblTrainDate.ForeColor = systemLblFont;
lblTrainDate.BackColor = systemLblBack;
}
AuthenticationReset();
}
}
我想创建某种Extension Method,它可以改变Control的ForeColor和BackColor,具体取决于Control是什么以及值是否已经改变。
这是一次尝试,但在代码中使用this
无效,并且扩展方法不会出现在我想要使用它们的任何控件上。
public static class ColorChange {
public static Color ForeColorChange(this Color owned, bool changed) {
if (this is TextBox) {
return changed ? Color.Red : SystemColors.WindowText;
} else {
return changed ? Color.Red : SystemColors.ControlText;
}
}
public static Color BackColorChange(this Color owned, bool changed) {
if (this is TextBox) {
return changed ? Color.Yellow : SystemColors.Window;
} else {
return SystemColors.Control;
}
}
}
有没有办法接近我正在尝试做的事情,或者这不是扩展方法的用途?
我想结束这样的事情:
private void TrainDate_Changed(object sender, EventArgs e) {
if (sender.Equals(dtTrainDate)) {
DateTime trainDate = Global.SqlDate(emp0.TrainDate);
if (trainDate != dtTrainDate.Value) {
lblTrainDate.ForeColorChange(true);
lblTrainDate.BackColorChange(true);
btnOK.Enabled = true;
} else {
lblTrainDate.ForeColorChange(false);
lblTrainDate.BackColorChange(false);
}
AuthenticationReset();
}
}
解决方案:
使用下面检查的答案,我写了以下扩展方法:
public static class ColorChange {
public static void AlteredText(this Control owned, bool changed) {
if (owned is TextBox) {
owned.ForeColor = changed ? Color.Red : SystemColors.WindowText;
owned.BackColor = changed ? Color.Yellow : SystemColors.Window;
} else {
owned.ForeColor = changed ? Color.Red : SystemColors.ControlText;
owned.BackColor = SystemColors.Control;
}
}
}
以下是我将如何使用它:
private void TrainDate_Changed(object sender, EventArgs e) {
if (sender.Equals(dtTrainDate)) {
DateTime trainDate = Global.SqlDate(emp0.TrainDate);
if (trainDate != dtTrainDate.Value) {
lblTrainDate.AlteredText(true);
btnOK.Enabled = true;
} else {
lblTrainDate.AlteredText(false);
}
AuthenticationReset();
}
}
我希望其他人也可以使用它。 〜乔。
答案 0 :(得分:4)
您已为Color
类定义了扩展方法,而不是Control
,这就是为什么它赢了; t显示控件。此外,您的方法可能需要检查您需要传递的owned
控件。
答案 1 :(得分:1)
使用owned
代替this
..哦,正如Rowland Shaw所说,Control
代替Color
。
所以你的代码最终应该是
public static Color ForeColorChange(this Control owned, bool changed) {
if (owned is TextBox) {
return changed ? Color.Red : SystemColors.WindowText;
} else {
return changed ? Color.Red : SystemColors.ControlText;
}
}
public static Color BackColorChange(this Control owned, bool changed) {
if (owned is TextBox) {
return changed ? Color.Yellow : SystemColors.Window;
} else {
return SystemColors.Control;
}
}
答案 2 :(得分:1)
这扩展了Control而不是Color(需要在静态类中定义):
public static void ForeColorChange(this Control owned, bool changed) {
if (owned is TextBox) {
owned.ForeColor = changed ? Color.Red : SystemColors.WindowText;
} else {
owned.ForeColor = changed ? Color.Red : SystemColors.ControlText;
}
}
public static void BackColorChange(this Control owned, bool changed) {
if (owned is TextBox) {
owned.BackColor = changed ? Color.Yellow : SystemColors.Window;
} else {
owned.BackColor = SystemColors.Control;
}
}
用法:
TextBox box = ...;
box.ForeColorChange(true);
答案 3 :(得分:1)
如果要在文本框中调用扩展方法,则必须在命名空间的顶层执行此操作:
public static class Extensions
{
public static void myExtensionMethod(this Textbox textbox)
{
//Do what you want to do
}
}
然后可以通过文本框访问它:
Textbox myTextbox = new Textbox();
myTextbox.myExtensionMethod();
如果需要,您还可以将参数传递给扩展方法。