在某个瞬间看了眼同学买的程序设计指导书,感觉应该使用学校里普遍使用的C#写个东西了.......
那就是第二个上机实践吧
源代码
说实话,用C#完全没经验,不过还是硬着头皮做了下去
(实际是为了继续装好这次逼......
吐槽一下,我为什么感觉单文档编辑器比多文档编辑器好看多了.........
(好吧,可能是我不会窗口样式的改装...
由于这个课程似乎在每个高校都很普遍,不会C#第一想到的肯定是搜搜相关的知识
发现网上有各种现成的示例.................不过我还是尽量的搞了一个我自己的
(一些代码实在不会,就"借鉴"了网上的一些,不过还是学到一些东西
一直认为学习别人的代码也是一种高效率的方式,可能别人并不认可这个观点吧
主要的功能如下:
窗口设计
老话:既然是GUI软件,第一步当然是要设计UI界面了
对于C#窗体设计,VS2010还是比较方便的,可以直接拖拽各种控件
直接设计出你需要的界面,这里需要三个窗口:主窗口,搜索窗口,子窗口(和主窗口差不多)
对于各种控件属性的改变和调整,就是右侧属性图形化界面里修改
多看几遍右侧栏就可以了,其中子窗口添加控件RichTextBox
主窗口另外添加下面控件
如何再添加窗口
点击项目->添加Windows窗体
实现子窗口右键
在子窗口里面添加ContextMenuStrip控件,并设置RichTextBox
属性ContextMenuStrip为你设置的ContextMenuStrip控件名
主窗口各函数
首先了解下C#中get{}set{}
简单来说就是我们封装的某些类中会有外界不可访问的属性
而这段代码就是打开外界对某些私有属性的访问权限
(具体的解释可以看文末的参考文章
自动换行,字体,颜色,时间日期,全选,页面设置,打印,窗口样式
这几个功能都是对于函数的一两句代码的应用,具体可以参考源代码
文本Type的获取
VB中通用对话框Filterindex属性值为0,1,2,3时分别表示什么意思
...
RichTextBoxStreamType fileType = TrunFileType(openFileDialog.FilterIndex);
...
public RichTextBoxStreamType TrunFileType(int i) //数据转换
{
RichTextBoxStreamType fileType;
switch (i)
{
case 1: fileType = RichTextBoxStreamType.PlainText;
break;
case 2: fileType = RichTextBoxStreamType.RichText;
break;
default: fileType = RichTextBoxStreamType.UnicodePlainText;
break;
}
return fileType;
}
...
保存文件的实现
private void 保存SCtrlSToolStripMenuItem_Click(object sender, EventArgs e)
{
FormChild df = (FormChild)this.ActiveMdiChild; //获取子窗口的活动状态
if (df.GetFilePath() == "")
{
SaveFile(df);
}
}
...
public void SaveFile(FormChild df) //保存文件
{
...
RichTextBoxStreamType fileType = TrunFileType(sfd.FilterIndex);
file.SetFileTypeIndex(sfd.FilterIndex);
file.SetFilePath(saveFileDialog.InitialDirectory);
df.Sourse.SaveFile(sfd.FileName, fileType);
df.SetFilePath(sfd.FileName);
oldFile = fileType;
...
}
新建窗口
private void 新建NCtrlNToolStripMenuItem_Click(object sender, EventArgs e)
{
file = new FormChild();//新建一个子窗体
file.MdiParent = this; //定义此窗体的父窗体,使之成为MDI窗体
++ChildFormCount;
file.Text = "子窗体" + ChildFormCount.ToString(); //命名
file.Show();
}
搜索窗口各函数
查找,替换函数
...
//查找
if (file.Sourse.Text.Contains(str))
{
index = file.Sourse.Find(str, index, RichTextBoxFinds.None);
file.Sourse.Select(index, str.Length);
file.Sourse.Focus(); //焦点获取
}
...
//替换
if (file.Sourse.Text.Contains(strOld))
{
Clipboard.Clear(); //清空剪切板
count = file.Sourse.Find(strOld, index, RichTextBoxFinds.None);
file.Sourse.Select(count, str.Length);
Clipboard.SetText(file.Sourse.SelectedText.Replace(strOld, strNew)); //设置text
file.Sourse.Paste();
}
//借用剪切加入要替换成的字符,然后Paste
...
对于寻找下一个,加入一个count,每找一个符合条件的字符串,计数加一
子窗口各函数
复制,粘贴,撤销等
//复制
public void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox.Copy();
}
//粘贴
this.richTextBox.Paste();
//剪贴
this.richTextBox.Cut();
//撤消
this.richTextBox.Undo();
//删除
this.richTextBox.SelectedText = "";
设置RichTextBox控件数据的输入流和输出流的类型
public void SetFileTypeIndex(int i)
{
_index = i;
}
public void SetFilePath(string value)
{
_filePath = value;
}
public FormChild(RichTextBoxStreamType fileType, string filePath,int i)
{
InitializeComponent();
this.SetFilePath(filePath);
this.richTextBox.LoadFile(filePath, fileType);
this.SetFileTypeIndex(i);
}
设计页面偶然关闭
中间有次遇到偶然间关闭了可视化设计页面
只剩下了.cs文件,如何重新生成呢
只需要点击视图->解决方案资源管理器->双击你需要转换的cs文件就行了
参考:RichTextBox右键显示ContextMenuTrip
深入C#中get与set的详解
(还有部分忘记记录网址了,不过还是要感谢一下他们的经验分享