C#实现利用单选框实现更改文本的richTextBox字体、大小、加粗。通过选择字体、大小和是否加粗决定,我们在文本框中字体格式是什么。
单选按钮:RadioButton,常用事件是CheckedChanged和Click,当选择状态改变(即单选按钮checked属性值改变)后,触发CheckedChanged事件;当单机单选按钮时,触发Click事件
复选框:CheckBox,包含CheckedChanged和Click事件,但使用最多的是CheckStateChanged事件。当复选框的Checked属性值改变后,触发CheckedChanged事件;当单击复选框时,触发Click事件;当复选框的CheckState属性值改变后,触CheckStateChanged发事件。
二者通常放在CheckBox这一控件当中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 单选界面
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (jiazu.Checked)
richTextBox1.Font = new Font(richTextBox1.Font, richTextBox1.Font.Style | FontStyle.Bold);
else
richTextBox1.Font = new Font(richTextBox1.Font, richTextBox1.Font.Style ^ FontStyle.Bold);
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
richTextBox1.Font = new Font("宋体", richTextBox1.Font.Size, richTextBox1.Font.Style);
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
richTextBox1.Font = new Font(richTextBox1.Font.FontFamily, 10, richTextBox1.Font.Style);
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
richTextBox1.Font = new Font(richTextBox1.Font.FontFamily, 16, richTextBox1.Font.Style);
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
richTextBox1.Font = new Font("楷体", richTextBox1.Font.Size, richTextBox1.Font.Style);
}
}
}