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