实验三 一个标准的Windows应用程序

【实验目的】

      熟练使用windows基本控件的使用,如菜单、工具栏、状态栏、列表框、组合框、Tabcontrol和Listview等,并掌握其主要的事件、属性。掌握开发Windows应用程序的一般过程。

【实验要求】

1.是一个标准的Windows应用程序,内容自定义。

2.要体现在实验目的中列出的控件的使用。

【实验步骤】(要求自己填写详细的实验步骤,设计思路和关键代码)

【实验体会及存在问题】(要求自己填写,感想、设计时碰到的问题,包括设计思想、调试等)

这次实验报告时写个记事本,还是先看截图吧:

1、首先是在主窗口FormMain.cs 下设置两个变量:2、新建:

  

string currentFileName ="新建shang文档.txt";//这个变量来跟踪当前的文档名字
bool changed =false;//判断文本是否改动
代码
privatevoid 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Text
="";
StreamWriter sw
=new StreamWriter(currentFileName);
sw.Close();
//MessageBox.Show("hello");
}

3、打开

代码
privatevoid 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{

openFileDialog1.InitialDirectory
="c:\\";//默认打开在c盘
openFileDialog1.Filter ="txt files (*.txt)|*.txt|All files (*.*)|*.*";//文件类型
//openFileDialog1.FilterIndex = 2;
//openFileDialog1.RestoreDirectory = true;
DialogResult dr =this.openFileDialog1.ShowDialog();

if (dr == DialogResult.OK)
{
currentFileName
= openFileDialog1.FileName;
openFile(
this.openFileDialog1.FileName);
}
}

这里涉及到一个文件写入函数 openFile(string file) 其定义如下:

代码
privatevoid openFile(string file)
{
try
{
using (StreamReader sr =new StreamReader(file, Encoding.GetEncoding(0)))
{
string text ="";
string line;
while ((line = sr.ReadLine()) !=null)
{
text
+= line +"\n";//给读取的文本加上换行
}
this.richTextBox1.Text = text;//写入
}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}

 

4、好了,到保持了,保持是涉及到一个文件的读取,像刚刚文件写入一样,也用了一个函数writeFile(string file),它保持的位置是默认的

 

代码
privatevoid writeFile(string file)
{
using (StreamWriter sw =new StreamWriter(file))
{
foreach (string s in richTextBox1.Lines)
{
sw.WriteLine(s);
}
}
}

5、另存为,其实另存为的方法跟保持相似,前半段代码跟打开完全一样:

代码
privatevoid 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.InitialDirectory
="c:\\";//默认打开在c盘
saveFileDialog1.Filter ="txt files (*.txt)|*.txt|All files (*.*)|*.*";//文件类型
DialogResult dr =this.saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
currentFileName
= openFileDialog1.FileName;
writeFile(
this.saveFileDialog1.FileName);
}
}

6、这步的页面设置是打印时候才有用到的,它用到了一个pageSetDialog控件,代码很简单,直接调用

privatevoid 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
pageSetupDialog1.ShowDialog();
}

7、打印,也是直接控件的调用,其中也PrintPreviewDialog是打印预览控件

代码
privatevoid 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
//打印预览
try
{
//对printPreviewDialog进行实例化可以解决报错
PrintPreviewDialog printPreviewDialog1 =new PrintPreviewDialog();
printPreviewDialog1.Show();
//this.printDocument1.Print();
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
//报错说无法访问已释放的printPreviewDialog
}

}

8、编辑->撤消 撤消命令不用我们写Undo有聚成的直接用

代码
privatevoid 撤消ToolStripMenuItem_Click(object sender, EventArgs e)
{
// Determine if last operation can be undone in text box.
if (richTextBox1.CanUndo ==true)
{
// Undo the last operation.
richTextBox1.Undo();
// Clear the undo buffer to prevent last action from being redone.
richTextBox1.ClearUndo();
}

}

9、编辑->剪切  跟撤消一样,剪切也是集成的 cut方法直接调用

代码
privatevoid 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
// Ensure that text is currently selected in the text box.
if (richTextBox1.SelectedText !="")
// Cut the selected text in the control and paste it into the Clipboard.
richTextBox1.Cut();

}

10、复制 同上

11、粘贴

代码
privatevoid 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
// Determine if there is any text in the Clipboard to paste into the text box.
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) ==true)//这边只粘贴文本
{
// Determine if any text is selected in the text box.
if (richTextBox1.SelectionLength >0)
{
// Ask user if they want to paste over currently selected text.
// if (MessageBox.Show("Do you want to paste over current selection?", "Cut Example", MessageBoxButtons.YesNo) == DialogResult.No)
// Move selection to the point after the current selection and paste.
richTextBox1.SelectionStart = richTextBox1.SelectionStart + richTextBox1.SelectionLength;//覆盖选中的
}
// Paste current text in Clipboard into text box.
richTextBox1.Paste();
}

}

12、查找 打开另一个窗口 FormSearch 这里有主副窗口的设定 这个很聪明,老师说主副窗口用一个全局变量会比较好,但我试过了,没做出来,等课程设计做好了再来搞这鬼东东.

    private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
    {
            FormSearch fs = new FormSearch(this );
            fs.Owner = this;
            fs.Show();

    }

下面先来看看怎么设定主副窗口把 使之可以传参,就是在FormSearch的构造函数设定

  public FormSearch(FormMain f)//
        {
            this.Owner = f;
            InitializeComponent();
        }

 

查找下一个按钮的代码:

代码
//怎样设置每次点击时,查询会自动跳到下一个要查询的字符串
privatevoid btnNext_Click(object sender, EventArgs e)
{

FormMain fm
=this.Owner as FormMain;

string s1 = tbSeacher.Text;
string s2 = fm.richTextBox1.Text;
int res = s2.IndexOf(s1);
if (!tbSeacher.Text.Trim().Equals(""))
{
while (res !=-1)
{
fm.richTextBox1.Select(res, s1.Length);
fm.richTextBox1.Focus();
//把焦点设置在formmain的文本框中
System.Threading.Thread.Sleep(1000);//间隔1秒
res = s2.IndexOf(s1, res +1);//如果找不到时res值为-1
}
}
MessageBox.Show(
"没有找到!");
}
代码

privatevoid 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fd
=new FontDialog();
DialogResult dr
= fd.ShowDialog();
if (dr==DialogResult.OK)
{
richTextBox1.Font
= fd.Font;
}
}

 14、字体颜色跟字体一样 要用到一个ColorDialog控件

。。。

当然还有其他的,比如说工具栏啦 状态栏啦 还有listview控件...等等 我觉得这些没意思 就不想写了。。好了,先这样,要去做网页了。。。。

13、接下来看看字体怎么设置吧 它用到了一个FontDialog控件

原文地址:https://www.cnblogs.com/huaizuo/p/1898128.html