确定窗体运行时控件的位置 [英] Determines control's location when form is running

查看:89
本文介绍了确定窗体运行时控件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在(10,14)位置创建了一个带有textbox1的表单.
现在,通过从该文本框1按F4调用另一个名为FORM2的模式形式.
我想在运行时在textbox1下方显示FORM2.
但是我无法计算FORM2的位置.请帮助我.在此先谢谢您.

I''ve created a form with a textbox1 at the location (10,14).
Now calling another modal form named FORM2 by pressing F4 from that textbox1.
I want to display the FORM2, just below the textbox1 in run time.
But I cannot calculate the location of FORM2. Pls, help me. Thanks in advance.

推荐答案

每个控件都有一个PointToScreen方法,您可以使用该方法将您拥有的本地坐标转换为可以放置新坐标的屏幕坐标.格式.因此,从您的原始表单开始,您应该可以执行以下操作:

Each control has a PointToScreen method which you can use to convert the local coords that you have into screen coordinates that you can place your new form at. So from your original form you should be able to do something like this:

void textBox1_KeyDown(...) //Assuming your using an event on the form for catching F4
{
  Point newFormPos = this.PointToScreen(new Point(10,40));
  newForm.Location = newFormPos;
  newForm.Show();
}


为什么不呢?很简单.

Form2的位置相对于屏幕的TLHC.
TextBox1的位置相对于Form1的工作区的TLHC.

因此,要将Form2放置在TextBox1下,将Form1.Location添加到TextBox1.Location,并通过标题栏粗细和左侧边框粗细偏移它,再加上偏移量使其恰好在下面".
Why not? It''s pretty simple.

Form2 location is relative to the TLHC of the screen.
TextBox1 location is relative to the TLHC of the client area of Form1.

So, to place Form2 below TextBox1 add Form1.Location to TextBox1.Location, and offset it by the title bar thickness and the left hand border thickness, plus an offset to get it "just below".


这篇关于确定窗体运行时控件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆