473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Play With String Of Button/Label

maheshwag
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

Expand|Select|Wrap|Line Numbers
  1. Normal                     Put some Efforts on
  2. ----------------------------------------------------
  3.  
  4. Hello                       HEllo Or HeLlo
  5.  
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.
Feb 13 '11 #1

✓ answered by GaryTexmo

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...

Expand|Select|Wrap|Line Numbers
  1.     public class TestButton : Button
  2.     {
  3.         public string FormattedText { get; set; }
  4.  
  5.         protected override void OnPaint(PaintEventArgs pevent)
  6.         {
  7.             base.OnPaint(pevent);
  8.  
  9.             Graphics g = pevent.Graphics;
  10.  
  11.             // Draw formatted text here
  12.         }
  13.     }
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...

Expand|Select|Wrap|Line Numbers
  1. TestButton t = new TestButton();
  2. 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...

Expand|Select|Wrap|Line Numbers
  1. Font f = this.Font;
  2. string formatStr = this.FormattedString;
  3. string currText = "";
  4. for (int i = 0; i < formatStr.Length; i++)
  5. {
  6.   string currCode = "";
  7.   if (formatStr[i] = '[')
  8.   {
  9.     currCode = ...; // read until next ], figure out what the code is
  10.   }
  11.  
  12.   if (currCode != "")
  13.   {
  14.     switch (currCode)
  15.     {
  16.       case "b":
  17.         f.Bold = true;
  18.         break;
  19.       case "/b":
  20.         f.Bold = false;
  21.         break;
  22.       ...
  23.     }
  24.   }
  25.   else
  26.   {
  27.     currText += formatStr[i];
  28.   }
  29.  
  30.   if (all our open tags have been closed)
  31.   {
  32.     g.DrawString(currText, f, new SolidBrush(this.ForeColor), x, y);
  33.     x += g.MeasureString(currText, f, ... whatever else this needs ...);
  34.     currText = "";
  35.     tagsOpened = false;
  36.     tagsClosed = false;
  37.   }
  38. }
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 :)

3 2833
GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1.     public class TestButton : Button
  2.     {
  3.         public string FormattedText { get; set; }
  4.  
  5.         protected override void OnPaint(PaintEventArgs pevent)
  6.         {
  7.             base.OnPaint(pevent);
  8.  
  9.             Graphics g = pevent.Graphics;
  10.  
  11.             // Draw formatted text here
  12.         }
  13.     }
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...

Expand|Select|Wrap|Line Numbers
  1. TestButton t = new TestButton();
  2. 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...

Expand|Select|Wrap|Line Numbers
  1. Font f = this.Font;
  2. string formatStr = this.FormattedString;
  3. string currText = "";
  4. for (int i = 0; i < formatStr.Length; i++)
  5. {
  6.   string currCode = "";
  7.   if (formatStr[i] = '[')
  8.   {
  9.     currCode = ...; // read until next ], figure out what the code is
  10.   }
  11.  
  12.   if (currCode != "")
  13.   {
  14.     switch (currCode)
  15.     {
  16.       case "b":
  17.         f.Bold = true;
  18.         break;
  19.       case "/b":
  20.         f.Bold = false;
  21.         break;
  22.       ...
  23.     }
  24.   }
  25.   else
  26.   {
  27.     currText += formatStr[i];
  28.   }
  29.  
  30.   if (all our open tags have been closed)
  31.   {
  32.     g.DrawString(currText, f, new SolidBrush(this.ForeColor), x, y);
  33.     x += g.MeasureString(currText, f, ... whatever else this needs ...);
  34.     currText = "";
  35.     tagsOpened = false;
  36.     tagsClosed = false;
  37.   }
  38. }
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 :)
Feb 14 '11 #2
GaryTexmo
1,501 Expert 1GB
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 :)
Feb 15 '11 #3
Thanks Gary your post is really useful to demonstrate in various ways.
Feb 16 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: craig | last post by:
i want to make a button that will start an embedded windows media player playing AND in full screen. i can do it with TWO buttons: <input onclick="MediaPlayer.controls.play();" type="button"...
4
by: Stuart Norris | last post by:
Dear Readers, I am attempting to draw box around some text using unicode on multiline label. The label is forty characters wide and 12 lines deep. I have been trying to draw a box around text...
1
by: Divya | last post by:
Hello This is my 1st project where I have to create a Webcontrol. I have created a simple custom control with a button and 2 labels added to a panel. My problem is that the event handler that I...
3
by: Paulers | last post by:
Hello, I have an arraylist that looks like this: 1,4,6 I have buttons called button1, button4, button6. I would like to change the text on the buttons that corispond to the numbers in the...
2
by: Sue | last post by:
All I have a bunch of label controls on my form. All those label controls will start with the id "lblEffDt_". (lblEffDateValue_ddlDeliveryMethod, lblEffDateValue_txtPriority etc...) I want...
2
by: mfsiddiq | last post by:
Hi I want to know if there is any way a string can be interpreted as function name.I need this to call functions dynamically.Below is a code snippet which works fine if the function is in the same...
3
by: chandan | last post by:
Hi, Is any way to palce a control(button/label) at a location on webPage in runtime?? In page_load envet I am adding a button on the page in the contol collection of a panel but not able to...
3
by: MLN | last post by:
Hello everyone, I am trying to create a button control with x^y as button label. How to set this text with superscript as label for the button control? Any help in this direction is highly...
2
by: somacore | last post by:
I have a webform in C#. This form has both a gridview and a formview. The formview displays the details of the selected gridview index. No problem there. On the formview is a button. When this...
5
by: Mahyog | last post by:
Hello everyone, I want to dynamically change location of button, label and panel within the form that can be automatically appear when I restore and maximize the form. Please guys help me. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.