温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

6.WinFor练习--简单记事本程序

发布时间:2020-06-18 15:36:00 来源:网络 阅读:216 作者:初禾 栏目:编程语言

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("保存成功");
    }
}

}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI