C#在设计器中交换
我有我的控件。当我更改控件的属性时。我明白了:C#在设计器中交换,c#,.net,winforms,C#,.net,Winforms,我有我的控件。当我更改控件的属性时。我明白了: this.myLabel1.BorderShadow = true; this.myLabel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.myLabel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.myLabel1.BorderShadow = true;
this.myLabel1.BorderShadow = true;
this.myLabel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.myLabel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.myLabel1.BorderShadow = true;
我需要得到这个:
this.myLabel1.BorderShadow = true;
this.myLabel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.myLabel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.myLabel1.BorderShadow = true;
如何在Form.Desinger.cs中自动执行此操作?
如果你说为什么
private bool BorderShadow_ = false;
public bool BorderShadow
{
get
{
return BorderShadow_;
}
set
{
if (Border_Style_ == BorderStyle.FixedSingle)
{
BorderShadow_ = value;
}
else
{
throw new ArgumentOutOfRangeException("BorderShadow", "BorderShadow can be true if BorderStyle=FixedSingle");
}
}
}
您可以查看接口。它允许您在初始化控件时跳过有效性检查。例如(改编自我的一个项目):
如果我没记错的话,VS设计器也会自动为您添加对
BeginInit
和EndInit
的调用。我想它们会按照定义的顺序出现。因此,您可以使用new
old设置进行覆盖:
public new BorderStyle BorderStyle
{
get {return base.BorderStyle;}
set {base.BorderStyle = value;}
}
然后声明您的
BorderShadow
设置。设计器将始终按字母顺序排列属性,这是无法更改的
属性的意义在于它没有副作用,并且可以在任何时间以任何顺序进行更改。这意味着,如果您有多个表示某种复杂状态的属性,并且并非所有组合都有意义,则在切换属性本身时不应报告此错误
因此,要解决这些问题,有两种可能性:
ISupportInitialize
,如果您处于此状态,请小心public class MyControl : Control
{
private bool _BorderShadow;
private BorderStyle _BorderStyle;
public bool BorderShadow
{
get { return _BorderShadow; }
set
{
if(_BorderShadow != value)
{
_BordeShadow = value;
ApplyBorderShadowIfNeeded();
}
}
}
public BorderStyle BorderStyle
{
get { return _BorderStyle; }
set
{
if(_BorderStyle != value)
{
_BorderStyle = value;
ApplyBorderShadowIfNeeded();
}
}
}
private void ApplyBorderShadowIfNeeded()
{
if(_BorderStyle == BorderStyle.FixedSingle
&& _BorderShadow)
{
// ToDo: Apply the shadow to the border.
}
}
}
你想要什么
和你得到什么
有什么区别?交换Desinger属性。这样做的目的是什么?如果BorderStyle
未设置为BorderStyle.fixedsSingle
?@AndrewSun,为什么不忽略BorderShadow
。