C#文本框使用void更改字体样式 [英] C# Textbox using void to change font style

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

问题描述

我是编程的初学者,并尝试使用Internet在家里教自己,并尝试练习任务,我试图创建一个非常简单的文字处理器.通过字体编辑(粗体,下划线,斜体等),我发现使用了

I''m a beginner in programming and trying to teach myself at home using the Internet and such to practice on tasks and I''m trying to create a very simple word processor. With the font editing (Bold, underline, italic, etc.) I found using 

this.Font = new Font(this.Font, FontStyle.Underline);


将整个应用程序字体更改为该单一字体样式.我希望用户能够同时使用粗体和下划线,因此我尝试使用布尔值. 
例如:


changes the entire applications fonts to that single font style. I want the user to be able to have Bold and underlined at the same time so I tried using booleans. 
For example:

static bool bold = false;
static bool underlined = false;
private void label3_Click(object sender, EventArgs e)
{
            bold = true;
            fonts();
        }
        private void label2_Click(object sender, EventArgs e)
        {
            underlined = true;
            fonts();
        }
        void fonts()
        {
            if ((bold) && (underlined))
            {
                textBox1.Font = new Font(textBox1.Font, FontStyle.Bold & FontStyle.Underline);
                underlined = false;
                bold = false;
            }
        }


但是当单击粗体和带下划线的标签时,应该注意两个布尔值都为真,因此将textBox1.Font设置为粗体和带下划线的,对吗?
但是什么也没发生.为什么? :D
记住我对此很陌生,任何基本且易于理解的解释和技巧都将受到赞赏=)


but nothing happens when the bold and underlined label are clicked when it should notice both bool''s are true and therefore set textBox1.Font to bold and underlined, right?

But nothing happens. Why? :D
Remember I''m very new to this and any basic and easy to understand explanations and tips would be so appreciated =)

推荐答案

FontStyle是枚举,因此您可以设置|的多种样式,例如:

FontStyle is enum and therefore you can set multiple styles by |, like this:

textBox1.Font = new Font(textBox1.Font, FontStyle.Bold | FontStyle.Underline);


1.假设您现在正在使用TextBox:对任何TextBox Font属性的所有更改都将为所有内容设置字体样式,大小等.如果这是您想要的,则可能要跳过此评论的其余部分:)

...删除了不正确的内容:控件字体的属性,例如文本框,例如粗体是只读的...

一个.查看您的特定代码:为什么要麻烦将粗体"和带下划线的布尔值"重置为false?

b.您已经在这里有了很好的答案,向您展示了如何使用逻辑或运算符组合样式标志,因此请注意

C.如果您想预先定义FontStyles并重新使用它们:很简单:
1. Assuming that you are now using a TextBox: all changes to any TextBox Font property will set the font-styles, size, etc., for ALL content. If that''s what you want, you may want to skip the rest of this comment :)

... edit : removed incorrect content: properties of a Font of a Control, like a TextBox, like ''Bold are read-only ...

a. looking at your specific code: why are you bothering to reset ''bold and ''underlined booleans to false ?

b. you already have good answers here to show you how to combine style flags using logical-Or, so that''s taken care of

c. if you want to define FontStyles in advance and re-use them: it''s pretty easy:
FontStyle fs1 = new FontStyle();
fs1 = FontStyle.Bold;

textBox1.Font = new Font(textBox1.Font, fs1);

FontStyle fs2 = new FontStyle();
fs2 = FontStyle.Underline;

textBox1.Font = new Font(textBox1.Font, fs1 | fs2);

2.但是,假设您要创建一个文本编辑器,其中一些文本可以加下划线或加粗,而文本的其他部分则可以是纯色或不同的字体大小甚至颜色:

您需要更改为使用RichTextBox.

在CP上,有几个基于扩展RichTextBox的编辑器的出色示例:建议您搜索并研究它们.

最好,比尔

2. But, assuming that you want to make a text-editor where some of the Text can be underlined, or bolded, but other parts of the Text can be plain, or in different font-sizes, or even colors:

You need to change to using a RichTextBox.

Here on CP are several excellent examples of extended RichTextBox based editors: suggest you search for them, and study them.

best, Bill


这篇关于C#文本框使用void更改字体样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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