6.WinFor練習--簡單記事本程序

namespace _6.簡單記事本程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {
        //一開始將不需要顯示的控件進行隱藏
        txtWord.WordWrap = false;
        btnWrap.Visible = false;
        btnSave.Visible = false;
        txtWord.Visible = false;
    }

    private void btnLoging_Click(object sender, EventArgs e)
    {
        //單擊登錄按鈕時判斷用戶密碼是否正確
        //獲取輸入的用戶及密碼
        string name = txtName.Text.Trim();
        string pwd = txtPwd.Text;
        //判斷用戶密碼是否正確
        if(name=="admin" && pwd == "admin")
        {
            MessageBox.Show("登錄成功");
            //之前隱藏控件要顯示出來
            btnWrap.Visible = true;
            btnSave.Visible = true;
            txtWord.Visible = true;
            //現顯示的控件要進行隱藏
            labName.Visible = false;
            labPwd.Visible = false;
            txtName.Visible = false;
            txtPwd.Visible = false;
            btnLoging.Visible = false;
            btnReset.Visible = false;

        }
        else
        {
            //登錄失敗時彈出一個提示框
            MessageBox.Show("用戶或密碼錯誤,請重新輸入");
            //清除用戶框和密碼框輸入的字符
            txtName.Clear();
            txtPwd.Clear();
            //讓用戶框獲得焦點,光標停留在用戶輸入框
            txtName.Focus();
        }
    }

    private void btnWrap_Click(object sender, EventArgs e)
    {
        //程序剛開始初始化時已將textWord設爲不自動換行
        //判斷btnWrap的文本來決定是否自動換行
        if (btnWrap.Text == "自動換行")
        {
            txtWord.WordWrap = true;
            btnWrap.Text = "取消自動換行";
        }
        else
        {
            txtWord.WordWrap = false;
            btnWrap.Text = "自動換行";
        }
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        //保存,將文件保存到指定路徑
        using (FileStream fsWrite = new FileStream(@"C:\Users\Administrator.USER-20180925HC\Desktop\1.txt", FileMode.OpenOrCreate, FileAccess.Write))
        {
            //先拿到用戶輸入的內容
            string str = txtWord.Text.Trim();
            //轉成字符串數組
            byte[] buffer = System.Text.Encoding.Default.GetBytes(str);
            //調用寫的對象
            fsWrite.Write(buffer,0, buffer.Length);
        }
        MessageBox.Show("保存成功");
    }
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章