Win Form 的 Splitter 使用心得与技巧
程序员文章站
2023-11-09 13:12:04
今天作个分析html代码,然后再批量下载的程序,其中用到 splitter (分割条),编译程序后,发现分割条不起作用,拖动分割条的时候,相邻的两个&n...
今天作个分析html代码,然后再批量下载的程序,其中用到 splitter (分割条),编译程序后,发现分割条不起作用,拖动分割条的时候,相邻的两个 panel 没有变换大小。为这个几乎花了一天时间,也没找到原因。包括到其他机子上测试。
后来,再次作一个完全独立的测试项目,发现 splitter 的使用有个算是 bug 的问题,如果你首先放两个 panel ,然后再放一个 splitter 。(注意这时候的次序)就会产生我上面出现的问题。这时候代码中的 initializecomponent 函数部分代码如下:
private void initializecomponent()
{
//
// ... 其他代码
//
this.panel1 = new system.windows.forms.panel();
this.panel2 = new system.windows.forms.panel();
this.splitter1 = new system.windows.forms.splitter();
this.panel2.suspendlayout();
this.suspendlayout();
//
// ... 其他代码
//
//
// panel1
//
this.panel1.dock = system.windows.forms.dockstyle.left;
this.panel1.location = new system.drawing.point(0, 42);
this.panel1.name = "panel1";
this.panel1.size = new system.drawing.size(120, 209);
this.panel1.tabindex = 6;
this.panel1.resize += new system.eventhandler(this.panel2_resize);
this.panel1.paint += new system.windows.forms.painteventhandler(this.panel2_paint);
//
// panel2
//
this.panel2.controls.add(this.splitter1);
this.panel2.dock = system.windows.forms.dockstyle.fill;
this.panel2.location = new system.drawing.point(120, 42);
this.panel2.name = "panel2";
this.panel2.size = new system.drawing.size(328, 209);
this.panel2.tabindex = 7;
this.panel2.resize += new system.eventhandler(this.panel2_resize);
this.panel2.paint += new system.windows.forms.painteventhandler(this.panel2_paint);
//
// splitter1
//
this.splitter1.backcolor = system.drawing.systemcolors.desktop;
this.splitter1.location = new system.drawing.point(0, 0);
this.splitter1.name = "splitter1";
this.splitter1.size = new system.drawing.size(3, 209);
this.splitter1.tabindex = 0;
this.splitter1.tabstop = false;
//
// form1
//
this.autoscalebasesize = new system.drawing.size(5, 13);
this.clientsize = new system.drawing.size(448, 273);
this.controls.add(this.panel2);
this.controls.add(this.panel1);
this.controls.add(this.toolbar1);
this.controls.add(this.statusbar1);
this.name = "form1";
this.text = "站点下载工具 2003年9月21日";
this.panel2.resumelayout(false);
this.resumelayout(false);
}
注意:这时候的代码中的顺序。这时候,程序的执行是有问题的。分隔条会不起作用。
但是如果你把这三个控件放入顺序修改为下面的顺序就没有问题了。
1、放入一个 panel 比如:panel1 然后设置他的 dock 属性为:left;
2、放入一个 splitter 比如:splitter1 设置它的背景颜色为一个特殊的颜色,便于看执行效果;
3、放入一个 panel 比如:panel2 然后设置他的 dock 属性为:fill;
4、编译执行程序,这时候就没有问题了。
这时候正确的代码应该是:( initializecomponent 函数部分)
private void initializecomponent()
{
//
// ... 其他代码
//
this.panel1 = new system.windows.forms.panel();
this.panel2 = new system.windows.forms.panel();
this.splitter1 = new system.windows.forms.splitter();
this.panel2.suspendlayout();
this.suspendlayout();
//
// ... 其他代码
//
//
// panel1
//
this.panel1.dock = system.windows.forms.dockstyle.left;
this.panel1.location = new system.drawing.point(0, 42);
this.panel1.name = "panel1";
this.panel1.size = new system.drawing.size(200, 209);
this.panel1.tabindex = 6;
//
// panel2
//
this.panel2.controls.add(this.splitter1);
this.panel2.dock = system.windows.forms.dockstyle.fill;
this.panel2.location = new system.drawing.point(200, 42);
this.panel2.name = "panel2";
this.panel2.size = new system.drawing.size(248, 209);
this.panel2.tabindex = 7;
//
// splitter1
//
this.splitter1.backcolor = system.drawing.systemcolors.desktop;
this.splitter1.location = new system.drawing.point(0, 0);
this.splitter1.name = "splitter1";
this.splitter1.size = new system.drawing.size(3, 209);
this.splitter1.tabindex = 0;
this.splitter1.tabstop = false;
//
// form1
//
this.autoscalebasesize = new system.drawing.size(5, 13);
this.clientsize = new system.drawing.size(448, 273);
this.controls.add(this.panel2);
this.controls.add(this.panel1);
this.controls.add(this.toolbar1);
this.controls.add(this.statusbar1);
this.menu = this.mainmenu1;
this.name = "form1";
this.text = "站点下载工具 2003年9月21日";
this.load += new system.eventhandler(this.form1_load);
this.panel2.resumelayout(false);
this.resumelayout(false);
}
后来,再次作一个完全独立的测试项目,发现 splitter 的使用有个算是 bug 的问题,如果你首先放两个 panel ,然后再放一个 splitter 。(注意这时候的次序)就会产生我上面出现的问题。这时候代码中的 initializecomponent 函数部分代码如下:
复制代码 代码如下:
private void initializecomponent()
{
//
// ... 其他代码
//
this.panel1 = new system.windows.forms.panel();
this.panel2 = new system.windows.forms.panel();
this.splitter1 = new system.windows.forms.splitter();
this.panel2.suspendlayout();
this.suspendlayout();
//
// ... 其他代码
//
//
// panel1
//
this.panel1.dock = system.windows.forms.dockstyle.left;
this.panel1.location = new system.drawing.point(0, 42);
this.panel1.name = "panel1";
this.panel1.size = new system.drawing.size(120, 209);
this.panel1.tabindex = 6;
this.panel1.resize += new system.eventhandler(this.panel2_resize);
this.panel1.paint += new system.windows.forms.painteventhandler(this.panel2_paint);
//
// panel2
//
this.panel2.controls.add(this.splitter1);
this.panel2.dock = system.windows.forms.dockstyle.fill;
this.panel2.location = new system.drawing.point(120, 42);
this.panel2.name = "panel2";
this.panel2.size = new system.drawing.size(328, 209);
this.panel2.tabindex = 7;
this.panel2.resize += new system.eventhandler(this.panel2_resize);
this.panel2.paint += new system.windows.forms.painteventhandler(this.panel2_paint);
//
// splitter1
//
this.splitter1.backcolor = system.drawing.systemcolors.desktop;
this.splitter1.location = new system.drawing.point(0, 0);
this.splitter1.name = "splitter1";
this.splitter1.size = new system.drawing.size(3, 209);
this.splitter1.tabindex = 0;
this.splitter1.tabstop = false;
//
// form1
//
this.autoscalebasesize = new system.drawing.size(5, 13);
this.clientsize = new system.drawing.size(448, 273);
this.controls.add(this.panel2);
this.controls.add(this.panel1);
this.controls.add(this.toolbar1);
this.controls.add(this.statusbar1);
this.name = "form1";
this.text = "站点下载工具 2003年9月21日";
this.panel2.resumelayout(false);
this.resumelayout(false);
}
注意:这时候的代码中的顺序。这时候,程序的执行是有问题的。分隔条会不起作用。
但是如果你把这三个控件放入顺序修改为下面的顺序就没有问题了。
1、放入一个 panel 比如:panel1 然后设置他的 dock 属性为:left;
2、放入一个 splitter 比如:splitter1 设置它的背景颜色为一个特殊的颜色,便于看执行效果;
3、放入一个 panel 比如:panel2 然后设置他的 dock 属性为:fill;
4、编译执行程序,这时候就没有问题了。
这时候正确的代码应该是:( initializecomponent 函数部分)
复制代码 代码如下:
private void initializecomponent()
{
//
// ... 其他代码
//
this.panel1 = new system.windows.forms.panel();
this.panel2 = new system.windows.forms.panel();
this.splitter1 = new system.windows.forms.splitter();
this.panel2.suspendlayout();
this.suspendlayout();
//
// ... 其他代码
//
//
// panel1
//
this.panel1.dock = system.windows.forms.dockstyle.left;
this.panel1.location = new system.drawing.point(0, 42);
this.panel1.name = "panel1";
this.panel1.size = new system.drawing.size(200, 209);
this.panel1.tabindex = 6;
//
// panel2
//
this.panel2.controls.add(this.splitter1);
this.panel2.dock = system.windows.forms.dockstyle.fill;
this.panel2.location = new system.drawing.point(200, 42);
this.panel2.name = "panel2";
this.panel2.size = new system.drawing.size(248, 209);
this.panel2.tabindex = 7;
//
// splitter1
//
this.splitter1.backcolor = system.drawing.systemcolors.desktop;
this.splitter1.location = new system.drawing.point(0, 0);
this.splitter1.name = "splitter1";
this.splitter1.size = new system.drawing.size(3, 209);
this.splitter1.tabindex = 0;
this.splitter1.tabstop = false;
//
// form1
//
this.autoscalebasesize = new system.drawing.size(5, 13);
this.clientsize = new system.drawing.size(448, 273);
this.controls.add(this.panel2);
this.controls.add(this.panel1);
this.controls.add(this.toolbar1);
this.controls.add(this.statusbar1);
this.menu = this.mainmenu1;
this.name = "form1";
this.text = "站点下载工具 2003年9月21日";
this.load += new system.eventhandler(this.form1_load);
this.panel2.resumelayout(false);
this.resumelayout(false);
}
上一篇: 一个简单的ASP.NET验证码