winform程序中,怎么用代码控制界面大小 form1本来是500*300的;点击“详细信息”(button1),界面变成500*400的;再点一下,又恢复成500*300的请问button1里的代码怎么写啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 控件的Width属性代表宽,Height属性代表高;如果你是用两个按钮来控制,代码如下://button1_Clickform1.Height=400;//button2_Clickform1.Height=300;如果你是用一个按钮来控制,就需要创建一个字段来表示状态:例如bool state=true;if(this.state==true){ form1.Height=400; this.state=false}else{ form1.Height=300; this.state=true;}//或者也可以用下面的代码根据窗体的高度来判断if(this.Height==300) this.Height=400;else this.Height=300; 只能用this.Height this.Width 来改变高度和宽度,不能用Form1.Height,Form1.Width来改变。 不行,我就是这么试的提示说:不可访问“System.Windows.Forms.Control.Height”,因为它受保护级别限制后来我又改成Form1 a=new Form1();a.Height=400;虽然执行了,但是没变化,还是300 可以这样试试:public Form1 : System.Windows.Form{ private int CurrentHeigt; public Form1() { ... this.CurrentHeigth = this.Height;//Height = 300 NewHeigth = 400; ... } private void btnDetails(object sender,System.EventArgs e) { if(this.Height == CurrentHeight) { .... this.Height = height; .... } else this.Height = currentHeight; }} if(this.Height==300) this.Height=400;else this.Height=300; 更正:可以这样试试:public Form1 : System.Windows.Form{ private int CurrentHeight; private int NewHeight public Form1() { ... this.CurrentHeigth = this.Height;//Height = 300 NewHeigth = 400; ... } private void btnDetails(object sender,System.EventArgs e) { if(this.Height == CurrentHeight) { .... this.Height = NewHeight; .... } else this.Height = CurrentHeight; }} 后来我又改成Form1 a=new Form1();a.Height=400;虽然执行了,但是没变化,还是300==界面没有重绘? private bool click=true; private void btnDetails_Click(object sender, System.EventArgs e) { if(click) { this.Size=new System.Drawing.Size(390,288); this.click=false; } else { this.Size=new System.Drawing.Size(390,160); this.click=true; } } 初始化为this.ClientSize = new System.Drawing.Size(384, 263); 关于c#文件如何在开发平台上打开问题!!求大侠!!赤脚 c# WebBrowser如何去掉最大化按钮 winform 导出 Excel的一个问题 如何提高程序的运行速度,未与数据库接触!! Remoting 的一个问题 C#如何计算两个日期d1和d2相差的天数x 两种用法有什么区别,关于base 如何改变文件夹的访问权限 请问asp.net中如何链接sybase数据库?多谢多谢!! splitContainer与treeview的使用 怎么在相同的CS文件中处理Form?谢谢。 XML在集成中的使用
如果你是用两个按钮来控制,代码如下:
//button1_Click
form1.Height=400;//button2_Click
form1.Height=300;如果你是用一个按钮来控制,就需要创建一个字段来表示状态:例如bool state=true;
if(this.state==true)
{
form1.Height=400;
this.state=false
}
else
{
form1.Height=300;
this.state=true;
}//或者也可以用下面的代码根据窗体的高度来判断
if(this.Height==300)
this.Height=400;
else
this.Height=300;
提示说:不可访问“System.Windows.Forms.Control.Height”,因为它受保护级别限制后来我又改成
Form1 a=new Form1();
a.Height=400;
虽然执行了,但是没变化,还是300
public Form1 : System.Windows.Form
{
private int CurrentHeigt;
public Form1()
{
...
this.CurrentHeigth = this.Height;//Height = 300
NewHeigth = 400;
...
}
private void btnDetails(object sender,System.EventArgs e)
{
if(this.Height == CurrentHeight)
{
....
this.Height = height;
....
}
else this.Height = currentHeight;
}
}
this.Height=400;
else
this.Height=300;
可以这样试试:
public Form1 : System.Windows.Form
{
private int CurrentHeight;
private int NewHeight
public Form1()
{
...
this.CurrentHeigth = this.Height;//Height = 300
NewHeigth = 400;
...
}
private void btnDetails(object sender,System.EventArgs e)
{
if(this.Height == CurrentHeight)
{
....
this.Height = NewHeight;
....
}
else this.Height = CurrentHeight;
}
}
Form1 a=new Form1();
a.Height=400;
虽然执行了,但是没变化,还是300==
界面没有重绘?
{
if(click)
{
this.Size=new System.Drawing.Size(390,288);
this.click=false;
}
else
{
this.Size=new System.Drawing.Size(390,160);
this.click=true;
}
}
this.ClientSize = new System.Drawing.Size(384, 263);