Including SelectionColor in TextBox - c#

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.

Related

Change the part of the node text to be bold of TreeView [duplicate]

i have "search TextBox" to search in treeview, i give result very well. But i want to get those parts get Bold which i typed in "search TextBox" of my winform.
Ex: i Typed Ram then it gives *Ram*esh .
The TreeNode class doesn't support that, its Text is always drawn with one font, the TreeView.Font. Making parts of the text bold is technically possible but very hard to get right. You need to enable custom drawing with the TreeView.DrawMode property and DrawItem event, there's a good example of it in the MSDN Library article.
That's the easy part, the hard problem is that the node is too small to fit the text after you draw parts of it in a bold font. TreeView is missing a "MeasureNodeText" event that would allow you to ask for enough space. The only workaround for that is to lie about the node text and make it artificially wider by prefixing characters. Which you then don't draw in the DrawItem event. Very hard to get consistently right, you'll want to consider a fixed pitch font instead.
I cannot recommend you pursue this unless the feature is really important to you. This otherwise explains why you never see this feature in other programs. Consider changing the color instead of the font weight too. Still hard to glue the pieces together btw.

Replace Text in Label With Inline Image In C#

My program has several Label controls that are updated to have different text every so often. I have a few icons that I want to reference within the text. I figured that instead of just displaying "(E)" in the Label, there should be a way to replace that with the corresponding image that I have that looks like: . I figure that I need to override the Label.Paint event, but I'm not too sure how to do that properly. Every occurrence of "(E)" needs to be replaced with the image inline.
Example
Look for the (E) icon on the top. → Look for the icon on the top.
This is not a trivial exercise.
You'd have to provide a property on your derived label class to attach the image(s) to the text.
Embed some sort of token(s) in the text to represent the image(s).
In the OnPaint you have to parse the text for the token(s)
Do a graphics.MeasureString() for each bit of text between the tokens. And then render it with graphics.DrawString() move to the right by the width of the text, render the image based on the token using one of the many graphics.DrawImage() overrides - move to the right by the width of the image and repeat.

Drawing Text onto Panel similar to MS Paint

I am in the process of learning C# as I am most familiar with C++ so if there is a simple answer to this question please forgive me. Also thank you for taking the time to read this, I appreciate it!!! :)
I am currently in the process of creating a program similar to MS Paint. I would like to be able to draw text on a panel. I've found the .DrawString() which can apparently be used to do exactly this, though in all of the examples I've seen they have coordinates predefined and do not allow the user to choose where to place it. I want this to be similar to MS Paint by allowing the user to click and drag to make the size of the box the text shall appear in. I am not concerned about allowing them to move the text around once the rectangle is drawn with the text.
I was originally thinking to have them type their string into a textbox and then draw it onto a rectangle so they could size it appropriately to fit their string since the string should wrap inside of the rectangle. So I have two questions:
How can I have the text wrap around the rectangle and obviously cut
off anything that does not fit (again similar to MS Paint)?
By drawing a rectangle (if my idea is plausible of course) it will
leave a border around the text since it is making a rectangle. I do
not want this border, I only want the text. How can I achieve this?

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.

DrawString over a TextBox [duplicate]

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?

Categories