DrawString over a TextBox [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Watermarked Textbox for Compact Framework
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

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.
Set the text of the RichTextBox to the silhouette value but make the text color gray for all the characters.
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.

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?

Related

Get a word location from a label and hover it with another label c#

I have a question...i'm trying to hover a specific word from a label with another label, but i don't know how to get the specific word position from the label's text. I would be very happy if you could help me at least, how to get the word location in the label. I have to say that the label Autosize is false, so it could have more than just one line. Thank you!
If I understand correctly, you want to reference one of the words in one label's .Text property and use it in another label when you hover over the first label.
If you know already what word in the string you want, it's easy. You can fetch it by the 0-indexed position of the letters, since a string is really just an array of type Char. You can also use String class tricks like .Substring() to fetch the word.
I think the degree of complexity you've added, though, is that (and correct me if I'm wrong) you want the second label to display the specific word over which the mouse is hovering while the mouse is hovering over the first label. That involves getting the label's coordinates as well as the pointer's, and a rather complex method for determining "what is a word, really?". If possible, I would split your source label into a label for each word. This can be done with dynamic labels by inheriting from the Label (or a container, like Panel) class in a custom control and giving it an IEnumerable of Labels as a property. Doing this, you can assign the OnHover handlers at runtime, and evaluate these events as individual labels rather than having to do the math and hope that a screen resolution change doesn't ruin your day.

failedtoperformactiononhiddencontrolexception despite the textbox being clearly visible

I have a wpf application which has a text box in which I can write a text value to just fine the first time around. If I exit the window the text box is in and then reopen that same window the box can no longer be found by the testing, despite being enabled, and clearly visible on my screen.
I looked at the values of the box, the x,y = -1,-1 and height,width are also -1,-1 which is confusing seeing as the box is completely visible.
I know what the text in the box will be, is it possible to search for a text box with that as a workaround?
I'll keep researching this but any feedback is appreciated :)
EDIT: Upon further study there is a second field on top of the original field providing an initial explanation as to what the box is for ("Enter field value").
Maybe this is a wpf error?
I found that the box wasn't being updated properly on the UIMap. This can be solved by setting the application windows SearchConfinguration (only in the specific test) to always search like so:
UIMap.ApplicationWindowName.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
Apparently this can cause a performance hit but I haven't noticed!
I would look at the SearchProperties and FilterProperties of the text boxes, I suspect that two have been found and the wrong one is chosen. Or, it may be that the second window has some different values for the properties used to identify the windows.
One idea: Run the program again manually, move the first window with the text box to one corner of the screen, then close that window. Cause the window to be reopened and move it to the opposite corner of the screen; leave the window on display. Open the UI Map of the Coded UI test and use the Locate command to find the box - it should draw a blue line around the text box.
Another idea is to call FindMatchingControls (as shown in Coded UI conditional check on a method is not working) then use the properties (such as some of x, y, height, width) to select the wanted control.
I doubt if it is a WPF error, but it may be an error in the WPF application where it is not clearing up properly when the first window closes.

Including SelectionColor in TextBox

I need the property called SelectionColor in the TextBox class, for a simple Syntax Highlighter - I can't use directly a RichTextBox - it causes too many problems, that's why I try to do this.
Is there any way to make that property available for a TextBox?
If it isn't possible, I'd try to write my own, but I need an idea on how to do it, basically how it works - is it based on drawing strings over the original text?
Thanks in advance.
Basically, using a TextBox for anything but plain text is a bad idea. First of all, you will eventually get a new feature to implement which is not present in TextBox and you will have to handle it manually. After some time you will implement a custom RichTextBox or something similar.
Second, it is relatively hard to even solve the problem you mentioned. Technically, you can override painting function (which you have to do if you want new functionality for TextBox). You can then let TextBox paint itself and paint the colored text above the image. But don't do it. You will get two (maybe more) problems:
Flicker of image. Once the original textbox has drawn itself, the image can be shown on screen (if you don't use double buffering).
Text alignment. It is hard to place colored text exactly above black text, plus you can run into problems with text rendering: you will need to clear area you're drawing in.

How to replicate Windows 7 Calculators Textbox

I would like to know how it is possible to replicate the text box used in windows 7, that is when you press a symbol it adds the numbers on the upper part of the text box. I tried adding a label in the text box but it still did not replicate the same effect. Can anyone please give me a hint to what I can try to achieve the same result.
Thanks
Use two textboxes and apply events to mathematical operator buttons. (and a graphic underneath if you feel fancy)
I don't think there is a control that can be just 'added', you could alternatively create your own, using a User Control template.

Scrolling all but the top line in a multiline textbox

I have a multiline textbox in a WinForms application. What I'd like to do is always have the top line visible, even if it scrolls. Is there some trick someone knows to do this?
Fake it. Use two TextBox objects, draw your own borders. You will need to deal with wrapping to the next line yourself.
You could also copy the first X characters to a label so when the TextBox scrolls they can see the first line in the label.
Unless it is an essential feature I would try to cut it.
The simple answer; depending on the appearance you are going for is to use existing windows controls to get the effect you want.
You can use a label control above a textbox and allow the textbox to scroll.
You can use two textbox - the top with it's .multiline property set to false, while the bottom allows scrolling.
You could encapsulate this all into a user control for reuseability.
Beyond that, I think you'd be looking at a fairly large project to implement your control (or at least overriding the onPaint() event of the textbox) with the desired behavior.

Categories