Hi thedinesh01,
I came up with the same solution as lukeer (but I don't see a need to create a special constructor for Form2). It looks you still have problems. So maybe you give it a try:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SetFormLocation
{
class Program
{
static void Main(string[] args)
{
Form formMain = new Form();
Button buttonOpenDialog = new Button { Text = "Open", Location = new Point(20, 20), Parent = formMain };
buttonOpenDialog.Click += (object objSender, EventArgs ea) =>
{
Button buttonSender = objSender as Button;
if(buttonSender == null ) return;
Point ptLocation = buttonSender.PointToScreen(new Point(0, buttonSender.Height));
Form dialog = new Form { StartPosition = FormStartPosition.Manual, DesktopLocation = ptLocation };
dialog.ShowDialog(formMain);
};
Application.Run(formMain);
}
}
}
(Create a new Forms project and replace the generated Program.cs file's content with the above code).
Steps involved are:
Set
StartPosition
to
FormStartPosition.Manual
Get the Screen coordinates for your button (with
PointToScreen
)
Add the desired offset to the found screen coordinates (in your case the button hight)
Set
DesktopLocation
(or
Location
) for your dialog to the desired value in screen-coordinates.
Should be realy simple and works across multiple monitors etc. (I tested the code too)
Kind regards Johannes