我在一个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();        }

解决方案 »

  1.   

    取得按钮的LOCATION 
    在按钮单击事件中设置新窗口的location
      

  2.   

    使用窗体的Location属性,通过计算按钮在屏幕中的位置,来实现这个效果
      

  3.   


    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();  }  
      }
      

  4.   

    先要设置this.StartPosition = FormStartPosition.Manual;
     设置窗体的起始位置为手动的
    再设置location 才有用
      

  5.   

    由于新窗口的location值与旧窗口的按钮的location值的参照点不一样(按钮的location值,是相对于它所在的容器来说吧),所以即使他们的location值相等,也不能够整齐排列,我现在需要的是他们相对于屏幕的位置,这样才能设置相应的值。
      

  6.   

    正如你说,他们的参照是不一样的。比较简单的转换方法是PointToScreen(),PointToClient()。
    就你的问题,只需要PointToScreen(button1.Location)就可以了!
      

  7.   

    先设置
    dwForm.StartPosition = FormStartPosition.Manual;
    再设置
    dwForm.Location = btn_SelDW.PointToScreen(0, btn_SelDW.Height);
      

  8.   

    由于新弹出的窗体坐标是以当前屏幕为参照系,不能单纯使用按钮在窗体内的坐标来传递,应该使用按钮相对于屏幕的坐标来实现
      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();  }