}
private void btn_Click(object sender, EventArgs e) {
//创建打开对话框实例
OpenFileDialog odf = new OpenFileDialog(); //设置对话框的标题
odf.Title = \请选择要打开的文件O(n_n)O\; //设置对话框多选
odf.Multiselect = true; //设置对话框的初识目录
odf.InitialDirectory = @\; //设置对话框的文件类型
odf.Filter = \文本文件|*.txt|媒体文件|*.wav|图片文件|*.jpg|所有文件|*.*\;
//显示对话的方法 odf.ShowDialog();
//获得在打开对话框中文件的路径 string path = odf.FileName; if(path==\) {
return; }
using(FileStream fsRead=new
FileStream(path ,FileMode.OpenOrCreate,FileAccess.Read)) {
byte[] buffer = new byte[1024 * 1024 * 5]; int r = fsRead.Read(buffer, 0, buffer.Length);
textBox1.Text = Encoding.Default.GetString(buffer, 0, r); } } } }
//17.简单的文本编辑器,可以保存文本内容 在窗体拖入botton控件和textbox控件 Botton控件
Text:保存
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using System.IO;
namespace 保存对话框 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) {
SaveFileDialog sdf = new SaveFileDialog(); sdf.Title = \请选择要保存的路径\;
sdf.InitialDirectory = @\桌面\\\;
//vs2013版本可以这样写 //VS2008版本的写法
// sdf.Filter = \文本文件(*.txt)|*.txt|所有文件(*.*)|*.*\;
sdf.Filter = \文本文件|*.txt|所有文件|*.*\;
sdf.ShowDialog();
string path = sdf.FileName; if (path == \) {
return; }
using(FileStream fsWrite=new
FileStream(path,FileMode.OpenOrCreate,FileAccess.Write)) {
byte[] buffer = Encoding.Default.GetBytes(textBox1.Text); fsWrite.Write(buffer, 0, buffer.Length); MessageBox.Show(\保存成功\); } } } }
//18.文本字体和颜色编辑器
在窗体拖入2个botton控件和1个textBox控件 Botton控件
Botton1 Botton2
Text:字体 Text:颜色
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms;
namespace 字体和颜色对话框 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) {
FontDialog f = new FontDialog(); f.ShowDialog();
textBox1.Font = f.Font; }
private void button2_Click(object sender, EventArgs e) {
ColorDialog c = new ColorDialog(); c.ShowDialog();
textBox1.ForeColor = c.Color; } } }
//19.文本编辑器
在窗体拖入1 个textbox控件在该控件中放入1个panel控件在该控件中放入1个listBox控件和1个botton控件。在放入一个menuStrip控件并分别修改其属性 enmuStrip控件
文件-打开-保存 格式-自动换行 样式 -字体-颜色 打开记录-显示-隐藏
Botton控件
Text:<<
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using System.IO;
namespace 记事本 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) {
//加载程序时隐藏panel panel1.Visible = false; //取消文本框的自动换行功能 textBox1.WordWrap = false; }
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible = true; }
private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e) {
panel1.Visible = false; }
List
///
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) {
OpenFileDialog op = new OpenFileDialog(); op.Title = \选择打开的文本文件\;
op.InitialDirectory = @\桌面\\\; op.Multiselect = true;
op.Filter = \文本文件(*.txt)|*.txt|所有文件(*.*)|*.*\; op.ShowDialog(); //获得选择文件的路径
string path = op.FileName; list.Add(path);
string fileName = Path.GetFileName(path); listBox1.Items.Add(fileName); if (path == \) {
return; }
using(FileStream fsRead = new
FileStream(path,FileMode.OpenOrCreate,FileAccess.Read)) {
byte[] buffer = new byte[1024 * 1024 * 5]; int r = fsRead.Read(buffer, 0, buffer.Length);
textBox1.Text = Encoding.Default.GetString(buffer, 0,r); } }
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) {
SaveFileDialog sd = new SaveFileDialog();
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库C#winform练习(6)在线全文阅读。
相关推荐: