在设计时限制用户控件的大小

发布于 2024-07-25 07:03:18 字数 406 浏览 4 评论 0原文

我有一个命名空间 Company.UI.Forms,其中有一个继承自 System.Windows.Forms.Form 的表单基类 BaseForm

我需要限制此表单的大小,以便如果具体表单(例如 ExampleForm)派生自 BaseForm,它在 VS 设计器视图中应该具有固定大小。 固定大小应该以某种方式设置或定义(在BaseForm ctor中?),以便派生的ExampleForm和设计者选择它。

那可能吗?

[编辑] 目的是使用基类作为已知屏幕尺寸的 Windows CE 设备的全屏表单的模板,为此我希望能够在设计器中布置子控件。

I have a namespace Company.UI.Forms where we have a form base class BaseForm that inherits from System.Windows.Forms.Form.

I need to limit the size of this form, so that if a concrete form, say ExampleForm, derives from BaseForm, it should have a fixed size in the VS designer view. The fixed size should be set or defined in some way (in the BaseForm ctor?) so that the derived ExampleForm and the designer picks it up.

Is that possible?

[Edit] The purpose is to use the base class as a template for a full screen form for a Windows CE device of known screen size, for which I want to be able to lay out child controls in the designer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栀梦 2024-08-01 07:03:18

在标准 WinForms 中,您可以重写 SetBoundsCore 方法。 此代码将阻止基类在设计时更改它...但请记住它可以被任何继承的类覆盖:无论如何,

protected override void SetBoundsCore(int x, int y,
     int width, int height, BoundsSpecified specified)
{
  if (this.DesignMode)
    base.SetBoundsCore(x, y, 500, 490, specified);
  else
    base.SetBoundsCore(x, y, width, height, specified);
}

我已经放置了 if 因为您指定在设计时,如果您不想要一个在运行时调整大小并删除它。

In standard WinForms you can override the SetBoundsCore method. This code will prevent the base classes to change it at design time... but remember it can be overriden by any inherited class anyway:

protected override void SetBoundsCore(int x, int y,
     int width, int height, BoundsSpecified specified)
{
  if (this.DesignMode)
    base.SetBoundsCore(x, y, 500, 490, specified);
  else
    base.SetBoundsCore(x, y, width, height, specified);
}

I've put the if because you specified to be at design time, if you don't want a resize at runtime as well remove it.

寂寞花火° 2024-08-01 07:03:18

好的,现在对于 CF,快速解决方案,您可以通过将 Size 存储在字段中来编写更清晰的代码......

public BaseForm()
{
 InitializeComponent();
 this.Size=new Size(20,20);
}


bool resizing;

protected override void OnResize(EventArgs e)
{   
 if(!resizing)
 {
   resizing = true;
   try
   {
     this.Size = new Size(20, 20);
   }
   finally
   {
     resizing = false;
   }
 }
 else
   base.OnResize(e);
}

Ok, now for CF, quick solution, you may do a cleaner code by storing Size in a field...

public BaseForm()
{
 InitializeComponent();
 this.Size=new Size(20,20);
}


bool resizing;

protected override void OnResize(EventArgs e)
{   
 if(!resizing)
 {
   resizing = true;
   try
   {
     this.Size = new Size(20, 20);
   }
   finally
   {
     resizing = false;
   }
 }
 else
   base.OnResize(e);
}
清风不识月 2024-08-01 07:03:18

我不认为这种情况有任何直接支持。 控制表单上最大大小的所有值都可以被子表单覆盖。 因此,没有办法从属性角度对其进行限制。

我能想到的唯一方法是重写尺寸更改方法,并将尺寸重置为最大尺寸(如果超过)。 但这可能会产生一些不好的影响,在您决定将其作为解决方案之前,您应该先使用它。

I don't believe there is any direct support for this type of scenario. All of the values which control the maximum size on a Form can be overridden by a child form. Hence there is no way to constrain it from a property perspective.

The only way I can think of is to override the size changed method and reset the size to the max size if it ever goes over. This may have some bad effects though and you should expirement with it before you settle on it as a solution.

‖放下 2024-08-01 07:03:18

假设我们在用户控件中有一个名为“button1”的按钮。
我希望在设计时更改用户控件大小时,button1 的大小可以根据用户控件的大小进行调整。

该解决方案工作属性。

在用户控件的调整大小事件中写入此内容。

 private void ButtonDesigned_Resize(object sender, EventArgs e)
    {            
        button1.Size = new Size(this.Size.Width - 2, this.Size.Height - 2); 

         // Note :  this.size hint to usercontrol size 

         or

        button1.Size = this.Size
    }

我的英语不好。我希望这个解决方案对你来说足够了。

assuming we have a button that it name be "button1" in a usercontrol.
I want،while change usercontrol size at design time،the size of button1 be adjaust with the size of usercontrol.

this solution work property.

at the Resize event of usercontrol write this.

 private void ButtonDesigned_Resize(object sender, EventArgs e)
    {            
        button1.Size = new Size(this.Size.Width - 2, this.Size.Height - 2); 

         // Note :  this.size hint to usercontrol size 

         or

        button1.Size = this.Size
    }

my english language not good.i hoop this solution be sufficient for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文