Click here to Skip to main content
15,910,211 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I would like to put some text on a label and increase the font size at runtime.

How can I do this?
Posted
Updated 18-Nov-11 2:06am
v2
Comments
[no name] 16-Nov-11 9:06am    
What environment? Web, Windows, WPF?
Himu from Orissa 17-Nov-11 1:34am    
windows

For WinForms, you need to set the 'Font'property of the Label with new size parameters instead of setting the Size property directly. The Size property is read-only in Winforms.

Heres the code for it

C#
float size = 100;
label1.Font = new Font(FontFamily.GenericSerif, size);


Use the Font(....) overloads as per your requirement

Try to be more clear on your question next time. Cheers !!!
 
Share this answer
 
v3
Comments
Himu from Orissa 17-Nov-11 1:31am    
its showing that size is only read only
bbirajdar 17-Nov-11 4:02am    
The size property is not read-only.

Here is the metadata for the class. Check this

namespace System.Web.UI.WebControls
{
// Summary:
// Encapsulates the font properties of text. This class cannot be inherited.
[TypeConverter(typeof(ExpandableObjectConverter))]
public sealed class FontInfo
{
// Summary:
// Gets or sets a value that indicates whether the font is bold.
//
// Returns:
// true if the font is bold; otherwise, false. The default value is false.
[WebSysDescription("FontInfo_Bold")]
[WebCategory("Appearance")]
[DefaultValue(false)]
[NotifyParentProperty(true)]
public bool Bold { get; set; }
//
// Summary:
// Gets or sets a value that indicates whether the font is italic.
//
// Returns:
// true if the font is italic; otherwise, false. The default value is false.
[NotifyParentProperty(true)]
[WebCategory("Appearance")]
[DefaultValue(false)]
[WebSysDescription("FontInfo_Italic")]
public bool Italic { get; set; }
//
// Summary:
// Gets or sets the primary font name.
//
// Returns:
// The primary font name. The default value is System.String.Empty, which indicates
// that this property is not set.
//
// Exceptions:
// System.ArgumentNullException:
// The specified font name is null.
[Editor("System.Drawing.Design.FontNameEditor, System.Drawing.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[TypeConverter(typeof(FontConverter.FontNameConverter))]
[WebCategory("Appearance")]
[DefaultValue("")]
[WebSysDescription("FontInfo_Name")]
[NotifyParentProperty(true)]
[RefreshProperties(RefreshProperties.Repaint)]
public string Name { get; set; }
//
// Summary:
// Gets or sets an ordered array of font names.
//
// Returns:
// An ordered array of font names.
[TypeConverter(typeof(FontNamesConverter))]
[RefreshProperties(RefreshProperties.Repaint)]
[NotifyParentProperty(true)]
[WebSysDescription("FontInfo_Names")]
[WebCategory("Appearance")]
[Editor("System.Windows.Forms.Design.StringArrayEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string[] Names { get; set; }
//
// Summary:
// Gets or sets a value that indicates whether the font is overlined.
//
// Returns:
// true if the font is overlined; otherwise, false. The default value is false.
[NotifyParentProperty(true)]
[WebCategory("Appearance")]
[DefaultValue(false)]
[WebSysDescription("FontInfo_Overline")]
public bool Overline { get; set; }
//
// Summary:
// Gets or sets the font size.
//
// Returns:
// A System.Web.UI.WebControls.FontUnit that represents the font size.
//
// Exceptions:
// System.ArgumentOutOfRangeException:
// The specified font size is negative.
[RefreshProperties(RefreshProperties.Repaint)]
[WebCategory("Appearance")]
[DefaultValue(typeof(FontUnit), "")]
[WebSysDescription("FontInfo_Size")]
[NotifyParentProperty(true)]
public FontUnit Size { get; set; }
//
// Summary:
// Gets or sets a value that indicates whether the font is strikethrough.
//
// Returns:
// true if the font is struck through; otherwise, false. The default value is
// false.
[NotifyParentProperty(true)]
Himu from Orissa 17-Nov-11 5:44am    
but its showing so what to do
to change the font try this


Label.text="Your new text"
Label.Font.Size=14;
 
Share this answer
 
Comments
Himu from Orissa 17-Nov-11 1:31am    
its showing that size is only read only,so can not be set
bbirajdar 17-Nov-11 9:30am    
Please elaborate your question...

Are you working in WebForms or Winforms? Is the label a built in .NET Control or a custom third party control ?
Himu from Orissa 17-Nov-11 11:43am    
i m working on windows appln and the label is a built in .net control

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