我在一个form中的按钮按下去后,弹出另个窗口,如何让新窗口的显示位置在按钮正下方?
private void btn_SelDW_Click(object sender, EventArgs e)
{
int x, y,h;
SelDWForm dwForm = new SelDWForm();
if (sender is Button)
{
x=(sender as Button).Location.X;
y = (sender as Button).Location.Y;
//dwForm.Left= x;
//dwForm.Top = y;
} dwForm.ShowDialog(); }
private void btn_SelDW_Click(object sender, EventArgs e)
{
int x, y,h;
SelDWForm dwForm = new SelDWForm();
if (sender is Button)
{
x=(sender as Button).Location.X;
y = (sender as Button).Location.Y;
//dwForm.Left= x;
//dwForm.Top = y;
} dwForm.ShowDialog(); }
在按钮单击事件中设置新窗口的location
int x, y
private void btn_SelDW_Click(object sender, EventArgs e)
{
SelDWForm dwForm = new SelDWForm();
if (sender is Button)
{
x=(sender as Button).Location.X;
y = (sender as Button).Location.Y;
//dwForm.Left= x;
//dwForm.Top = y;
dwForm.Location = new Point(x,y);
dwForm.ShowDialog(); }
}
设置窗体的起始位置为手动的
再设置location 才有用
就你的问题,只需要PointToScreen(button1.Location)就可以了!
dwForm.StartPosition = FormStartPosition.Manual;
再设置
dwForm.Location = btn_SelDW.PointToScreen(0, btn_SelDW.Height);
private void btn_SelDW_Click(object sender, EventArgs e)
{
int x, y,h;
SelDWForm dwForm = new SelDWForm();
if (sender is Button)
{
x = (sender as Button).PointToScreen(new Point()).X;
y = (sender as Button).PointToScreen(new Point()).Y;
dwForm.Location = new Point(x, y);
} dwForm.ShowDialog(); }