Play With String Of Button/Label

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheshwag
    New Member
    • Aug 2010
    • 71

    Play With String Of Button/Label

    Suppose if my form has button and label and Button1 has Text Property’s Text is “Hello” Than If I wants to make some character of “Hello” text in Bold,Underline and size increment than How to do it as below?.

    Example of Button1 string/text

    Code:
    Normal                     Put some Efforts on
    ----------------------------------------------------
    
    Hello                       H[B][U]E[/U][/B]llo Or He[B]L[/B]lo
    Actually Here i unable to demonstrate well but trying to explain in words that the above "Hello" words available on Put some Efforts on Columns. If I wants "E" Bold and underline or "L" on Bold or increment in size out from "Hello" words than how to do it?.

    I have to perform this task on both button and label.

    This is for winform application.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I'm not entirely certain you can... the font and such controls the entire string. That said, you can easily inherit from button and override the paint events, allowing you to draw your own text, though you'll also have to draw your own button as well.

    Oooh! I just had a crazy idea... you could do something like this...

    Code:
        public class TestButton : Button
        {
            public string FormattedText { get; set; }
    
            protected override void OnPaint(PaintEventArgs pevent)
            {
                base.OnPaint(pevent);
    
                Graphics g = pevent.Graphics;
    
                // Draw formatted text here
            }
        }
    As long as the button's text property is blank (which you can actually override and enforce), the only text that would appear would be your FormattedText string. You can use whatever you like there to control it, but if you used BBCode you could do something like...

    Code:
    TestButton t = new TestButton();
    t.FormattedText = "H{b}{u}E{/u}{/b}llo Or He{b}L{/b}lo";
    (I used curly braces here 'cause the forum will convert my square brackets into formatted text inside the quote block.. *sigh* :D)

    Then it'd just be a matter of parsing the string... something along the lines of...

    Code:
    Font f = this.Font;
    string formatStr = this.FormattedString;
    string currText = "";
    for (int i = 0; i < formatStr.Length; i++)
    {
      string currCode = "";
      if (formatStr[i] = '[')
      {
        currCode = ...; // read until next ], figure out what the code is
      }
    
      if (currCode != "")
      {
        switch (currCode)
        {
          case "b":
            f.Bold = true;
            break;
          case "/b":
            f.Bold = false;
            break;
          ...
        }
      }
      else
      {
        currText += formatStr[i];
      }
    
      if (all our open tags have been closed)
      {
        g.DrawString(currText, f, new SolidBrush(this.ForeColor), x, y);
        x += g.MeasureString(currText, f, ... whatever else this needs ...);
        currText = "";
        tagsOpened = false;
        tagsClosed = false;
      }
    }
    I guess y could be the position of the text centered on the button. For x, you'd need to find the position of the text centered on the button, then add to it as you built and drew your string.

    Something like that... that's just an idea, don't take that code as complete. Hopefully it'll get you started though :)

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      Glad you found this helpful :) I just wanted to post a couple of follow ups...

      1) You could also consider using the Rich Text format instead of BBCode. Personally I find it annoying to parse but it's a well documented format and it would be compatible with a RichTextBox control.

      2) Overriding the Text property on a label with the autosize property on could cause a few sizing issues. That said, you could always have it draw the text but make the foreground colour property the same as the back colour so you'd never see it, then use a different property to control the foreground colour of your formatted text.

      3) This concept would make an excellent insight. Once you finish and test your new control, perhaps consider sharing it (along with a small write up and tutorial) in the insights section :)

      Comment

      • maheshwag
        New Member
        • Aug 2010
        • 71

        #4
        Thanks Gary your post is really useful to demonstrate in various ways.

        Comment

        Working...