none
subscript / superscript? RRS feed

  • Question

  • Dear All

    Is there a way of making the text label of a checkbox have subscripts and/or superscripts?

    Some code would help please.

    cheers,
    prasad..
    Monday, November 27, 2006 9:34 AM

Answers

  • If you want button functionality then use a button.  A user would never assume that a RTB is clickable.  This is bad UI design.  Create a button and drop an image on it containing the text you want.  You can programmatically create an image by using the Graphics object and then apply the image to the button.  In order to get the super/subscripting that you want you'll need to generate multiple strings and combine them together using DrawString.  This actually gets really complex because you need to measure each string using the appropriate font (the font changes when you switch from normal to super/subscripts) and then track the position in the image where you are placing them.  You'll also need to handle clipping and centering issues.  Here is some sample code to get you started.  You'll need to work on getting the text to display properly for your needs.

    private Image CreateImage ( string text, Rectangle rect, Font fnt, Color clr )

    {

    Image img = new Bitmap(rect.Width, rect.Height);

    using (Graphics gfx = Graphics.FromImage(img))

    {

    using (Brush brush = new SolidBrush(clr))

    {

    StringFormat fmt = new StringFormat();

    fmt.Alignment = StringAlignment.Center;

    gfx.DrawString(text, fnt, brush, 0, 0, fmt);

    };

    };

    return img;

    }

    private void btnApply_Click ( object sender, EventArgs e )

    {

    btnTarget.Image = CreateImage(textBox1.Text, btnTarget.ClientRectangle, btnTarget.Font, btnTarget.ForeColor);

    btnTarget.ImageAlign = ContentAlignment.MiddleCenter;

    }

    I recommend that you get a good book on this topic such as Programming Windows Forms with C#.

    Michael Taylor - 11/28/06

    Tuesday, November 28, 2006 1:42 PM

All replies

  • Not the entire text like so:  ft² because the font is applied to the entire text.  You'd need to either draw the control yourself or create a custom control.  You could also lay a label next to the checkbox text but now you'll be getting into layout issues.

    One alternative is to use the extended character codes.  This only works for a couple of values but you can make superscript 2 and 3 values I know.  You can do this by holding ALT while typing the 3-4 digit character code.  The easiest way to get this value is to use charmap in WIndows.

    BTW please post WinForms related questions in the WinForms forum.  Please reserve the C# forum for C# related questions.  I'm moving your post to the WinForms forum.  Thanks.

    Michael Taylor - 11/27/0

    Monday, November 27, 2006 1:43 PM
  • hi

    sorry for posting it in the wrong section.
    If instead of checkboxes I use rich text boxes, is it possible to have subscripts and superscripts?

    Infact I need to have only subscripts.

    cheers
    prasad..
    Monday, November 27, 2006 1:56 PM
  • RTB can handle super/subscripts but presumably if you are currently using a check box then you want a yes/no|on/off option which a RTB won't give you.  Nevertheless RTB can handle the formatting you want.

    Michael Taylor - 11/27/06

    Monday, November 27, 2006 3:20 PM
  • i want to use RTB as a button. So when when single clicked I want an eventhadler and when double clicked I want another event handler.

    The textof the RTB needs to have subscripts and superscripts.

    How can I do it?

    cheers,
    prasad..
    Tuesday, November 28, 2006 11:10 AM
  • You don't seem to be happy with Michael's answer.  What's the problem?
    Tuesday, November 28, 2006 11:31 AM
    Moderator
  • its not that I wasnt happy...but I donot know how to handle RTB, I just created one dynamically (code) and now I want to write something into it and make it uneditable. I donot know how to write text with subscript and superscript into it!

    prasad..
    Tuesday, November 28, 2006 11:57 AM
  • Don't go the RTB route, you'll have loads of trouble trying to turn it into a button or checkbox.  If charmap.exe doesn't give you what you need (have you tried it?), draw the text the way you want it in a painting program and put that image on the button or checkbox...
    Tuesday, November 28, 2006 12:03 PM
    Moderator
  • actually the text from the boy comes dynamically from the code (inturn reads from the xnl file) and is a dynamic combination of 2 or 3 strings...so I cant draw it as you say.

    please suggest...I know that the guy BTW I just need subscripts.

    and BTW once I have the RTBs as buttons and define event handlers for them..I dont need to touch them again...just need to do some other things in the event handlers thats all.
    Do you forsee any problems?

    cheers
    prasad..
    Tuesday, November 28, 2006 12:12 PM
  • If you want button functionality then use a button.  A user would never assume that a RTB is clickable.  This is bad UI design.  Create a button and drop an image on it containing the text you want.  You can programmatically create an image by using the Graphics object and then apply the image to the button.  In order to get the super/subscripting that you want you'll need to generate multiple strings and combine them together using DrawString.  This actually gets really complex because you need to measure each string using the appropriate font (the font changes when you switch from normal to super/subscripts) and then track the position in the image where you are placing them.  You'll also need to handle clipping and centering issues.  Here is some sample code to get you started.  You'll need to work on getting the text to display properly for your needs.

    private Image CreateImage ( string text, Rectangle rect, Font fnt, Color clr )

    {

    Image img = new Bitmap(rect.Width, rect.Height);

    using (Graphics gfx = Graphics.FromImage(img))

    {

    using (Brush brush = new SolidBrush(clr))

    {

    StringFormat fmt = new StringFormat();

    fmt.Alignment = StringAlignment.Center;

    gfx.DrawString(text, fnt, brush, 0, 0, fmt);

    };

    };

    return img;

    }

    private void btnApply_Click ( object sender, EventArgs e )

    {

    btnTarget.Image = CreateImage(textBox1.Text, btnTarget.ClientRectangle, btnTarget.Font, btnTarget.ForeColor);

    btnTarget.ImageAlign = ContentAlignment.MiddleCenter;

    }

    I recommend that you get a good book on this topic such as Programming Windows Forms with C#.

    Michael Taylor - 11/28/06

    Tuesday, November 28, 2006 1:42 PM
  • thanks for the code. You are right...I think I should rather go for buttons...but in my context I thought that checkboxes would make sense so I went ahead and populated with checkboxes.

    Thank you so much for your help. Will try it and let you know. For a while now, I will go ahead without the subscripts because its just a minor part of the full proj.

    Thanks again
    cheers
    prasad..
    Tuesday, November 28, 2006 2:04 PM
  • Hi ,Similarly Superscript for Listbox Item w.r.t when checkbox checked i have to pass letter 'M' in superscript formate to listbox item. if not checked i don't want superscript for same.

     

    some code please

    regards

    Ram

     

     

    Thursday, September 16, 2010 5:58 AM
  • Can some one help me on this  Revers Superscript in listbox items when i check checkbox i want to show superscript if not no need  this is windows C# code.
    Thursday, September 16, 2010 6:03 AM