Textbox Character Wrap?

Richard Crist

Regular
Joined
Dec 10, 2004
Messages
97
Location
near Nashville, TN
I've searched the forum but can't seem to find information about textbox character wrapping. I need to be able to have a textbox control that character wraps instead of word wraps. That is, when the textbox displays the text it wraps the text on a character boundary, not a word boundary. I must display EDI X12 data at work and word wrapping can be very misleading.

I've found precious little on the web (either MSDN or Google) about .NET textbox character wrapping. Can someone point me in the right direction if I need to code a custom inheritable textbox control to do this? I'm not yet hip to the base methods I would need to mess with, maybe paint or draw or display or something? Surely someone else has wanted or needed to characater wrap instead of word wrap a text box. :eek:

Thank you for any and all help! :)
 
I would say use a standard multiline word wrapping textbox but use the keydown /keypress etc. event to add spaces to the words to force a wrap after x characters. The difficulty then comes in removing these spaces again if characters are deleted.

Some sort of tracking of the spaces therefore needs putting in place. Off the top of my head I can't remember what events there are but you could do with a before keypress event that removes spaces and a after keypress event that adds them.

HTH

:)
 
Seeing as the label control can character wrap
It can? If so, thats news to me.

If you are only displaying the text, you can insert line breaks where needed, which would be simplified by a non-proportional font (i.e. courier new). If you need to be able edit it I don't know what to tell you. You might want to look up the Graphics.MeasureString method. Could be of use.
 
It can? If so, thats news to me.
My mistake. I tested this using words that were too long to fit on one line, so it appeared to wrap on a character level.

Good catch. My bad. Sorry about. I deleted my original reply seeing as the info was wrong.
 
Multiple text boxes might be a way to go though with one having had the additional characters added and one with the actual typed in string. The GotFocus of TextBoxDisplay could hide itself, unhide TextBoxOriginal and put the focus in that box. The lost focus event of TextBoxOriginal could then hide itself, show TextBoxDisplay and assign it text with spaces added after each c number of characters to force a wrap at that point thus giving the illusion of character wrapping.

This should be pretty easy to set up I think and work quite well.

:)
 
marble_eater said:
If you are only displaying the text, you can insert line breaks where needed, which would be simplified by a non-proportional font (i.e. courier new). If you need to be able edit it I don't know what to tell you. You might want to look up the Graphics.MeasureString method. Could be of use.

marble_eater,

Thanks for your reply. :) I'm afraid I'm going to have to go with your Graphics.MeasureString method unless something else pops up. Using the string measure method was all that I had personally found to be feasible, amongst all the control methods I had waded through. Most of the time I will be displaying predetermined and/or uneditable text, so I could use the measure string method by adding one character at a time to a buffer and then adding a newline as I go as needed to wrap at the character level. And, as you suggested, I will be using courier new as the font. I think that I will also have to incorporate 007's keypress/keydown idea, because some text boxes will initially display the text, but also be open for editing the text, which I still need to wrap at the character level. I'll reply to 007's post about that idea.

Thank you again for your help, and let me know if you happen to find another or better way, and I'll do the same. :cool:
 
mark007 said:
I would say use a standard multiline word wrapping textbox but use the keydown /keypress etc. event to add spaces to the words to force a wrap after x characters. The difficulty then comes in removing these spaces again if characters are deleted.
Some sort of tracking of the spaces therefore needs putting in place. Off the top of my head I can't remember what events there are but you could do with a before keypress event that removes spaces and a after keypress event that adds them.
Multiple text boxes might be a way to go though with one having had the additional characters added and one with the actual typed in string. The GotFocus of TextBoxDisplay could hide itself, unhide TextBoxOriginal and put the focus in that box. The lost focus event of TextBoxOriginal could then hide itself, show TextBoxDisplay and assign it text with spaces added after each c number of characters to force a wrap at that point thus giving the illusion of character wrapping.
This should be pretty easy to set up I think and work quite well.

mark007,

As I mentioned in my reply to marble_eater, I think that I will also have to use your keypress idea as I will not only be displaying static text, but editable text in addition. Your ideas of control switching may come in handy. I will experiment and get back.

Thank you! :)
 
mark007 said:
Here's an example of my thinking. Seems to work pretty well. Could easily compile it to a custom control.

Intersting, but there are a couple caveats:

1. It will only work with fixed-width fonts and you have to hard code the characters per line into the code.

I guess the real trick would be getting this to work with ANY font as I would suspect most people use proportionate width fonts.

Maybe tapping into the GDI might be the only way to make things foolproof, but that would be a real challenge especially in the case of multiline scrolling textboxes.

Regardless, your code is kinda cool.

:)
 
cincyreds said:
Intersting, but there are a couple caveats:

1. It will only work with fixed-width fonts and you have to hard code the characters per line into the code.

I guess the real trick would be getting this to work with ANY font as I would suspect most people use proportionate width fonts.

Maybe tapping into the GDI might be the only way to make things foolproof, but that would be a real challenge especially in the case of multiline scrolling textboxes.

Regardless, your code is kinda cool.

:)

Yes, for a non-fixed-width font it won't work though the measure string method mentioned earlier could perhaps be used to find the number of characters before a space is needed.

If I have some freetime or need a break from what I'm working on I might try and knock something more foolproof up and compile it to a custom control.

:)
 
Back
Top