سؤال

My Question is to how to extend a TextBox such that it may start behaving like RichTextBox? There can be various properties that RichTextBox may add: appearance mainly. Should I use this kind of method where I extend the TextBox class and create a basic TextBox which would contain several other textboxes which would behave like a big container node containing small specialized nodes?

For starters, to have texts with alternate color after '+', I ve used this way:

class CustomTextBox : TextBox
{
 List<TextBox> _textboxes = new List<TextBox>();
 string _Text="";
 List<Color> colorlist = new List<Color>();
 public override string Text
 {
   get{return this._Text;}
   set{this._Text = value;}
 }
 public CustomTextBox()
 {
   foreach(Color color in (Color[]) Enum.GetValues(typeOf(Color)))
   {
     colorlist.add(color);
   }
   this.KeyUp+= new KeyUpEventHandler(TextChangedCheck);
 }
 int i=0;
 private void TextChangedCheck(object sender, KeyUpEventArgs e)
 {
   if(e.KeyData == Keys.Add)
   {
     TextBox Temp = new TextBox();
     Temp.Text = this.Text;
     this.Text = "";
     Temp.ForeColor = colorlist[i];
     i++;
     this._textboxes.Add(Temp);
     this.Controls.Add(_textboxes[i]);
     e.Handled = true;
   }
 }
}

EDIT:

The MAIN purpose of this question is to extend a TextBox using its own properties to have a RTB like behavior and not using Graphics or related.

هل كانت مفيدة؟

المحلول

I'd have to guess what you really want to achieve here.

But if you want a Control with formatted text (FT) and really really really don't want a RichTextBox, I think you shouldn't create a new, embedded TextBox for every piece of FT.

Instead you should write the FT yourself, maybe on a panel with DrawString and use only one Textbox for text entry. Interesting project, really, once one accepts the challenge. Of course you must think your format through and also consider all sorts of interface questions..

Last week I have avoided a RTF for a tiny help system by using a ListView; it formats by line only, using the first character to indicate the format from a list of a dozen or so.. Works fine, but only one format per line.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top