Strange PointToScreen( Location ) in a Form


  • I want to automatically show the SIP when an user enters a TextBox.
    More than just show it, I don't want the SIP to overlap the TextBox, so that the user cannot see what is being written.

    I did the following:

    class MyTextBox: TextBox
    override void OnGotFocus( EventArgs e )
        System.Drawing.Point scrLoc = PointToScreen( Location );

        if ( scrLoc.Y + this.Height >= sipRect.Top &&    // Not enough space below...
            scrLoc.Y > sipRect.Height + 26 )            //    ... but enough above.
            SIPpos = new System.Drawing.Point( 0, scrLoc.Y - sipRect.Height - 1 );
        // Default position, else.
            SIPpos = SIPorgPos;


    In my example, I have a Form whose Location = Point( 0, 26 ) (of course, it's a CF Form);
     this Form contains a MyTextBox, having its Location = Point( 3, 37 ).

    When I am inside MyTextBox.OnGotFocus, scrLoc = PointToScreen( Location ) = Point( 7, 101 )!!!

    Shouldn't it be Point( 3, 63 )???

    My Form.Parent = null.

    I did some other tests (all in the same

        Parent.Location    {X = 0 Y = 26}
        PointToScreen( Parent.Location )    {X = 4 Y = 90}
        Parent.PointToScreen( Parent.Location )    {X = 0 Y = 52}
        Parent.PointToScreen( Location )    {X = 3 Y = 63}

    Of course I tested other MyTextBoxes, in different positions, bla, bla, bla, and nothing smelled better.

    What is the joke? Where I am failing?
    Any help, please?
    Thursday, May 24, 2007 1:47 PM


  • Hi,


    PointToScreen works relative to the client area of your current control. So within your MyTextBox class the point (0,0) is considered the top left of the text box, not the top left of the form.


    So when you call PointToScreen() with a co-ordinate (3,37) you are asking what are the screen co-ordinates of a point 3 pixels across and 37 pixels down from the top left hand corner of the textbox. Perhaps this will be clearer if you rewrite the line as follows:


    Code Snippet
    System.Drawing.Point scrLoc = this.PointToScreen( Location );


    Notice the that the PointToScreen method is provided by "this", i.e. the current instance of the MyTextBox control.


    So to get the proper screen co-ordinates of your Location property you need to call the PointToScreen method which belongs to the parent of the control. You will notice in the list of tests you provided that the last one did exactly this and it returned the correct resutls (3, 63).


    Code Snippet
    Parent.PointToScreen( Location )    {X = 3 Y = 63}


    This works because the Location of the MyTextBox instance is relative to the client area of it's parent, i.e. if Location is set to (10, 20), it means the control is 10 pixels across and 20 pixels down from the top left corner of it's parent.


    Hope this helps,

    Christopher Fairbairn



    Thursday, May 24, 2007 10:54 PM