Home 活动信息 如何以基本形式更改控件属性(FlatStyle)?

如何以基本形式更改控件属性(FlatStyle)?

在baseForm中,您可以将ControlAdded事件挂在要添加Button的Panel上,并通过代码对其进行样式设置。这将适用于从baseForm继承的每个表单。

例如(在baseForm中)

代码语言:javascript复制public partial class BaseForm : Form

{

public BaseForm()

{

InitializeComponent();

// "myPanel" is the panel where the button will be added in inherited forms

myPanel.ControlAdded += myPanel_ControlAdded;

}

private void myPanel_ControlAdded(object sender, ControlEventArgs e)

{

var button = e.Control as Button;

if (button != null)

{

button.FlatStyle = FlatStyle.Flat;

button.ForeColor = Color.Red;

}

}

}做了个非常快速的测试..。它甚至在设计模式下也能工作:

作为另一种选择,如果要在应用程序中的任何地方使用样式较重的按钮,可以考虑创建从Button继承的自定义控件,并在那里分配属性,如下所示:

代码语言:javascript复制public class FlatButton : System.Windows.Forms.Button

{

public FlatButton()

{

FlatStyle = FlatStyle.Flat;

}

}生成后,您将在“工具箱”(“您的项目的组件”选项卡下)中找到它,或者您可以将其插入您自己的控件库(在不同的解决方案中),并将其永久添加到Visual中的工具箱中。