locked
How to Change Label Font Size Programmatically in ASP.Net C# RRS feed

  • Question

  • User2083426521 posted

    Hi,

    I hope this is a little thing but I can't find the solution. I want to change the text font size into my label dynamically, can anyone help me to resolve this, I am doing following code but Receiving Error .

    Error is: CS1729: 'System.Web.UI.WebControls.FontSize' does not contain a constructor that takes '1' arguments

     

    if (bl.Save(a))
            {
                lblName.ForeColor=System.Drawing.Color.Red;
                lblName.Font = new FontSize(FontUnit.XXLarge);
                lblName.Text = "Insert Successful";
            }
            else
            {
                lblName.ForeColor = System.Drawing.Color.Red;
                 lblName.Font = new FontSize(FontUnit.Small);
                lblName.Text = "There is some Problem, Try again or Inform Developer.";
            }         

    Thanks for your help.

    Thursday, April 7, 2011 7:59 AM

Answers

All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2011 9:22 AM
  • User1705931812 posted

    Wasted a lot of time looking for an OO solution. I built the following class.

    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;
                }

    For instance:

    lbl.Font=SetFontStyle(lbl.Font, true, false, false, true);

    Will 'return' the label text as Bold and Strikeout.

    Monday, May 20, 2013 2:23 AM
  • User1147673308 posted
      FontFamily family = new FontFamily("Times New Roman");
    
    	    Font font = new Font(family, 16.0f,
    		FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
    
    	    // Set Font property.
    	    this.Font = font;
    	    this.Controls.Add(new Label() { Text = "Your Label Text", Width = 250 });
    	    this.Size = new Size(300, 200
    Monday, May 20, 2013 2:37 AM