VC#NET 创建一个可以对RTF文件进行编辑操作的Windows应用程序.docx
- 文档编号:14053026
- 上传时间:2023-06-20
- 格式:DOCX
- 页数:11
- 大小:430.02KB
VC#NET 创建一个可以对RTF文件进行编辑操作的Windows应用程序.docx
《VC#NET 创建一个可以对RTF文件进行编辑操作的Windows应用程序.docx》由会员分享,可在线阅读,更多相关《VC#NET 创建一个可以对RTF文件进行编辑操作的Windows应用程序.docx(11页珍藏版)》请在冰点文库上搜索。
VC#NET创建一个可以对RTF文件进行编辑操作的Windows应用程序
实验任务及要求
1.简单的用户信息登录界面示例.
2.创建一个可以对RTF文件进行编辑操作的Windows应用程序,要求实现以下功能;读取“D:
\source.rtf”文件中的内容,对其中的特定字符串进行替换,但要保护其中指定的关键内容不被修改,将修改后的内容保存为”D:
\result.rtf”文件,并以列表的形式对窗体功能进行说明.
实验内容(步骤、表格、算法或流程图等)
1.
(1)新建一个名为MaskedTextBoxExample的Windows应用程序项目,在[解决方案资源管理器]中Form1.cs重命名为FormMaskedTextBox.cs.如下各图所示.
(2)调整窗体到适当大小;设置[Text]属性为“员工信息管理示例”,作为窗体标题栏显示的文本.注意:
要先单击“FormMaskedTextBox.cs[设计]”小窗口标题,再设置该窗体的[Text]属性
(3)向Form窗体中添加5个MaskedTextBox控件、1个Button控件和7个Label控件
(4)在FormMaskedTextBox类中直接编写MyMaskInputRejected事件和MyMaskInputReset事件.
(5)在构造函数中添加触发事件需要的代码
(6)添加maskedTextBoxBirthday的TypeValidationCompleted事件代码.
(7)双击buttonOK按钮,添加Click事件代码.
(8)按
2.
(1)新建一个名为RichTextBoxExample的Windows应用程序项目,将Form1.cs重命名为FormRichTextBox.cs.
(2)选中Form窗体,在[属性]中设置[Size]属性为Width=600,Height,或者可以直接使用鼠标拉动窗体右下角调整到适当大小;设置[Text]属性为“RTF文件操作“,作为标题栏的显示文本.
(3)向窗体中添加5个Label控件、一个RichTextBox控件、3个TextBox控件和5个Button控件
源程序及注释(不足可加附页)
//1.第一个
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceWindowsFormsApplication1
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
labelResult.Visible=false;
maskedTextBoxBirthday.ValidatingType=typeof(System.DateTime);
//指定maskedTextBoxPhone的MaskInputRejected事件
maskedTextBoxPhone.MaskInputRejected+=newMaskInputRejectedEventHandler(MyMaskInputRejected);
maskedTextBoxPhone.KeyDown+=newKeyEventHandler(MyMaskInputReset);
maskedTextBoxSalary.MaskInputRejected+=newMaskInputRejectedEventHandler(MyMaskInputRejected);
maskedTextBoxSalary.KeyDown+=newKeyEventHandler(MyMaskInputReset);
}
privatevoidMyMaskInputRejected(objectsender,MaskInputRejectedEventArgse)
{
((MaskedTextBox)sender).ForeColor=Color.Red;
}
//重新输入时,以改写方式输入,并恢复正常的字体颜色
privatevoidMyMaskInputReset(objectsender,KeyEventArgse)
{
((MaskedTextBox)sender).InsertKeyMode=InsertKeyMode.Overwrite;
((MaskedTextBox)sender).ForeColor=Color.FromName("WindowText");
}
privatevoidmaskedTextBoxBirthday_TypeValidationCompleted(objectsender,TypeValidationEventArgse)
{
if(!
e.IsValidInput)
{
MessageBox.Show("您输入的日期不正确!
");
((MaskedTextBox)sender).Focus();
((MaskedTextBox)sender).InsertKeyMode=InsertKeyMode.Overwrite;
}
}
privatevoidbuttonOK_Click(objectsender,EventArgse)
{
//用户名和真实姓名不允许空,进行处理时首先验证二者
if(maskedTextBoxUserCode.Text=="_"||maskedTextBoxUserName.Text=="")
{
MessageBox.Show("您还没有填写用户名或真实姓名!
");
maskedTextBoxUserCode.Focus();
return;
}
//若输入满足要求,则显示输入的信息
labelResult.Visible=true;
labelResult.Text=string.Format("新添加雇员信息如下:
\n"+"员工号:
{0}\n姓名:
{1}\n出生日期:
{2}\n电话号码:
{3}\n月薪:
{4}",maskedTextBoxUserCode.Text,maskedTextBoxUserName.Text,maskedTextBoxBirthday.Text,maskedTextBoxPhone.Text,maskedTextBoxSalary.Text);
}
}
}
//2.第二个
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceRichTextBoxExample
{
publicpartialclassFormRichTextBox:
Form
{
publicFormRichTextBox()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
//清除richTextBox1中的内容
richTextBox1.Clear();
//采用不同的字体及颜色,以列表的方式显示每一项介绍信息
richTextBox1.SelectedText="窗体使用介绍:
"+"\n";
richTextBox1.SelectionBullet=true;
richTextBox1.SelectionFont=newFont("隶书",14,System.Drawing.FontStyle.Bold);
richTextBox1.SelectionColor=Color.OrangeRed;
richTextBox1.SelectedText="读入的RTF文件为:
source.rtf\n";
richTextBox1.SelectionFont=newFont("宋体",14,System.Drawing.FontStyle.Bold);
richTextBox1.SelectionColor=Color.LightSalmon;
richTextBox1.SelectedText="保存RTF文件为:
result.rtf\n";
richTextBox1.SelectionFont=newFont("隶书",14,System.Drawing.FontStyle.Bold);
richTextBox1.SelectionColor=Color.BlueViolet;
richTextBox1.SelectedText="可以使用锁定功能保护文本中的关键字符,以免被错误地修改\n";
richTextBox1.SelectionFont=newFont("隶书",14,System.Drawing.FontStyle.Bold);
richTextBox1.SelectionColor=Color.Purple;
richTextBox1.SelectedText="可以使用替换功能修改文本中的某些内容,或直接修改.\n";
//取消列表形式,以显示其他内容
richTextBox1.SelectionBullet=false;
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
try
{
richTextBox1.LoadFile("D:
\\source.rtf",RichTextBoxStreamType.RichText);
}
catch(Exception)
{
MessageBox.Show("没有找到文件!
");
}
}
privatevoidbutton3_Click(objectsender,EventArgse)
{
try
{
richTextBox1.SaveFile("D:
\\result.rtf",RichTextBoxStreamType.RichText);
MessageBox.Show("当前文本已保存于‘result.rtf’文件中!
");
}
catch(Exception)
{
MessageBox.Show("保存失败!
");
}
}
privatevoidbutton5_Click(objectsender,EventArgse)
{
//若没有指定替换或被替换字符串,则不应执行该事件
if(textBox1.Text==""||textBoxFind2.Text=="")
{
MessageBox.Show("没有完整指定替换或被替换字符串!
");
return;
}
//若没有找到所要替换的指定字符串,则提示并退出
intmyTextPosition=richTextBox1.Find(textBox1.Text);
if(myTextPosition==-1)
{
MessageBox.Show("文本中没有找到字符串<"+textBox1.Text+">!
");
return;
}
//若所要替换的指定字符串是已经被保护的关键信息,则提示不能替换
if(richTextBox1.SelectionProtected)
{
MessageBox.Show("字符串<"+textBox1.Text+">已经被设为只读状态,不能替换!
");
return;
}
//若替换操作的条件合法,则进行替换操作,并给出提示信息
richTextBox1.SelectionColor=Color.Red;
richTextBox1.SelectionFont=newFont("隶书",16,System.Drawing.FontStyle.Bold);
richTextBox1.SelectedText=textBoxFind2.Text;
myTextPosition++;
labelResult.Visible=true;
labelResult.Text=string.Format("目前共有{0}个文字,你要查找的<{1}>在第{2}个字符的位置上,已被替换为<{3}>!
",richTextBox1.Text.Length,textBox1.Text,myTextPosition,textBoxFind2.Text);
}
privatevoidbutton4_Click(objectsender,EventArgse)
{
intmyText2=richTextBox1.Find(textBox1.Text);
if(textBox1.Text=="")
{
MessageBox.Show("没有指定要保护的信息!
");
return;
}
if(myText2==-1)
{
MessageBox.Show("文本中不存在指定的字符串<"+textBox1.Text+">!
");
return;
}
richTextBox1.SelectionProtected=true;
}
}
}
运行结果及分析
1.
2.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VC#NET 创建一个可以对RTF文件进行编辑操作的Windows应用程序 VC NET 创建 一个 可以 RTF 文件 进行 编辑 操作 Windows 应用程序