溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

18.WinForm練習(xí)--記事本應(yīng)用程序

發(fā)布時(shí)間:2020-07-20 11:54:30 來(lái)源:網(wǎng)絡(luò) 閱讀:298 作者:初禾 欄目:編程語(yǔ)言

namespace _18記事本應(yīng)用程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {
        //加載程序的時(shí)候隱藏panel
        panel1.Visible = false;
        //取消文本框自動(dòng)換行
        textBox1.WordWrap = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //點(diǎn)擊按鈕時(shí)也將panel隱藏
        panel1.Visible = false;
    }

    private void 顯示ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //點(diǎn)擊顯示按鈕時(shí)顯示panel
        panel1.Visible = true;
    }

    private void 隱藏ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //點(diǎn)擊隱藏按鈕時(shí)隱藏panel
        panel1.Visible = false;
    }
    //獲得打開(kāi)文件的全路徑
    List<string> list = new List<string>();
    private void 打開(kāi)ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //打開(kāi)對(duì)話框
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "請(qǐng)選擇要打開(kāi)的文件";
        ofd.InitialDirectory = @"C:\Users\Administrator.USER-20180925HC\Desktop\pic";
        ofd.Multiselect = true;
        ofd.Filter = "文本文件|*.txt|所有文件|*.*";
        //顯示打開(kāi)對(duì)話框
        ofd.ShowDialog();

        //將選中打開(kāi)的txt文件展現(xiàn)在textBox中
        //獲得文件的全路徑
        string path = ofd.FileName;
        //添加全路徑到list集合中
        list.Add(path);
        //獲得用戶打開(kāi)的文件名
        string fileName = Path.GetFileName(path);
        //將文件名賦值給listBox
        listBox1.Items.Add(fileName);
        //判斷路徑是否為空
        if (path == "")
        {
            return;
        }
        //如果路徑不為空
        using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
        {
            //讀到字節(jié)數(shù)組當(dāng)中
            byte[] buffer = new byte[1024 * 1024 * 5];
            //調(diào)用讀取的方法
            int r = fsRead.Read(buffer, 0, buffer.Length);
            //開(kāi)始解碼,解碼后賦值給textBox
            textBox1.Text = Encoding.Default.GetString(buffer,0, r);
        }

    }

    private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //保存對(duì)話框
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Title = "請(qǐng)選擇保存路徑";
        sfd.InitialDirectory = @"C:\Users\Administrator.USER-20180925HC\Desktop\pic";
        sfd.Filter = "文本文件|*.txt|所有文件|*.*";
        sfd.ShowDialog();

        //獲得文件要保存的路徑
        string path = sfd.FileName;
        //判斷路徑是否為空
        if (path == "")
        {
            return;
        }
        //不為空則開(kāi)始解碼
        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 自動(dòng)換行ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (自動(dòng)換行ToolStripMenuItem.Text == "自動(dòng)換行")
        {
            textBox1.WordWrap = true;
            自動(dòng)換行ToolStripMenuItem.Text = "取消自動(dòng)換行";
        }
        else if(自動(dòng)換行ToolStripMenuItem.Text=="取消自動(dòng)換行")
        {
            textBox1.WordWrap = false;
            自動(dòng)換行ToolStripMenuItem.Text = "自動(dòng)換行";
        }
    }

    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_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
               //雙擊listBox文件列表文件名,則在textBox顯示文件信息
        //獲得雙擊文件的全路徑
        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);
        }

    }
}

}

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI