c# - 将控件的大小重置为其默认值

标签 c# .net winforms size controls

如何在 Windows 窗体中将控件的大小重置为其默认值?

我尝试将大小设置为 (-1,-1),但这没有用,尽管高度设置为默认值。

最佳答案

您可以使用反射来获取控件的 DefaultSize 属性。

Size GetDefaultSize(Control ctrl)
{
    PropertyInfo pi = ctrl.GetType().GetProperty("DefaultSize", BindingFlags.NonPublic | BindingFlags.Instance);
    return (Size)pi.GetValue(ctrl, null);
}

myCtrl.Size = GetDefaultSize(myCtrl);

MSDN :

The DefaultSize property represents the Size of the control when it is initially created.

关于c# - 将控件的大小重置为其默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13608961/

相关文章:

c# - 合并两个 IObservable 时的意外行为

c# - DataGridview 搜索 : show only searchresult and hide other rows?

c# - 显示 MessageBox 取消 SupressKeyPress=true

c# - 在 Winforms 中编写代码的最佳和合适方法是什么?

c# - 从 .NET 驱动程序连接到 Cassandra 2.0

.net - 在 .NET 中审核用户身份 - 什么是好的策略?

c# - 从 string[] 构建一个字符串而不使用任何循环

c# - 对象深度克隆实现

c# - 如何配置 Serilog 接收器以记录到 CloudWatch

C# Winforms,没有使用 GrowAndShrink 调整大小的箭头