简体   繁体   English

.net调整大小控制不起作用?

[英].net Resizing control won't work?

I have a couple of pictureboxes that need to be resized by aspect ratio when the window size changes. 我有几个图片框,当窗口大小更改时,需要根据宽高比调整它们的大小。 I assumed I can anchor the width, but set the height manually (ie anchor the left, right, and top edges; but not the bottom.) However, my control won't resize if I try changing the Size property. 我以为我可以锚定宽度,但可以手动设置高度(即锚定左,右和上边缘;但不能锚固底部)。但是,如果尝试更改Size属性,控件将不会调整大小。 Why wouldn't that work? 为什么不行呢? How can I resize my control? 如何调整控件的大小?

    private void Form1_Resize(object sender, System.EventArgs e)
    {
        int oldWidth = 1280;
        int oldHeight = 1024;
        int newWidth = webcamPictureBox.Size.Width; // this is auto-resized w/ window; becomes 591
        int newHeight = (oldHeight * newWidth) / oldWidth; // this is calculated manually; becomes 472

        // Control won't resize if I change the Size property
        // Size property stays the same
        this.webcamPictureBox.Size = new Size(newWidth, newHeight);
        this.thumbnailPictureBox.Size = new Size(newWidth, newHeight);
    }

You could be running into one of a few problems, I suppose: 我想您可能会遇到以下几个问题之一:

  • What's the SizeMode of the PictureBox? PictureBox的SizeMode是多少? Is it AutoSize , StretchImage , or something like that? AutoSizeStretchImage还是类似的东西? It should probably be Normal for what you want to do. 对于您想做的事情可能应该是Normal的。

  • Are you sure you have your anchoring set up correctly? 您确定锚定设置正确吗?

  • Did you debug and check the final size of the picture boxes you're trying to resize? 您是否调试并检查了要调整尺寸的图片框的最终尺寸? You should verify that it's what you expect and that the form has been repainted. 您应该验证它是您所期望的,并且表单已重新粉刷。

These are just some ideas; 这些只是一些想法; your code along is not enough to provide a great response. 您的代码不足以提供出色的响应。

It's difficult to answer this question definitively with the information you've posted. 很难用您所发布的信息来明确回答这个问题。

Windows forms controls problems are difficult to debug by nature because of all the controls that might be affecting what you're doing. Windows窗体控件问题本质上很难调试,因为所有控件都可能影响您的工作。 First off, you'll want to try debugging this on your own on a form with as few controls as possible. 首先,您将要尝试使用尽可能少的控件在窗体上自行调试。 Are there any circumstances under which the resizing behaves properly? 在任何情况下,调整大小的行为都正常吗?

That said, is the Dock property set on these controls? 也就是说,是否在这些控件上设置了Dock属性? They definitely won't resize if they're set to DockStyle.Fill. 如果将它们设置为DockStyle.Fill,则它们绝对不会调整大小。

Thanks for the ideas -- they pointed me on the right track! 感谢您的想法-他们指出了我的正确方向! Everything was set up as they should be, except the TableLayoutPanel they were in was constraining their sizes. 一切都按应有的方式设置,除了它们所在的TableLayoutPanel限制了它们的大小。 I determined this by noticing the pictureboxes' size values were simply uneditable, both in runtime and in the designer. 我通过注意到图片框的大小值在运行时和设计器中都无法编辑来确定这一点。

So I set TableLayoutPanel's AutoSize to true, and it works great! 因此,我将TableLayoutPanel的AutoSize设置为true,并且效果很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM