Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone.

I'm brand new to programming as I have started 4 weeks ago.
I'm creating a spreadsheet software similar to Excel.

My problem is the following.

When the personne who will be using my software is clicking on a cell
and then changing the style of the font by pressing some icones, this style is saved in a table in memory with the name of the cell.

On opening of a saved spreadsheet or during the redisplay of the grid which contains the cells (for example during a horizontal or vertical scroll), I'm getting the informations for each cells from my table in memory and then pass them to a stub which display each cell on a picture box.

My probleme is that I do not know how to change the style of my font before drawing on the picture box the information (texte, date, value...) contained in my cell.

For the part concerning the fontstyle stored in the memory table, as it is stored as a string, I can easily extract each style from that string which would give me a list of style.

Now with that list of styles, i need to change the style of the font which will display the information contained into the cell.

I could do a

if (cellPoliceStyle = "Bold")
{
Font fontToUse = new Font(cellColumnSize, cellRowSize, style bold
//I didn't write the proper way of putting a style in this example, but it's not a problem, it's just to show what I want to do
drawstring (textToDisplay, locationX, locationY ....)
}

The real problem is that I don't want to have to write a if with Italic, another one with Bold, another one with Underline, another one with Bold + Underline...

I don't want to write every single combination of styles.

I'm really struggling on that problem and would appreciate if somebody could come up with an idea and a code example in c#

Thankyou very much for your help
Posted

FontStyle is marked with FlagsAttribute.

This means you can 'or' them together.


private FontStyle GetCellStyle(object yourOptions)
{
    FontStyle style = FontStyle.Regular;
    style = yourOptions.IsBold ? style | FontStyle.Bold : style | FontStyle.Regular;
    style = yourOptions.IsItalic ? style | FontStyle.Italic : style| FontStyle.Regular;

    return style;
}


If IsBold and IsItalic are true, you'll get a bold+italic FontStyle here.

Regular = 0, Bold = 1, Italic = 2, etc.

Cheers.
 
Share this answer
 
v2
Comments
Ticardiff 17-Aug-10 11:33am    
Thank you very much for your answer.

Cheers
TheyCallMeMrJames 17-Aug-10 11:43am    
No worries, please mark as answer if this helped you, or if you need anything else just ask. Cheers.
I built a class to enable easy ussage assuming that the bolBold, bolUnderline ... etc were always available.

C#
public static Font SetFontStyle(Font sourcefont, bool bolBold, bool bolUnderline, bool bolItalic, bool bolStrikeout)
            {
            Font ffont = new Font(sourcefont.Name, sourcefont.Size, FontStyle.Regular);

            if (bolBold)
                {
                ffont = new Font(sourcefont.Name, sourcefont.Size, ffont.Style | FontStyle.Bold);
                }
            if (bolUnderline)
                {
                ffont = new Font(sourcefont.Name, sourcefont.Size, ffont.Style | FontStyle.Underline);
                }
            if (bolItalic)
                {
                ffont = new Font(sourcefont.Name, sourcefont.Size, ffont.Style | FontStyle.Italic);
                }
            if (bolStrikeout)
                {
                ffont = new Font(sourcefont.Name, sourcefont.Size, ffont.Style | FontStyle.Strikeout);
                }
            return ffont;
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900