• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1136
  • Last Modified:

winform positioning problem

I have a winform with borderstyle fixed dialog named form1. Now I want to open another form from that form whose border style is none named form2. I want to open the form2 such as:

form1.button1.left=form2.left
form1.button1.top=form2.top.

but the form location does not appear to be right. Why? I am using VS2008Express with XP SP3
0
Amitava_Mukherjee
Asked:
Amitava_Mukherjee
  • 4
  • 3
  • 2
  • +1
1 Solution
 
sunithnairCommented:
Should it not be the other way round?
form2.left=form1.button1.left;
form2.top=form1.button1.top;
0
 
Amitava_MukherjeeAuthor Commented:
sorry ,

form2.left=form1.button1.left;
form2.top=form1.button1.top;

this is not working actually
0
 
alex_pavenCommented:
First of all, it makes a difference whether you set Left and Top before or after Show()-ing the form;
but the real problem here is that you're setting the form's position to the button's position which is relative to the button's parent container. Say the button is located at 10, 10 -> the second form would then be located at 10, 10, which is near the top left corner of the screen.

To get the 'real' (screen) coordinates of your button, you need to call button1.PointToScreen(button1.Location), then you can use the result to set the form's position. However, you need to set the location after calling the form's Show() method, and I recommend using the .DesktopLocation property.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
sunithnairCommented:
Try it this way
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show(this);
            f2.Location = new Point((this.Left + button1.Left), (this.Top + button1.Top + 52));
        }

Open in new window

0
 
alex_pavenCommented:
With regards to the previous poster, if I have a different windows theme or display settings that make my borders or title bar larger, that 52 value won't be correct (and if you move the button in another container, none of the values will be correct and you have to modify the code). It's better to use .PointToScreen, so everything becomes a whole lot simpler:
f2.DesktopLocation = button1.PointToScreen(button1.Location);
0
 
sunithnairCommented:
Then in that case the more appropriate would be
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show(this);
            Point p = button1.Location;
            p.Y += button1.Height;
            f2.DesktopLocation = this.PointToScreen(p);
            
            OR
 
            //Form2 f2 = new Form2();
            //f2.Show(this);
            //f2.DesktopLocation = this.PointToScreen(button1.Location);
        }

Open in new window

0
 
alex_pavenCommented:
Quite right, it's this.PointToScreen, not button.PointToScreen, and if the form is meant to appear directly under the button, then your code is the most correct.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I would just add that you can position the form BEFORE you show it.

Set the StartPosition() Property of the Form to Manual:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition.aspx

    "Manual - The position of the form is determined by the Location property."

Simple example in VB.Net:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f2 As New Form2
        f2.StartPosition = FormStartPosition.Manual
        Dim pt As Point = Me.PointToScreen(Button1.Location)
        pt.Offset(0, Button1.Height)
        f2.Location = pt
        f2.Show(Me)
    End Sub

Open in new window

0
 
alex_pavenCommented:
*facepalm* of course Idle_Mind's right, don't know how I could forget that.
0
 
Amitava_MukherjeeAuthor Commented:
Perfect Solution, Thanx :-)
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now