对话框根据控件隐藏或出现而伸缩
2018-02-01 08:58
176 查看
参考链接:http://blogs.msdn.com/b/cumgranosalis/archive/2006/05/18/collapisbleforms.aspx
You know what a collapsible window is – it’s usually used to show advanced features or to show extra information about an error.
有时候我们希望有以下对话框的效果。刚开始时是一个简单的对话框,当我们点击某个按钮后,出现一个另外的信息。而对话框的大小也随着伸缩。
Two examples:
When the error dialog opens, it shows only the basic information so as not to scare the user:
打开一个显示错误信息的对话框,它显示一些简单基本的信息。
![](http://s7.sinaimg.cn/middle/6c762bb3gb02e06284ea6&690)
When the user clicks on Expand, she sees the extra information:
当用户点击Expand按钮的时候,就可以看到一些详细的信息,这时候对话框也相应变长或变大。
![](http://s3.sinaimg.cn/middle/6c762bb3gb02e062afce2&690)
So how do you do this?
我们怎么样也能达到这样的效果呢?
There are two ways I employed so far:
1. Quick and dirty: Use constant values when the “Collapse/Expand” button is clicked. Move controls around (namely, the ones containing the buttons at the bottom).
2. Plain dirty: Use hidden controls to act as “markers” – that way, you still have visual control over where things collapse or expand to via the designer. Pretty easy to do, still need to
move controls around and muck with resizing forms.
I usually use method two – it’s as fast as doing the other one, but much easier to control.
But now I have a new favorite way of doing this..
但是现在我已经有了一个更好的方法。
By using a FlowLayoutPanel and autoresizing forms, you can get the form to do most of the work of growing and shrinking all by itself. This is how you do it:
主要是用到FlowLayoutPanel控件和Form的Autosize属性。下面就是设置的步骤:
1. Set your form to AutoSize true, and the AutoSizeMode to GrowAndShrink.
Form的AutSize属性设为true, AutoSizeMode设为GrowAndShrink
2. Place a FlowLayoutPanel in your form and make
it dock-fill the form.
选择一个FlowLayoutPanel在Form上,选择dock属性为fill
3. Set the panel’s AutoSize to true and it’s AutoSizeMode to GrowAndShrink. Set the FlowDirection to TopDown.
设置FlowLayoutPanel的AutoSize属性为true,AutoSizeMode为GrowAndShrink,FlowDirection属性为TopDown
4. Resize your form to the size you feel comfortable with.
调整Form的大小。
5. Change the MaximumSize property of the form to be the width it currently has with some large number for height (so, if your form has a width of 340, change MaximumSize to be 340,1024). Do the same thing for the panel. If you don’t do this, the form will
grow horizontally if labels you have need more space.
改变MaximumSize属性。这不我没有设,不影响效果。
6. Add your controls – in the example above, there’s a label for the error message and a read-only textbox for the details.
加入你想放入的控件。
7. Add your buttons (Okay and “Expand” probably) – you probably want to first add a panel and then add the buttons to that panel.
因为FlowLayoutPanel的FlowDirection属性为TopDown,所以放置在里面的控件都是由上到下排列的,如果希望按自己的方向排列,就先放一个panel,然后再放入自己的控件。如两个按钮就是放在一个panel里,然后将它们放到右边。
8. In your Expand/Collapse button, change the visibility of the Detail textbox.
9. In the VisibleChange event on the Detail textbox, change the text of the button (so it alternates between “Collapse” and “Expand” or something.
That’s it. WinForms will do the rest of the heavy lifting for you when the form is shown.
代码如下:
You know what a collapsible window is – it’s usually used to show advanced features or to show extra information about an error.
有时候我们希望有以下对话框的效果。刚开始时是一个简单的对话框,当我们点击某个按钮后,出现一个另外的信息。而对话框的大小也随着伸缩。
Two examples:
When the error dialog opens, it shows only the basic information so as not to scare the user:
打开一个显示错误信息的对话框,它显示一些简单基本的信息。
When the user clicks on Expand, she sees the extra information:
当用户点击Expand按钮的时候,就可以看到一些详细的信息,这时候对话框也相应变长或变大。
So how do you do this?
我们怎么样也能达到这样的效果呢?
There are two ways I employed so far:
1. Quick and dirty: Use constant values when the “Collapse/Expand” button is clicked. Move controls around (namely, the ones containing the buttons at the bottom).
2. Plain dirty: Use hidden controls to act as “markers” – that way, you still have visual control over where things collapse or expand to via the designer. Pretty easy to do, still need to
move controls around and muck with resizing forms.
I usually use method two – it’s as fast as doing the other one, but much easier to control.
But now I have a new favorite way of doing this..
但是现在我已经有了一个更好的方法。
By using a FlowLayoutPanel and autoresizing forms, you can get the form to do most of the work of growing and shrinking all by itself. This is how you do it:
主要是用到FlowLayoutPanel控件和Form的Autosize属性。下面就是设置的步骤:
1. Set your form to AutoSize true, and the AutoSizeMode to GrowAndShrink.
Form的AutSize属性设为true, AutoSizeMode设为GrowAndShrink
2. Place a FlowLayoutPanel in your form and make
it dock-fill the form.
选择一个FlowLayoutPanel在Form上,选择dock属性为fill
3. Set the panel’s AutoSize to true and it’s AutoSizeMode to GrowAndShrink. Set the FlowDirection to TopDown.
设置FlowLayoutPanel的AutoSize属性为true,AutoSizeMode为GrowAndShrink,FlowDirection属性为TopDown
4. Resize your form to the size you feel comfortable with.
调整Form的大小。
5. Change the MaximumSize property of the form to be the width it currently has with some large number for height (so, if your form has a width of 340, change MaximumSize to be 340,1024). Do the same thing for the panel. If you don’t do this, the form will
grow horizontally if labels you have need more space.
改变MaximumSize属性。这不我没有设,不影响效果。
6. Add your controls – in the example above, there’s a label for the error message and a read-only textbox for the details.
加入你想放入的控件。
7. Add your buttons (Okay and “Expand” probably) – you probably want to first add a panel and then add the buttons to that panel.
因为FlowLayoutPanel的FlowDirection属性为TopDown,所以放置在里面的控件都是由上到下排列的,如果希望按自己的方向排列,就先放一个panel,然后再放入自己的控件。如两个按钮就是放在一个panel里,然后将它们放到右边。
8. In your Expand/Collapse button, change the visibility of the Detail textbox.
9. In the VisibleChange event on the Detail textbox, change the text of the button (so it alternates between “Collapse” and “Expand” or something.
That’s it. WinForms will do the rest of the heavy lifting for you when the form is shown.
代码如下:
public partial class FrmTest : Form
{
public FrmTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Visible = !textBox1.Visible;
button1.Text = textBox1.Visible ? "Collapse" : " Expand";
}
private void button1_TextChanged(object sender, EventArgs e)
{
button1.Text = textBox1.Visible ? "Collapse" : " Expand";
}
}
相关文章推荐
- 根据条件,隐藏或显示gridview中的按钮或其他控件,
- Android Xlistview 或者 listview改变控件显示隐藏,chekBox状态发生改变,下拉,上划出现错乱。
- vc 根据字体对话框和颜色对话框设置控件里的字体和颜色
- 在VS2005和2008的对话框里,添加了一个MSCOMM通信控件,怎么mscomm.h和mscomm.cpp没有自动出现?
- 根据对话框大小调整控件大小和位置
- [iOS]滑动隐藏或者出现对应控件Demo
- vitamio videoView 用隐藏除videoview的控件,并旋转屏幕方向实现的全屏功能,出现的画面不能填充满videoview(画面不完整)
- 安卓控件隐藏和出现动画
- 对话框的隐藏和出现
- 根据条件,隐藏或显示gridview中的按钮或其他控件,
- 隐藏对话框上的所有控件
- 调用Gmaps根据地址显示对应的Google地图(对控件进行隐藏和改变)
- 根据需要对控件进行隐藏
- asp.net中弹出对话框,ListView中隐藏控件,validateRequest
- VC对话框加载图片、改变背景颜色、画图、显示隐藏控件
- 标签控件中显示对话框出现的问题及解决方法
- 弹出对话框+隐藏控件实现实现对确定和取消的判断
- 在ActiveX控件对话框中添加系统自带的ocx控件出现Debug Assertion Failed
- 服务器控件在客户端触发alert对话框后,根据情况进行回发服务器操作
- 根据对话框大小调整控件大小和位置