gpt4 book ai didi

winforms - 如何判断 WinForms 控件的属性是否为 "default"值?

转载 作者:行者123 更新时间:2023-12-03 06:24:00 25 4
gpt4 key购买 nike

如何判断 WinForms Control属性是否为“默认”值?

注意:在设计时,当属性变为粗体时,您可以看出该属性不再是“默认”属性。

Here is an example (at design-time) of a label's font that is the "default" value (which means it gets its value from its parent):

enter image description here

(and if the parent font changes, this label's font changes)

Here a label's font has been changed, is no longer the default value:

enter image description here

(if the parent font changes, this label's font doesn't change)

A careful observer will note that these two labels on the same panel are the "same font"; except one is default and the other isn't.

注意:我需要在运行时知道属性是否不是“默认”值。 (有人可能会作弊并说:“查看属性窗口。如果该属性是粗体,您就知道它不再是默认值”。)

注意:注意,我的问题不限于标签的字体 - 这只是我使用的示例。我可能想知道它是否仍然是默认的其他属性的示例:

  • 前景色
  • 背景颜色
  • ToolStrip.ImageScalingSize
  • SplitContainer.FixedPanel
<小时/>

我陷入疯狂的尝试

我将把这些失败的随机乱码呕吐尝试留在此处:

internal static Boolean GetIsPropertyDefault(object o, string propertyName)
{
return false;

/* Debug.WriteLine(String.Format("GetIsPropertyDefault: {0}.{1} = {2}", o, propertyName, TypeDescriptor.GetProperties(o)[propertyName].GetValue(o)));

// Gets the attributes for the property.
AttributeCollection attributes = TypeDescriptor.GetProperties(o)[propertyName].Attributes;

DefaultValueAttribute myAttribute = (DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];

Debug.WriteLine(String.Format("The default value is: {0}", myAttribute));

if (myAttribute == null)
return true;

return (TypeDescriptor.GetProperties(o)[propertyName].GetValue(o) == myAttribute.Value);
*/

// return (myAttribute.Value == null);



// Console.WriteLine("The default value is: " + myAttribute.Value.ToString());

/* System.Reflection.PropertyInfo pi;
try
{
//pi = o.GetType().GetProperty(propertyName).GetCustomAttributes(typeof(DefaultAttribute), false).
}
catch
{
return true;
}
if (pi == null)
return true;

System.Diagnostics.Trace.TraceInformation(pi.Attributes.ToString());
*/

// return false;
/* foreach (Attribute attr in pi.Attributes)
{
if (attr is System.ComponentModel.DefaultValueAttribute)
{
System.ComponentModel.DefaultValueAttribute dv = (System.ComponentModel.DefaultValueAttribute)attr;

}
}
*/
}

最佳答案

通过反射,您可以检查 DefaultValue 属性的属性。 PropertyGrid 使用它来确定什么应该加粗,什么不应该加粗。

要查找的“其他”属性是AmbientValueAttribute

当我这样做时:

List<string> list = new List<string>();
Attribute[] attrs = Attribute.GetCustomAttributes(typeof(Label).GetProperty("Font"));
foreach (Attribute att in attrs) {
list.Add(att.ToString());
}

我有以下属性:

  • System.ComponentModel.AmbientValueAttribute
  • System.Runtime.InteropServices.DispIdAttribute
  • System.Windows.Forms.SRCategoryAttribute
  • System.ComponentModel.LocalizedAttribute
  • System.Windows.Forms.SRDescriptionAttribute

来自AmbientValueAttribute Class :

If a property on a control has ambient behavior, this attribute must be present. Ambient properties query their parent for their value, for example, a Control.Font property or a Control.BackColor property.

Typically, a visual designer uses the AmbientValueAttribute attribute to decide which value to persist for a property. This is usually a value that causes the property to get its value from another source. An example of an ambient value is Color.Empty as the ambient value for the BackColor property. If you have a control on a form and the BackColor property of the control is set to a different color than the BackColor property of the form, you can reset the BackColor property of the control to that of the form by setting the BackColor of the control to Color.Empty.

可能不会让你想做的事情变得更容易,因为 AmbientValue 基本上告诉类查看它的父类。对于像“Font”这样的属性,您可以调用静态函数DefaultFont:

MessageBox.Show(Label.DefaultFont.ToString());

在我的系统上,结果是:

[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0, GdiVerticalFont=False]

关于winforms - 如何判断 WinForms 控件的属性是否为 "default"值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8251732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com