使用单一方法更改所选实体的 属性 的最简洁方法? C#
Cleanest way to change a property of the selected entity using a single method? C#
我有兴趣在所有按钮上使用相同的鼠标悬停效果(特别是更改背景颜色)。但是,我确信有比使用更明智的方法:
private void myButton_Enter(object sender, EventArgs e)
{
myButton.BackColor = Color.Blue;
}
我的每个按钮。有没有一种方法可以使用一种方法将这种效果应用到每个按钮?
提出这样的问题对我来说很陌生,所以如果问题具有误导性或措辞错误,请告诉我,以便我进行更正。
谢谢!
将此事件处理程序添加到所有按钮的输入事件中:
private void myButton_Enter(object sender, EventArgs e)
{
((Button)sender).BackColor = Color.Blue;
}
sender
参数是对引发事件的对象的引用 - 在本例中为特定 Button。
如果你想确定发件人确实是一个按钮,你可以使用这样的东西:
private void myButton_Enter(object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null)
button.BackColor = Color.Blue;
}
为所有按钮分配相同的事件,然后使用
private void myButton_Enter(object sender, EventArgs e)
{
((Button)sender).BackColor = Color.Blue;
}
以便触发正确按钮的事件。当您不小心将事件分配给另一个控件时,最好确保发送者是一个按钮,或者您可以将其转换为 Control
,BackColor 派生自
为什么不创建自己的继承 Button 的按钮 class?
这样你就可以只在一个地方处理行为,避免代码重复。
你能做的是:
public void ColorButtons(Button button)
{
button.BackColor = Color.Blue;
}
将此方法添加到每个鼠标悬停事件。在您的情况下,这将是:
private void myButton_Enter(object sender, EventArgs e)
{
ColorButton(myButton);
}
希望对您有所帮助!
像这样创建events
;你的 constructor
是放置它们的好地方。
this.Button1.MouseEnter += new System.EventHandler(this.myButton_Enter);
this.Button2.MouseEnter += new System.EventHandler(this.myButton_Enter);
那么你只需要1个事件。
private void myButton_Enter(object sender, EventArgs e)
{
Button button = (Button) sender;
button.BackColor = Color.Blue;
}
来源: https://msdn.microsoft.com/en-us/library/aa984308%28v=vs.71%29.aspx
您可以创建自定义按钮。
public class MyButton : Button
{
this.BackColor = Color.Blue; // I am not sure this line but
// you can set the background color here.
// then you can use this class as a button
}
您可以按照其他人的建议创建自己的 class 或 Button
并使用它(假设您希望在整个应用程序中而不是在表单上使用此功能)。如果您只想在表单上使用它并且几乎不需要其他一致性,您总是可以创建一个递归函数来筛选表单上的所有控件并动态添加处理程序。例如
public Form1()
{
InitializeComponent();
InitCustomButtons();
}
private void InitCustomButtons()
{
this.InitCustomButtonsRecursive(this);
}
private void InitCustomButtonsRecursive(Control container)
{
if (container is Button)
{
container.Enter += ColorButtonOnEnter;
}
for (var i = 0; i < container.Controls.Count; i++)
{
var control = container.Controls[i];
InitCustomButtonsRecursive(control);
}
}
private void ColorButtonOnEnter(object sender, EventArgs eventArgs)
{
var button = sender as Button;
if (button != null)
{
button.BackColor = Color.Blue;
}
}
但是,如果我理解你想要的意图,你可能想要
- 存储背景色的原色
- 恢复它
例如:
public Form1()
{
InitializeComponent();
InitCustomButtons();
}
private void InitCustomButtons()
{
this.InitCustomButtonsRecursive(this);
}
private Dictionary<Button, Color> _originalColors = new Dictionary<Button, Color>();
private void InitCustomButtonsRecursive(Control container)
{
if (container is Button)
{
container.MouseEnter += ColorButtonEnter;
container.MouseLeave += ColorButtonLeave;
}
for (var i = 0; i < container.Controls.Count; i++)
{
var control = container.Controls[i];
InitCustomButtonsRecursive(control);
}
}
private void ColorButtonLeave(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null)
{
if (_originalColors.ContainsKey(button))
{
button.BackColor = _originalColors[button];
}
}
}
private void ColorButtonEnter(object sender, EventArgs eventArgs)
{
var button = sender as Button;
if (button != null)
{
_originalColors[button] = button.BackColor;
button.BackColor = Color.Blue;
}
}
我有兴趣在所有按钮上使用相同的鼠标悬停效果(特别是更改背景颜色)。但是,我确信有比使用更明智的方法:
private void myButton_Enter(object sender, EventArgs e)
{
myButton.BackColor = Color.Blue;
}
我的每个按钮。有没有一种方法可以使用一种方法将这种效果应用到每个按钮?
提出这样的问题对我来说很陌生,所以如果问题具有误导性或措辞错误,请告诉我,以便我进行更正。
谢谢!
将此事件处理程序添加到所有按钮的输入事件中:
private void myButton_Enter(object sender, EventArgs e)
{
((Button)sender).BackColor = Color.Blue;
}
sender
参数是对引发事件的对象的引用 - 在本例中为特定 Button。
如果你想确定发件人确实是一个按钮,你可以使用这样的东西:
private void myButton_Enter(object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null)
button.BackColor = Color.Blue;
}
为所有按钮分配相同的事件,然后使用
private void myButton_Enter(object sender, EventArgs e)
{
((Button)sender).BackColor = Color.Blue;
}
以便触发正确按钮的事件。当您不小心将事件分配给另一个控件时,最好确保发送者是一个按钮,或者您可以将其转换为 Control
,BackColor 派生自
为什么不创建自己的继承 Button 的按钮 class?
这样你就可以只在一个地方处理行为,避免代码重复。
你能做的是:
public void ColorButtons(Button button)
{
button.BackColor = Color.Blue;
}
将此方法添加到每个鼠标悬停事件。在您的情况下,这将是:
private void myButton_Enter(object sender, EventArgs e)
{
ColorButton(myButton);
}
希望对您有所帮助!
像这样创建events
;你的 constructor
是放置它们的好地方。
this.Button1.MouseEnter += new System.EventHandler(this.myButton_Enter);
this.Button2.MouseEnter += new System.EventHandler(this.myButton_Enter);
那么你只需要1个事件。
private void myButton_Enter(object sender, EventArgs e)
{
Button button = (Button) sender;
button.BackColor = Color.Blue;
}
来源: https://msdn.microsoft.com/en-us/library/aa984308%28v=vs.71%29.aspx
您可以创建自定义按钮。
public class MyButton : Button
{
this.BackColor = Color.Blue; // I am not sure this line but
// you can set the background color here.
// then you can use this class as a button
}
您可以按照其他人的建议创建自己的 class 或 Button
并使用它(假设您希望在整个应用程序中而不是在表单上使用此功能)。如果您只想在表单上使用它并且几乎不需要其他一致性,您总是可以创建一个递归函数来筛选表单上的所有控件并动态添加处理程序。例如
public Form1()
{
InitializeComponent();
InitCustomButtons();
}
private void InitCustomButtons()
{
this.InitCustomButtonsRecursive(this);
}
private void InitCustomButtonsRecursive(Control container)
{
if (container is Button)
{
container.Enter += ColorButtonOnEnter;
}
for (var i = 0; i < container.Controls.Count; i++)
{
var control = container.Controls[i];
InitCustomButtonsRecursive(control);
}
}
private void ColorButtonOnEnter(object sender, EventArgs eventArgs)
{
var button = sender as Button;
if (button != null)
{
button.BackColor = Color.Blue;
}
}
但是,如果我理解你想要的意图,你可能想要
- 存储背景色的原色
- 恢复它
例如:
public Form1()
{
InitializeComponent();
InitCustomButtons();
}
private void InitCustomButtons()
{
this.InitCustomButtonsRecursive(this);
}
private Dictionary<Button, Color> _originalColors = new Dictionary<Button, Color>();
private void InitCustomButtonsRecursive(Control container)
{
if (container is Button)
{
container.MouseEnter += ColorButtonEnter;
container.MouseLeave += ColorButtonLeave;
}
for (var i = 0; i < container.Controls.Count; i++)
{
var control = container.Controls[i];
InitCustomButtonsRecursive(control);
}
}
private void ColorButtonLeave(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null)
{
if (_originalColors.ContainsKey(button))
{
button.BackColor = _originalColors[button];
}
}
}
private void ColorButtonEnter(object sender, EventArgs eventArgs)
{
var button = sender as Button;
if (button != null)
{
_originalColors[button] = button.BackColor;
button.BackColor = Color.Blue;
}
}