赞
踩
什么是winform应用程序?
winform应用程序是一种只能客户端技术,我们可以使用winform应用程序帮助我们获得信息或者传输信息等。(其实就是桌面应用程序)
winform做UI说实话很丑,不过有一名做winformUI的语言叫做XAML。
在winform中,Main函数同样也是程序执行的主入口,和控制台程序是一样的。和控制台不同的一点是,我们在控制台中创建的对象,都存在内存中,并不能实际看见,但是winform中,我们创建的对象都清晰的展现在我们眼前。
例如这个窗体,就是主函数中创建的Form对象。在main函数中创建的窗体对象,我们称之为这个窗体应用程序的主窗体。也就意味着,当你将主窗体关闭后,整个应用程序都关闭了。
那么如果我们想实现一些功能,需要写一些后台代码,来控制UI层。这些后台代码就写在Form1.cs中(我用的版本这个文件双击会快速访问到前台,后台页面应该是被隐藏了,不过一般给控件写功能的时候,都能自动打开。或者右键选择查看代码/设计器,都可以快速切换)
designer.cs是我们在设计窗体的时候,自动生成的代买,.resk这个是资源,资源文件都存在这里。我们是写后台程序的,所以一般操作的都是Form1.cs这个文件。
当我们拖一个控件的时候,实际上就是在new一个这个类的对象。
designer里面有一个Initializecomponent的方法,这个方法就是在初始化我们的窗体对象,也就是给窗体以及我们拖过来的控件对象赋值。
不过控件多了,代码也多了,不好找,可以通过属性栏,给这个控件赋值。(f4或者右键)
属性
Name:在后台要获得前台的控件,需要Name属性
Visible:指示一个控件是否可见
Enabled:指示一个控件是否可用
事件;发生一件事
注册事件:双击控件注册(这样注册都是控件默认选中的内个事件)
触发事件:(看你怎么设置了,点击触发还是怎么触发)
Button控件:一个按钮
Label控件:文本显示控件
TextBox控件:文本输入框控件
ScrollBars属性:拖动条显示
Timer控件:在指定的时间间隔内做一件指定的事情
Interval属性:间隔秒数(毫秒)
用上面几个组件做一个简单的记事本应用
在程序加载的时候,取消文本框的自动换行,以及让两个按钮和文本框隐藏
点击登录,判断是否登陆成功
自动换行功能
保存文本:FileStream
- private void Form1_Load(object sender, EventArgs e)
- {
- txtWords.WordWrap = false;
- btnChange.Visible = false;
- btnSave.Visible = false;
- txtWords.Visible = false;
- }
- /// <summary>
- /// 判断是否登录成功
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnSignIn_Click(object sender, EventArgs e)
- {
- string name = txtName.Text.Trim();
- string pwd = txtPwd.Text;
- if(name=="admin"&&pwd=="88888")
- {
- btnChange.Visible = true;
- btnSave.Visible = true;
- txtWords.Visible = true;
-
- lblName.Visible = false;
- lblPwd.Visible = false;
- btnSignIn.Visible = false;
- btnReset.Visible = false;
- txtName.Visible = false;
- txtPwd.Visible = false;
- }
- else
- {
- MessageBox.Show("登录失败");
- txtName.Clear();
- txtPwd.Clear();
- txtName.Focus();
- }
- }
- /// <summary>
- /// 重置
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnReset_Click(object sender, EventArgs e)
- {
- txtName.Clear();
- txtPwd.Clear();
- txtName.Focus();
- }
- /// <summary>
- /// 自动换行
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnChange_Click(object sender, EventArgs e)
- {
- //当前是否自动换行
- if (btnChange.Text == "自动换行")
- {
- txtWords.WordWrap = true;
- btnChange.Text = "取消自动换行";
- }
- else if(btnChange.Text=="取消自动换行")
- {
- txtWords.WordWrap = false;
- btnChange.Text = "自动换行";
- }
- }
- /// <summary>
- /// 保存
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnSave_Click(object sender, EventArgs e)
- {
- using(FileStream fsWrite=new FileStream(@"E:\Csharp Study Work\WindowsFormsApp1\new.txt",FileMode.OpenOrCreate,FileAccess.Write))
- {
- string str = txtWords.Text.Trim();
- byte[] buffer = System.Text.Encoding.Default.GetBytes(str);
- fsWrite.Write(buffer, 0, buffer.Length);
- }
- MessageBox.Show("保存成功");
- }

CheckBox控件:多选控件
RadioBotton控件:单选控件,默认情况下,所有单选按钮只能从中选一个,不过可以使用groupbox控件进行分组
GroupBox控件:容器里面的一个控件,可以用来分组
Checked属性:指示这个控件是否处于选中状态
老师学生登录
- private void btnSignin_Click(object sender, EventArgs e)
- {
- if (rdoStudent.Checked || rdoTeacher.Checked)
- {
- string name = txtName.Text.Trim();
- string pwd = txtPwd.Text;
- if (rdoStudent.Checked)//选学生
- {
- if (name == "student" && pwd == "student")
- {
- MessageBox.Show("学生登录成功");
- }
- else
- {
- MessageBox.Show("登录失败");
- txtName.Clear();
- txtPwd.Clear();
- txtName.Focus();
- }
- }
- else//选老师
- {
- if (name == "teacher" && pwd == "teacher")
- {
- MessageBox.Show("老师登录成功");
- }
- else
- {
- MessageBox.Show("登录失败");
- txtName.Clear();
- txtPwd.Clear();
- txtName.Focus();
- }
- }
- }
- else
- {
- MessageBox.Show("请选择登录身份");
- }
- }

WebBrowser组件:浏览器组件,显示浏览器页面
URL属性:指定web浏览器控件导航到的url
Uri类:把字符串转换成能被URL访问的格式
PictureBox控件:图片显示控件
Image属性:显示图片的属性
Directory类
这是一个静态类,他可以操作文件夹,目录。
创建文件夹
Directory.CreateDirectory(@"");
删除文件夹
Directory.Delete(@"",true);
剪切文件夹
Directory.Move(@"");
获得指定文件夹下的所有文件的全路径
string[] path = Directory.GetFiles(@"","*.jpg");
获得指定文件夹下的所有文件夹的全路径
string[] path = Directory.GetDirectories(@"");
判断指定文件夹是否存在
if(Directory.Exists(@""))
{
for (int i = 0; i < 100; i++)
{
Directory.CreateDirectory(@"" + i);
}
}
MDI窗体设计
首先确定一个父窗体,将Ismdicontainer设置为true。然后创建子窗体,并且设置他们的父窗体。
- private void 显示子窗体ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Form2 frm2 = new Form2();
- frm2.MdiParent = this;
- frm2.Show();
- Form3 frm3 = new Form3();
- frm3.MdiParent = this;
- frm3.Show();
- Form4 frm4 = new Form4();
- frm4.MdiParent = this;
- frm4.Show();
-
- }
-
- private void 横向排列ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- LayoutMdi(MdiLayout.TileHorizontal);
- }
-
- private void 纵向排列ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- LayoutMdi(MdiLayout.TileVertical);
- }

ComboBox控件:下拉框控件,这个下拉框可以看做是一个集合。
DropDownStyle属性:下拉框的外观功能(能否自己输入数据)
日期选择器
- private void Form1_Load(object sender, EventArgs e)
- {
- //程序加载的时候,将年份添加到下拉框
- //获得当前的年份
- int year = DateTime.Now.Year;
- for (int i = year; i >=1949 ; i--)
- {
- cboYear.Items.Add(i + "年");
- }
- }
- /// <summary>
- /// 年份发生改变,加载月份
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
- {
- cboMonth.Items.Clear();
- cboDays.Items.Clear();
- for (int i = 1; i <= 12; i++)
- {
- cboMonth.Items.Add(i + "月");
- }
- }
- /// <summary>
- /// 月份改变,加载天
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
- {
- cboDays.Items.Clear();
- int day = 0;//存天数
- //获得月份,年份
- string strMonth = cboMonth.SelectedItem.ToString().Split(new char[] {'月'},StringSplitOptions.RemoveEmptyEntries)[0];
- string strYear = cboYear.SelectedItem.ToString().Split(new char[] { '年' }, StringSplitOptions.RemoveEmptyEntries)[0];
- int year = Convert.ToInt32(strYear);
- int month = Convert.ToInt32(strMonth);
- switch (month)
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:day = 31;
- break;
- case 2:
- if((year%400==0)||(year%4==0&&year%100!=0))
- {
- day = 29;
- }
- else
- {
- day = 28;
- }
- break;
- default:day = 30;
- break;
- }
- for (int i = 1 ; i <= day; i++)
- {
- cboDays.Items.Add(i + "日");
- }
- }

ListBox控件:列表控件