77范文网 - 专业文章范例文档资料分享平台

C#winform练习(6)

来源:网络收集 时间:2018-12-20 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

}

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 list = new 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)在线全文阅读。

C#winform练习(6).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/378537.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: