Windows Develop Bookmark and Share   
 index > Windows Forms General > (partially) formatting radiobutton/checkbox text in vb.net
 

(partially) formatting radiobutton/checkbox text in vb.net

Hi all,

I've been searching for this for hours and start to wonder if it is possible:

I need some basic text formatting (bold/italic) on the text of radiobuttons and checkboxes. Of course I know I can set the font property for the whole text, but how can I format just a part of the text ?



I guess I need I to create a custom/inherited control using rich text, but I really can't find any relevant starting point. Any help would be appreciated!

regards

chaosvg

chaosvg  Saturday, October 15, 2005 8:11 PM
1: Derive your own class from the RadioButton and CheckBox control classes and override the OnPaint method.

2: Create a dedicated text painting class that does any kind of text rendering that you want. You could create a static method that takes a (formatted) String, a Graphics object and a Rectangle as arguments.

3: Call the text painting method from the overridden OnPaint methods.
Anders Tornblad  Sunday, October 16, 2005 1:43 PM
1: Derive your own class from the RadioButton and CheckBox control classes and override the OnPaint method.

2: Create a dedicated text painting class that does any kind of text rendering that you want. You could create a static method that takes a (formatted) String, a Graphics object and a Rectangle as arguments.

3: Call the text painting method from the overridden OnPaint methods.
Anders Tornblad  Sunday, October 16, 2005 1:43 PM
Thank you Anders,

(1) I found an interesting article about this here:
http://visualbasic.about.com/od/usingvbnet/l/aa010903a.htm
Thing is, more info on how the radio/checkbox class (onpaint event) exactly works would make it a lot easier to override it. Eg in the article above I found the coördinates for the square. Finding this out with trial and error is quite time consuming.
(I think I know the answer, it's unavailable?)

(2) That's what I'm wondering about now. My first idea would be to draw rtf, which would make it a powerful reusable component. Until now, I found now usable function or class to do so. Any hints in that area are very welcome.
If the above is too complicated (as I suppose), I will stick to what I really need: bold and italic. I will use html like tags <b> </b> and <i> </i> to change fonts in the onpaint event.

Thanks for helping me out
chaosvg


chaosvg  Sunday, October 16, 2005 2:19 PM
The size of the checkbox glyph (which I assume is what you mean by the square) is obtainable through the static CheckBoxRenderer.GetGlyphSize method. You can also use one of the CheckBoxRenderer.DrawCheckBox method variants for drawing the glyph within your overridden OnPaint method, then draw your text manually, first having subtracted the size of the Glyph from the ClientRectangle of the CheckBox.

Unfortunately the CheckBoxRenderer is a sealed class, so you cannot derive your own renderer class from it. It would be a nice thing to derive a class from CheckBoxRenderer, then use something like myCheckBox1.Renderer = new MyCheckBoxRenderer(). Or, since the methods of CheckBoxRenderer are all static, something like myCheckBox1.RendererType = typeof(MyCheckBoxRenderer)...
Anders Tornblad  Sunday, October 16, 2005 3:12 PM
Something like this perhaps:



private class MyCheckBoxRenderer
{
internal static void RenderText(string Text, Graphics g, Rectangle extent)
{
// TODO: Do my painting
}
}

public class FormattedCheckBox : CheckBox
{
protected override void OnPaint(PaintEventArgs pevent)
{
// TODO: Check the base.CheckState among other things to get a nice CheckBoxState for painting CheckBoxState
currentState = CheckBoxState.UncheckedNormal;

CheckBoxRenderer.DrawParentBackground(pevent.Graphics, ClientRectangle, this);

// TODO: Find a location for the glyph that's nicer than Point(0, 0)
Size glyphSize = CheckBoxRenderer.GetGlyphSize(pevent.Graphics, currentState);
CheckBoxRenderer.DrawCheckBox(pevent.Graphics, new Point(0, 0), currentState);

Rectangle availableForText = ClientRectangle;
availableForText.X += glyphSize.Width;
availableForText.Width -= glyphSize.Width;

MyCheckBoxRenderer.RenderText(this.Text, pevent.Graphics, availableForText);
}
}

 

Anders Tornblad  Sunday, October 16, 2005 3:24 PM

You can use google to search for other answers

Custom Search

More Threads

• Binding application settings to control properties
• Publishing my application
• How to populate a lable with Active Directory Current user info
• WebBrowser Control & HTTP Response
• ASP.NET Web Application on Linux Server
• Cancel Selected Index change ListView
• Music
• Transparency
• MDIParent Problems
• Setting variables on Parent form