extendeding the TextBox control

I get the feeling we aren't supposed to extend the TextBox control very much because there seems to be a lack of functionality available that I want and cannoy find. Either that, or I'm looking in the wrong place.

One things that I want to do is find out where the caret is in the text box because I want to implement an INSERT and OVERWRITE mode for the textbox. There are ways to see what is selected, but no way to see where the cursor position is.

I also want to manipulate the text in the textbox through special input rules such as filler characters, right or left justification and letting data "fall off" the ends of the text buffers. Not that these cannot be accomplished in other ways, but it seems there should be a "lower level" way of accessing the text buffer then the .TEXT property.

Can anyone comment on the best way to extend the TextBox control is there a better way to extend it I found the DOTGNU project and have the source from their textbox implementation. It seems to be a completely seperate implementation with a lot more features and might also be a good starting point for an extended TextBox.

Thanks

Bill

 



Answer this question

extendeding the TextBox control

  • Shane Colin

    Well, the big thing that I want to do is two-fold. I want to pad out the text box to always contain MaxLength character using a supplied pading character. I also want the textbox to "push" characters off the end of the field if it is already full. Additionally, it would be nice if It would support insert and overwrite modes (it in fact does not support it natively) and be able to justify the characters to either the left or the right.

    There may well be a control out there that does all this, but the TextBox control does not. Many of these items can be accomplished by overriding the Text property and some others by overriding ProcessCmdKey. Ideally however, I would like to be able to intercept the text that is about to be painted and modify it (adding the padding characters or justifying it). Another problem is that after letting ProcessCmdKey handle a keystroke, the modified text is NOT AVAILABLE via the Text Property. I don't know WHEN it would in fact be available, but this is also a hinderance because I dont really have an opportunity to manipulate the text at that time.

    The other Process overriables dont seem to help, but maybe I'm misunderstanding one of them. Also, if there is an edit type of control that is more designed for extending, I may take a look at it. I'm considering taking the TextBox implementation from DOTGNU as well.

    Any help, pointers or comments are greatly appreciated.

    Bill

     


  • Shoaib Aleem

    To do everything you want you are probably better off finding another control to extend. However, this link might help you out with understanding the Process events, and when the Text property is updated: http://blogs.msdn.com/jfoscoding/archive/2005/01/24/359334.aspx

  • Tea

    When SelectionLength is 0 (i.e. no text is selected), SelectionStart is the current location of the caret. However, I am pretty sure the standard textbox already responds to INSERT and OVERWRITE if the user presses the INSERT key.

    I'm not sure what you mean by a "lower level" way of accessing the text buffer than the text property. The text property can be manipulated pretty much like a character array if that is what you're looking for, although using the built-in functionality of the string class is much more flexible. You can handle KeyDown/Up/Press event s to preprocess incoming text, and handle TextChanged for postprocessing. What else do you think should be available

    The only thing you have to look out for when extending the TextBox control is painting. The Paint event of the textbox is never fired under normal circumstances, because the painting is handled by the operating system directly. You can force the paint event to be called by setting the textbox to be owner drawn, but then you lose all of the standard painting and have to do everything yourself.



  • Seabhcan

    There is very little what you can do to extend the textbox. Most of its functionality is hardcoded in the OS. If you are planning to do any custom painting, I can assure that the option are very very limited. If you are planning to just alter the text using the selected font, well, that's possible. But any more customization is nearly impossible. Even user painting doesn't seem to work well, since some of the Windows message keep on painting over or under your own custom painting.

    Also, custom painting will eventually boil down to writing the thing completely from scratch since you don't have access to the internal (paint)state of the textbox.



  • Dukebaby

    Have you considered MaskedTextBox it has some of the functionality you want like the Insert/Overwrite modes.

  • extendeding the TextBox control