我新学c#.用的要具是Microsoft Visual Studio 2008,请问好何对选中部分修改字体和颜色?

来源:百度知道 编辑:UC知道 时间:2024/06/20 10:09:47
namespace 练习
{
public partial class MidForm : Form
{
public MidForm()
{
InitializeComponent();
}

private void 新建文档ToolStripMenuItem_Click(object sender, EventArgs e)
{
Document childForm = new Document();
//将子窗体的“父窗体”属性设置为主窗体,要注意这里
childForm.MdiParent = this;
childForm.Show();
}

private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Dispose();
}

private void MenuOpen_Click(object sender, EventArgs e)
{
this.openFileDialog.ShowDialog();
this.lblMessage.Text = this.openFileDialog.FileName;
}

private void MenuSave_Click(object sender, EventArgs e)
{
this.saveFileDialog.ShowDialog();

哦,我知道你什么意思了,你想要编辑文本是吧。
要编辑文本就需要一个容器,在VS里面,TextBox是不行的,需要用到
System.Windows.Forms.RichTextBox

我简单的写了一个你看看:
namespace ColorFont
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnFont_Click(object sender, EventArgs e)
{
string s = richTextBox1.SelectedText;//当前选中的文本
if (s.Length == 0)
{
return;
}
FontDialog FD = new FontDialog();
if (FD.ShowDialog() == DialogResult.OK)
{
Font f= FD.Font;
richTextBox1.SelectionFont = f;
}
}

private void btnColor_Click(object sender, EventArgs e)
{
string s = richTextBox1.SelectedText;
if (s.Length == 0)
{
return;
}
ColorDialog CD = new ColorDialog();
if (CD.ShowDialog() == DialogResult.OK)
{
Color c = CD.Color;
richTextBox1.SelectionColor = c;
}
}
}