sd.Title = \选择要保存的文件路径\;
sd.InitialDirectory = @\桌面\\\; sd.Filter = \文本文件(*.txt)|*.txt|所有文件(*.*)|*.*\; sd.ShowDialog();
string path = sd.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(\保存成功\); }
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e) {
if (自动换行ToolStripMenuItem.Text == \自动换行\) {
textBox1.WordWrap = true;
自动换行ToolStripMenuItem.Text =\取消自动换行\; }
else if (自动换行ToolStripMenuItem.Text==\取消自动换行\) {
textBox1.WordWrap = false;
自动换行ToolStripMenuItem.Text = \自动换行\; } }
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) {
FontDialog fd = new FontDialog(); fd.ShowDialog();
textBox1.Font = fd.Font; }
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e) {
ColorDialog cd = new ColorDialog(); cd.ShowDialog();
textBox1.ForeColor = cd.Color; }
private void listBox1_DoubleClick(object sender, EventArgs e) {
//要获得双击的文件所对应的全路径
string path = list[listBox1.SelectedIndex]; 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 button1_Click(object sender, EventArgs e) {
panel1.Visible = false; } } }
//20.打开正在运行的进程,找开指定的应用程序和指定的文件
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics;
namespace 进程 {
class Program {
static void Main(string[] args) {
//获得当前运行的进程
//Process[] pros = Process.GetProcesses(); //foreach (var item in pros) //{
// Console.WriteLine(item); //}
//Console.ReadKey(); //打开一些应用程序
//Process.Start(\
//Process.Start(\ //Process.Start(\
//Process.Start(\ //Console.ReadKey();
//第一步:通过一个进程打开指定的文件
ProcessStartInfo psi = new ProcessStartInfo(@\); //第二步:创建进程对象
Process pros = new Process();
//pros.StartInfo = new ProcessStartInfo(@\ pros.StartInfo = psi; pros.Start(); Console.ReadKey(); } } }
//多线程实例
在窗体中拖入一个botton和一个textBox
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.Threading;
namespace 线程 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
Thread th;
private void button1_Click(object sender, EventArgs e) {
//创建一个线程去执行这个方法 th = new Thread(Test);
//将线程设置为后台线程 th.IsBackground = true;
//标记这个线程准备就绪了,可以随时被执行 th.Start(); }
private void Test() {
for (int i = 0; i < 10000; i++) {
//将运算结果赋值给textbox textBox1.Text = i.ToString(); } }
private void Form1_Load(object sender, EventArgs e) {
//取消跨线程的访问
Control.CheckForIllegalCrossThreadCalls = false; }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
//当你点击窗体的时候,判断新线程是否为null if (th != null) {
th.Abort();//结束这个线程 } } } }
//21.实现一个简单的音乐播放器
在窗体拖入3个botton控件和1个listbox控件并分别修改其属性 botton控件
botton1 botton2 botton3
Text:打开 Text:上一曲 Text:下一曲
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Media; using System.Text;
using System.Threading.Tasks; using System.Windows.Forms;
namespace 音乐播放器 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
List
private void button1_Click(object sender, EventArgs e) {
OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = \请选择音乐文件\;
ofd.InitialDirectory = @\; ofd.Multiselect = true;
ofd.Filter = \音乐文件|*.wav|所有文件|*.*\; ofd.ShowDialog();
//获得我们在文件夹中选择所有文件的全路径 string[] path = ofd.FileNames; for(int i=0;i //将音乐文件的文件名加载到listBox中 listBox1.Items.Add(Path.GetFileName(path[i])); //将音乐文件的全路径加载到集合中来 list.Add(path[i]); } } private void listBox1_DoubleClick(object sender, EventArgs e) { sp.SoundLocation=list[listBox1.SelectedIndex]; 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库C#winform练习(7)在线全文阅读。
相关推荐: