在C#中更改字体样式(粗体,斜体,粗体斜体) [英] Change Font Style (bold, Italic, bold italic) in C#

查看:2964
本文介绍了在C#中更改字体样式(粗体,斜体,粗体斜体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  在C#中 改变字体样式

Possible Duplicate:
change font style in c#

您好我想一个标签的字体样式更改为粗体,斜体等。在一个aspx页面使用一个DropDownList。请让我知道我做错了什么在这里,因为没有什么似乎工作。

HI there I want to change the font style of a label to either bold, italic, etc.. using a dropdownlist in an aspx page. Please let me know what I'm doing wrong here, because nothing seem to work.

我的code:

public void button_click(object sender, EventArgs e)
{
    var select= font1.SelectedItem.Value;
    if(select=="Bold")
    {
        // I have the following methods of doing this; None works for me.
        label1.Style["Font-Weight"]="bold";
        //I also tried this:
        label1.Font = new Font(label1.Font, FontStyle.Bold);
        //and this:
        this.label1.Font= new Font(this.label1.Font, FontStyle.Bold);
    }
}

在使用.Font我得到无效的参数错误,对于字体的参数必须是这样的:字体(字符串,浮点数)。另外,请记住,把实际尺寸参数列表的方法是不是我想做的事情。像这样

When using .Font I get errors for invalid arguments, that the arguments for Font must be like this: Font(string, float). Also, keep in mind that the method of putting the actual size in the argument list is not what i want to do. Like this

label1.Font=new Font("Arial", 16, FontStyle.Bold);
//I don't want to change the font family or sixe of the label's text

所以,这就是我有什么,我在一个果酱。请赐教。谢谢

So thats what I have, I'm in a jam. Please enlighten me. Thanks

推荐答案

一个更好的办法是改变的的CssClass 的标签,并有符合你想要的造型是定义不同的CSS类(在这将被包含在你的页面创建一个单独的CSS文件)。

A better approach would be to change the CssClass of the label and have different CSS classes defined that match the styling you want (in a separate CSS file that would be included in your page).

大部分你所描述通常是使用CSS和JavaScript,在这里你不需要往返服务器,以获得用户界面的变化在客户端(浏览器)做了什么。

Much of what you describe is normally done on the client side (browser) using CSS and javascript, where you don't need to round-trip to the server in order to get UI changes.

这篇关于在C#中更改字体样式(粗体,斜体,粗体斜体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆