Obtaining the location of a Control, relative to the SystemInformation.VirtualScreen.Location RRS feed

  • Question

  • I like using the Help.ShowPopup method when the user has entered something invalid in a textbox.  If, for example, the user was supposed to enter a decimal integer and they entered "my dog has fleas" instead, then I can use the following call to place a message on top of the offending textbox.

    Help.ShowPopup(this, "Enter the number of fleas that are on your dog (zero to 65535)", location_of_textbox);

    However, it is a pain in the neck to figure out that final parameter, the Point which I've called as "location_of_textbox".  The only way I've figured out how to do this is to get the Control.Location of the textbox and add it to the Location of its parent control, and add it to the location of the parent's parent control, and so on.

    Since the final textbox that I want to know its position has many, many layers of parents, this makes it cumbersome to calculate the position of the textbox relative to the VirtualScreen.

    Is there an easier way?  I thought about using the Cursor.Location, but that will not work if the user has been navigating by keyboard---the cursor may be nowhere near the textbox.  Or is there a simple way to create some other kind of tooltip or help bubble to temporarily annotate the control that the user needs to correct?

    Friday, August 18, 2006 9:25 PM


  • Hi,
    There is a simple way !You can use ErrorProviderControl and Control Validation events to achieve this.Assuming the ErrorProviderControl's name is MyErrorCtrl,the following line of code does the trick.
    MyErrorCtrl.SetError(TxtBoxName, "Enter the number of fleas that are on your dog (zero to 65535)");

    Friday, August 18, 2006 10:14 PM