views:

660

answers:

3

Using Visual Studio 2008 SP1, the latest Compact framework and Windows Mobile 5.

I need to use DrawString to put a string over a TextBox. But as soon as I draw the string the TextBox Control just over writes it. (I Know because I drew slightly off the edge of the control and my text is half visible (where is is off the control) and half gone (where it is on the control.)

Is there anyway I can get the TextBox to not refresh so I can keep my text there?

NOTE: I have looked into subclassing TextBox and just having it paint my text. However, Paint events for the TextBox class are not catchable in the CompactFramework. If you know a way to be able to paint on the TextBox without the Paint events then I would love to subclass the TextBox class.

--End of Question--

Just in case you are wondering why I need to do this, here is what I am working on: I need to have a text box where a numeric value must be entered twice. I need some sort of clear clue that they have to enter the number again. I would like to have a slightly grayed out text appear over the text box telling the user to re-enter.

I have tried using a label, a hyperlink label and another text box, but they obscure the text below (which has a default value that has to be partially visible).

If anyone knows a different way cue for re-entry that would be great too!

Vacano

+1  A: 

You can solve this problem in a different fashion. It sounds like you want to silhouette their previous input so they must type it again.

I don't know what strides the CF has made recently but if there is a RichTextBox then this method will work. If not you would have to write your own implementation starting with a base control.

  1. Set the text of the RichTextBox to the silhouette value but make the text color gray for all the characters.
  2. Capture the keypress events and as they press the correct key, change the text color for that character pressed from gray to black and discard that key press, and discard all other key presses.

This solution won't work if you want to allow them to go off the reservation, such as freeform text. Instead of discarding what they typed if they mistype or enter a different character you would not discard the keypress, but blank out the current and remaining gray characters thus allowing them to type with no silhouette.

cfeduke
+1  A: 

As I answered in the closed dupe of this:

Where are you doing the DrawText? On the TextBox parent? If so, then that would be expected behavior. Why not create a custom TextBox control that paints (by overriding OnPaint) the value the first time, maybe in something like a light grey, then the second time paints it again in Black?

ctacke