如何调整我构建的自定义控件的大小

发布于 2024-10-14 20:03:51 字数 575 浏览 3 评论 0原文

我使用以下代码调整自定义控件的大小:

private void Form1_Resize_1(object sender, EventArgs e)
{

    textBox1.Text = this.Width.ToString();
    textBox2.Text = (this.Height - 200).ToString();

   canvas21.Size = new System.Drawing.Size(this.ClientSize.Width,  this.ClientSize.Height - this.Top - 15);

    canvas21.Invalidate();

}

我只希望自定义控件的左上角 (0, 105) 保持在原位,并且控件随表单一起调整大小,但由于某种原因,这并没有改变工作。当我调整大小时,控件保持在原位,但会自动调整大小以填写表单的其余部分。

表单布局概述

有没有办法获取影响用户控件大小的所有内容的列表。为了搜索我设置的大小的其他地方,我可能忽略了哪些地方?

I'm resizing my custom control using the following code:

private void Form1_Resize_1(object sender, EventArgs e)
{

    textBox1.Text = this.Width.ToString();
    textBox2.Text = (this.Height - 200).ToString();

   canvas21.Size = new System.Drawing.Size(this.ClientSize.Width,  this.ClientSize.Height - this.Top - 15);

    canvas21.Invalidate();

}

I just want the top left corner of the custom control(0, 105) to stay in place, and the control to resize along with the form, but for some reason this does not work. When I resize the control stays in place, but automatically resizes to fill out the rest of the form.

Overview of form layout

Is there any way to get a list of everything that affects the size of a usercontrol. In order to search for other places the size i set, which i might have overlooked?

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

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

发布评论

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

评论(6

分開簡單 2024-10-21 20:03:51

如果您希望控件始终随表单调整大小,可以使用 Anchor 属性将其设置为锚定到表单的一侧或多侧,这样您就不必拥有自己的调整大小代码(假设您使用此方法获得的标准调整大小功能适合您的需求)。

您必须确保Dock 属性,否则它可能会填充整个表单(或表单的一侧,具体取决于设置)。

If you want your control to always resize with the form, you can use the Anchor property to set it to be anchored to one or more sides of the form so that you don't have to have your own resizing code (assuming that the standard resizing functionality you get with this fits your needs).

You have to make sure that the Dock property isn't set though, otherwise it might fill the whole form (or one side of the form depending on the setting).

霓裳挽歌倾城醉 2024-10-21 20:03:51

确实没有理由设置高度然后锚定到每一侧(对接设置为无)不应该工作。

但是,我确实注意到您提供的代码的逻辑有错误。你有:

canvas21.Size = new System.Drawing.Size(this.ClientSize.Width,  this.ClientSize.Height - this.Top - 15);

当它实际上应该是:

canvas21.Size = new System.Drawing.Size(this.ClientSize.Width,  this.ClientSize.Height - this.canvas21.Top - 15);

你只是采取了形式的顶部,而不是canvas21的顶部,这正是你所需要的。

至少从我的角度来看,这似乎正是您想要的。如果它不太有效,是否根本没有为您调整大小,或者是否调整到了错误的大小?

There's really no reason why setting the height and then anchoring to every side (with docking set to none) shouldn't work.

However, I did notice an error in the logic of your provided code. You have:

canvas21.Size = new System.Drawing.Size(this.ClientSize.Width,  this.ClientSize.Height - this.Top - 15);

When it should actually be:

canvas21.Size = new System.Drawing.Size(this.ClientSize.Width,  this.ClientSize.Height - this.canvas21.Top - 15);

You were just taking the top of the form, rather than the top of canvas21, which is what you need.

That seems to do exactly what you want, at least from my standpoint. If it doesn't quite work, is it simply not resizing at all for you or is it resizing to the wrong size?

冷弦 2024-10-21 20:03:51

如果可以设置 Anchor 属性,那么使用 Anchor 属性是更好的方法。要锁定的是

left, top, right, bottom (all!)

Anchor,让您可以控制已锁定一侧的 x 个单位,以便它与两侧的距离始终相同。

如果这不是一个选项,或者只是为了让您的代码运行,请尝试此方法

private void Form1_Resize_1(object sender, EventArgs e)
{
    textBox1.Text = this.Width.ToString();
    textBox2.Text = (this.Height - 200).ToString();

    int iTop = canvas21.Top;
    int iLeft = canvas21.Left;
    // - 200 - iTop keeps it 200 from the bottom, -iLeft keeps i stuck to right
    canvas21.Size = new System.Drawing.Size(this.Width -iLeft, this.Height-200 -iTop);
    canvas21.Left = iLeft;   // move back
    canvas21.Top = iTop;   // move back
    canvas21.Invalidate();
}

要使其底部 15 像素,请将上面的 200 更改为 15代码>.或者,在设计时或通过代码将其设置为 15,然后激活 bottom 锚点。

Using the Anchor property is the better way to go if you can set it. The ones to lock are

left, top, right, bottom (all!)

Anchor keeps you control x units form the side that you have locked, so that it is always the same distance from the sides.

If that is not an option, or just to get your code going, try this

private void Form1_Resize_1(object sender, EventArgs e)
{
    textBox1.Text = this.Width.ToString();
    textBox2.Text = (this.Height - 200).ToString();

    int iTop = canvas21.Top;
    int iLeft = canvas21.Left;
    // - 200 - iTop keeps it 200 from the bottom, -iLeft keeps i stuck to right
    canvas21.Size = new System.Drawing.Size(this.Width -iLeft, this.Height-200 -iTop);
    canvas21.Left = iLeft;   // move back
    canvas21.Top = iTop;   // move back
    canvas21.Invalidate();
}

To keep it 15 pixels in the bottom, change 200 above to 15. Or, set it at 15 at design time or via code, the activate the bottom anchor.

末骤雨初歇 2024-10-21 20:03:51

对于自定义控件以外的控件,您能否正确调整其大小?例如,如果您放入一个 Richtextbox 并相应地设置锚点(全部),它会为您正确调整大小吗?如果确实如此,那么您的控件的大小一定有问题,但是您可能可以通过这样做来欺骗它...

  1. 创建一个大小精确的面板,并从您的控件中锚定您想要的面板。
  2. 将控件添加到面板并将 Dock 设置为“填充”。实际上,您将使用控件填充面板,并使用面板来控制表单上的大小调整。

当然,如果您的面板也没有正确调整大小,那么这对您不起作用。当然,这可能是解决底层代码问题的一种解决方法,在完美的世界中,可以通过控件本身来修复该问题。

For controls other than your custom control can you get it to resize properly? For example if you put in a richtextbox and set Anchor accordingly (All) will it resize properly for you? If it does then there must be somthing fishy with the sizing of your control, BUT you could maybe cheat it by doing this...

  1. Create a Panel that is the exact size and Anchoring you would like from your control.
  2. Add your control to the Panel and set Dock to "Fill". Effectively you will fill the panel with your control and use the panel to control resizing on the form.

If of course your panel doesn't resize properly either then this won't work for you. Granted this is likely a workaround to an underlying code issue that in a perfect world would be remidied on the control itself.

高速公鹿 2024-10-21 20:03:51

我正在考虑调整用户控件的大小,并且我使用许多不同的事件来处理此问题(例如,OnSizeChanged、OnFontChanged、OnTextChanged),

具体取决于文本框在控件中的适应方式将会产生影响,但如果您的主容器具有 AutoScaleMode 或 AutoSize 属性,那么您可能会发现您没有捕获所有相关事件。

如果我不合时宜,我提前道歉,因为我对这类事情很陌生。

附注我最初没有找到您的帖子,因为我一直在查看标签 Windows 表单设计器和自定义控件。我真的不知道应该使用哪些标签,并且无法使用所有可能性,因为 5 个标签的限制已经超过了!

I'm looking at resizing a user control, and I'm using many different events to handle this (eg. OnSizeChanged, OnFontChanged, OnTextChanged)

Depending on how your text Boxes fit in your control will make a difference, but if your primary container has the property AutoScaleMode or AutoSize then you may find you are not catching all the relevant events.

I appologise in advance if I'm off-beam as I'm new to this sort of thing.

ps. I didn't find your post initially as I'd been looking at tags Windows-forms-designer and custom-control. I really don't know which tags I should use and can't use all the possibilities as the limit of 5 takes over!

愛上了 2024-10-21 20:03:51

兄弟,你会踢自己的。您不需要 ClientSize.Height - Top - 15,您需要 ClientSize.Height - canvas21.Top - 15。就是这样!

private void Form1_Resize_1(object sender, EventArgs e)
{
    textBox1.Text = this.Width.ToString();
    textBox2.Text = (this.Height - 200).ToString();

    canvas21.Size = new System.Drawing.Size(this.ClientSize.Width,  
        this.ClientSize.Height - canvas21.Top - 15);

    canvas21.Invalidate();
}

Dude, you're going to kick yourself. You don't want ClientSize.Height - Top - 15, you want ClientSize.Height - canvas21.Top - 15. That's it!

private void Form1_Resize_1(object sender, EventArgs e)
{
    textBox1.Text = this.Width.ToString();
    textBox2.Text = (this.Height - 200).ToString();

    canvas21.Size = new System.Drawing.Size(this.ClientSize.Width,  
        this.ClientSize.Height - canvas21.Top - 15);

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