Winform Activeform Location of X and Y coordinate C# code

Posted on 2011-10-15
Last Modified: 2013-12-16
I am trying to use the current x, and y position of the active form, to determine the current x and y mouse coordinates for a given (webbrowser) in the Main form of the application. As coordinates of the form will change depending on resolution, I am looking to ensure the mouse coordinates displayed are always only current with the browser form.

int NewTopBorderValueX = Main.ActiveForm.DesktopBounds.Location.X;
int NewTopBorderValueY = Main.ActiveForm.DesktopBounds.Location.Y;

I set up the above two variables to subtract these values when the form position changes from 0,0.

However, it asking me to use the new keyword to create an instance of the object. However, I am confused because the instance of the object is the active form.

Any suggestions on how I need to create a new instance of the active form.

Question by:gcastong

    Author Comment

    Here is the method;

     private void tCheckMouse_Tick(object sender, EventArgs e)
                int NewTopBorderValueX = Main.ActiveForm.DesktopBounds.Location.X;
                int NewTopBorderValueY = Main.ActiveForm.DesktopBounds.Location.Y;
                Point pMousePosition = Control.MousePosition;
                pMousePosition.X = pMousePosition.X - 33;
                pMousePosition.Y = pMousePosition.Y - 238;
                tbPositionX.Text = pMousePosition.X.ToString();
                tbPositionY.Text = pMousePosition.Y.ToString();

    - when the form is at 0, 0, the webrowser start coordinates are X 33 and Y 238. If the form changes to let's say 50, 50, then the start of the webrowser will be X33- 50 and Y 238 -50.
    LVL 17

    Expert Comment

    by:Carlos Villegas
    Hello, I think that you really want to use is:
    int NewTopBorderValueX = this.DesktopLocation.X;
    int NewTopBorderValueY = this.DesktopLocation.Y;

    Open in new window

    LVL 17

    Expert Comment

    by:Carlos Villegas
    if (Form.ActiveForm == null)
    int NewTopBorderValueX = Form.ActiveForm.DesktopLocation.X;
    int NewTopBorderValueY = Form.ActiveForm.DesktopLocation.Y;

    Open in new window


    Author Comment

    The top worked perfectly!

    LVL 17

    Accepted Solution

    You're welcome

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now