菜鸟问题:咋控制打开窗体的位置?

lusonghe18 2003-07-25 08:12:15
我想在用一按钮打开一窗体的时候,让这窗体始终出现在
紧靠按钮的下方?
...全文
33 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
shareach 2003-07-25
  • 打赏
  • 举报
回复
加个循环:
System.Windows.Forms.Control parentControl=button.Parent;
System.Drawing.Point position = new System.Drawing.Point(0,0);
while (!(parentControl is System.Windows.Forms.Form))
{
position.X += parentControl.Location.X;
position.Y += parentControl.Location.Y;
parentControl = parentControl.Parent;
}
position.X += button.Location.X;
position.Y += button.Location.Y+button.Height;
openForm.Location=position;
derek 2003-07-25
  • 打赏
  • 举报
回复
沒問題啊
openForm.Location=new System.Drawing.Point(tn.Location.X,btn.Location.Y+20);

試試用:
openForm.Left=btn.Left;
openForm.Top=btn.Top+20;
lusonghe18 2003-07-25
  • 打赏
  • 举报
回复
谢谢JOELIU,但是父窗体不是最大化时,openForm的位置就不会根我的按钮位置走啊?
joeliu 2003-07-25
  • 打赏
  • 举报
回复
private void button1_Click(object sender, System.EventArgs e)
{
form2=new Form2 ();
form2.StartPosition=FormStartPosition.Manual ;
form2.Location =new System.Drawing.Point(0,200);
form2.Show ();
}
即可
lusonghe18 2003-07-25
  • 打赏
  • 举报
回复
public class MainForm
{
private OpenForm openForm;//另一窗体
public MainForm()
{
......
}
private void Buttons_Click(object sender,EvengArgs e)
{
Button btn=(Button)sender;
openForm=new OpenForm();
openForm.Location=new System.Drawing.Point(tn.Location.X,btn.Location.Y+20);
openForm.Show();
}
}
liduke 2003-07-25
  • 打赏
  • 举报
回复
把代码贴出来看看
lusonghe18 2003-07-25
  • 打赏
  • 举报
回复
按钮不是动态生成的
lusonghe18 2003-07-25
  • 打赏
  • 举报
回复
求助
declude 2003-07-25
  • 打赏
  • 举报
回复
按钮是动态生成的?
lusonghe18 2003-07-25
  • 打赏
  • 举报
回复
to:declude
单击第一次它出现在左上方,以后每单击打开
它会沿,他的X,Y坐标会增加即往,bottom,right方向移动
到屏幕中间后有会退至第一次的位置
declude 2003-07-25
  • 打赏
  • 举报
回复
什么情况?
lusonghe18 2003-07-25
  • 打赏
  • 举报
回复
Button btn=(Button)sender;
openForm=new OpenForm();
openForm.Location=new System.Drawing.Point(btn.Location.X,btn.Location.Y+20);
openForm.Show();
我这样做是没有达到我上述要求,
为什么?
CMIC 2003-07-25
  • 打赏
  • 举报
回复
设置location属性或StartPosition属性
declude 2003-07-25
  • 打赏
  • 举报
回复
你可以用Form的location属性设置。
joeliu 2003-07-25
  • 打赏
  • 举报
回复
private void button1_Click(object sender, System.EventArgs e)
{
form2=new Form1 ();
form2.StartPosition=FormStartPosition.Manual ;
//横坐标
int X=this.Location.X +this.button1 .Location .X ;
//纵坐标
int Y=this.Location .Y+this.button1 .Location .Y
+this.button1 .Size .Height+30;
form2.Location =new System.Drawing.Point(X,Y);

form2.Show ();
}
不断点按钮,效果是不错的
坐标中的数字30是form2的上面显示标题部分(蓝色条)的高度

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧