如何设置“首选尺寸”表格?
我在表单上有一些控件,并将它们固定在表单的左侧和右侧,以确保它们随表单一起增长和收缩。我的表单还设置为自动增大和缩小,以适应一些可能会出现长字符串的标签。当这些标签的长字符串被删除时,我希望我的表单恢复到之前的宽度。固定在我的表单左侧和右侧的所述控件似乎阻止了这种情况。我能让它按我的预期工作吗?
I have some controls on a form, and I have them anchored to the left and right of the form to ensure they grow and shrink with the form. My form is also set to automatically grow and shrink in order to accommodate some labels that may get long strings. When those labels get their long strings removed, I want my form to go back to the width it was at before. Said controls which are anchored to the left and right on my form appear to be preventing this. Can I make it work as I intend?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一种不需要您编写任何尺寸特定代码的方法...但它假设您的所有按钮都位于底部。
步骤 1. 设置对话框的最小尺寸。
步骤 2. 在表单上创建面板。
步骤 3. 将按钮移至面板中。
步骤 4. 将面板停靠到表单底部。
步骤 5. 确保对话框 AutoSizeMode 设置为 ShrinkAndGrow
您可以使用此技术将面板停靠到任何左、右、上和下边框。并适当调整对话框的大小。
Here's a way that doesn't require you to write any sizing specific code... but it assumes all your buttons are at the bottom.
Step 1. Set a minimum size for the dialog.
Step 2. Create a panel on the form.
Step 3. Move your buttons into the panel.
Step 4. Dock the panel to the bottom of the form.
Step 5. Ensure that the dialogs AutoSizeMode is set to ShrinkAndGrow
You can dock panels to any of the left,right,top and bottom borders with this technique. and have the dialog resize appropriately.
尝试使用 TableLayoutPanel 或 flowlayoutpanel 控件。
http://msdn.microsoft.com/en- us/library/system.windows.forms.tablelayoutpanel.aspx
http://visualbasic.about.com/od/usingvbnet/a/tloflopnl。嗯
Try with TableLayoutPanel or flowlayoutpanel control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx
http://visualbasic.about.com/od/usingvbnet/a/tloflopnl.htm
我可以建议您不要这样做吗?我知道您正在努力为您的用户提供帮助,但是随机缩小窗口(对用户来说可能看起来是随机的)将会令人困惑,甚至可能令人沮丧。对于新的 Windows 7 UI 功能 Aero Snap 来说,情况可能更是如此,即您将窗口停靠在左侧,然后它会根据您的需要调整大小。
将窗口大小视为用户偏好。用户已经告诉你他们想要的东西有多大,尊重它!
编辑:我还应该补充一点,如果您确实走这条路线,则会出现错误。考虑表单的宽度为 1000 像素,用户将其拖动到 (-500, 100) 以在显示器上显示一半。如果他们点击可见的按钮(或者更糟糕的是,后台发生了某些事情!)并且表单大小调整为 450 像素,窗口就会消失!用户可以在任务栏中看到它,但无法访问它,除非他们知道一些花哨的 alt+space 快捷键。
Can I suggest you not do this? I know you're trying to be helpful for your users, but having a window shrinking randomly (it will likely seem random to the user) is going to be confusing and perhaps frustrating. This is probably even more true with the new Windows 7 UI feature Aero Snap i.e. you dock the window to the left and then it resizes on you.
Consider the window size a user preference. The user has already told you how big they want the thing to be, respect it!
Edit: I should also add that there is a bug if you do go down this route. Consider the form being 1000 pixes wide and the user drags it to (-500, 100) to show it half on their monitor. If they hit a button that's visible (or worse, something happens in the background!) and the form resizes down to 450 pixels the window has just disappeared! The user sees it in the taskbar, but can't get to it unless they know some fancy alt+space shortcuts.
一种建议是挂钩 OnPaint 事件。您的控件此时已构建,并将在窗体上设置其初始位置值。将您想要的所有内容捕获到在表单级别设置为私有的对象中。
在 OnStringsRemoved 事件期间,从对象中读取内容并将所有内容设置回原来的状态。修改锚定的控件应该会触发表单调整大小,但您可能必须手动设置所有内容。也可能是修改表单会缩小控件,而不是相反。
另一种建议是对您的初始值进行硬编码,然后每次都返回它们。我个人会选择 OnPaint。它将帮助您处理诸如大字体增加控件大小之类的事情或框架在用户看到它之前所做的任何事情。
One suggestion would be to hook the OnPaint event. Your controls are built by this point and will have their initial location values set on the form. Capture everything you want into an object set private at the form level.
During an OnStringsRemoved event, read from your object and set everything back the way it was. Modifying a control that's anchored should trigger the form resize but it may be that you have to set everything manually. It may also be that modifying the form will shrink the controls and not the other way around.
An alternative suggestion would be to just hardcode your initial values and go back to them every time. I personally would opt for OnPaint. It'll help you handle things like Large Fonts bumping up control size or anything the framework does before the user sees it.
使用 System.Drawing.Graphics.MeasureString
text=yourtext;
字体=标签字体;
宽度=标签的最大宽度;
MSDN 中的 MeasureString
您可以 google 了解更多信息
use System.Drawing.Graphics.MeasureString
text=yourtext;
font=fontofthelabel;
width=maxwidthoflabel;
MeasureString in MSDN
you can google for more