Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
suppose i have main from and button in it. and i have another Form form2, when i click in the button of the main form i want Form 2 appear just below the button. No matter where i kept the button and where i kept the main form i.e. drag to any corner..
Posted

Get the button's location in its event handler and pass it to a custom constructor like this: (untested)
C#
private void button1_Click(object sender, EventArgs e)
{
    Button source = sender as Button;
    if (source == null)
    {
        return;
    }

    Point lowerLeft = source.PointToScreen(new Point(0, source.Height));

    Form2 child = new Form2(lowerLeft);
    child.ShowDialog();
}

class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public Form2(Point location) : this()
    {
        this.Location = location;
    }
}
[Edit]
And set From2's StartPosition[^] property to Manual[^].
[/Edit]
 
Share this answer
 
v3
Comments
thedinesh01 9-Apr-14 4:00am    
this did not work for me :(..
thedinesh01 9-Apr-14 4:05am    
Point lowerleft = this.button1.Location;
Form2 from = new Form(lowerleft);
frm.Show().

i tried this since i got error in here.
Point lowerLeft = source.PointToScreen(source.Location.X, source.Location.Y + source.Height);

how can we fix that.
lukeer 9-Apr-14 5:33am    
0) "did not work" and "got error" are too vague.
1) What did you expect?
2) What happened instead (in case of error: post complete error message)?
thedinesh01 9-Apr-14 5:42am    
No overload for method 'PointToScreen' takes 2 arguments.
this was the error message i got :(..
lukeer 9-Apr-14 6:22am    
PointToScreen[^] needs an argument of type Point[^]. I corrected that in my solution.
BTW: MSDN[^] comes in very handy for every .NET developer. And it has a search function.
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:

C#
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
 
Share this answer
 
Comments
thedinesh01 11-Apr-14 1:04am    
thank you so much johannwsnestler. this code finally works but here the forms and button are created from back end. which is a bit confusing. i will try to implement this in my project. My friend me were really having nightmare trying this. i think it will be done now. :). thank you vey much..
lukeer 11-Apr-14 1:36am    
What's that "call constructor with braced expression" thing called? I'd like to be able to look that up eventually.
I mean Form dialog = new Form { StartPosition = FormStartPosition.Manual };
thedinesh01 11-Apr-14 2:09am    
i think we are giving the location of form dynamically so we are setting its StartPosition to be manual..
johannesnestler 11-Apr-14 4:02am    
This is "initializer Syntax" - have a look at http://msdn.microsoft.com/en-us/library/bb384062.aspx - this is equivalent to default constructor and setting the properties - just shorter writing...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900