Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everyone; I'm in a jam with some C# code behind my .aspx page. I have a dropdownlist with font-family options (Times Roman, Courier, Comic Sans), an asp textbox,an asp label, and a button object.

Now What must be done is: The user enters text in the textbox, and selects a font-family from the dropdownlist, when the user click on the button the Onclick event performs a textchange event and the text which was entered in the textbox will appear in the label in whatever font-family style was chosen.

My jam: I have no idea which format to use to change the font-family for C#.
lets say the dropdownlist id="drop1", TextBox id="Text1", button onClick="Text_Change", and Label id="label1" (Never mind that i did not write the exact code)
In Code behind snippet:

public void Text_Change(object sender, EventArgs e){
string f = drop1.SelectedItem.Value;
if(f== "Times Roman")
   label1.FontFamily= new FontFamily("Times Roma);
}


I have a separate method for making the TextBox text appear in the Label so never mind that step.
This is the format for changing font family i keep getting in books, and on the web. However, I just don't know how they used it because it doesn't work for me. .FontFamily is not being recognize by the complier. Yes i have all namespaces imaginable.

My question: Can anyone please explain to me how to change the font-family of the label using C# code. No javascript code. And would it be similar to changing the font-size, and style?

Thanks...good explanations are very appreciated :)
Posted
Updated 8-Apr-11 9:30am
v2
Comments
Wendelius 8-Apr-11 15:30pm    
Readability edit

For Web form
label1.Font.Name = "Times New Roman";


And Win form
FontFamily ff = new FontFamily("Times New Roman");
Font f = new Font(ff, 10.0f);

label1.Font = f;


Hope this solve your problem!
 
Share this answer
 
Try this.

FontFamily ff = new FontFamily("Times New Roman");
Font f = new Font(ff, 10.0f);

label1.Font = f;
 
Share this answer
 
Comments
Zen_2011 8-Apr-11 16:07pm    
when u say Font f= new Font (ff, 10.0f); Is the 10.0f changing the font size? If so, how do I use this function without changing the font size, because "Font" takes in more that one argument and it does not work for just defining the Font-family only.
web works 8-Apr-11 18:34pm    
Yes, it takes second parameter too.
For more info: http://www.w3.org/TR/CSS2/fonts.html
Zen_2011 8-Apr-11 20:47pm    
It doesn't work for me, get two errors saying: Property index or indexer 'System.Web.UI.WebControls.WebControl.font cannot be assigned to --it is read only' and "Cannot implicitly convert type 'System.Drawing.Font' to 'System.Web.UI.WebControls.FontInfo'.
And other thing is I am planning to have a different dropdownlist to handle font size changes. The List you gave has a lot of info thanks. But its info is to do with CSS styling and html, which is not what I want to do here.

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