1、第一个Windows程序 1)Form1.cs:窗体文件
Form1.Designer.cs:窗体设计文件,自动生成
Program.cs:主程序文件,Main()方法没有参数,Application.Run(new Form1());指定程序启动时运行的窗体 窗体文件编辑视图:窗体设计器、代码编辑器
partial关键字:分布类,将同一个类的代码分开放在多个文件中,每个文件都是类的一部分,具有相同命名空间和类名
2)Form:.NET 预定义的窗体类,默认所有窗体的父类
属性:Text、Name、BackColor、BackgroundImage、FormBorderStyle、MaximizeBox、ShowInTaskbar、StartPosition、Text、TopMost、WindowState、Icon、ShowIcon、HelpButton、AcceptButton、CancelButton 方法:Close()、Show()、Hide()、ShowDialog() 事件:Load、Closed、Closing
注:1)禁止改变窗体大小可设置FormBorderStyle为FixedSingle,不显示边框则设置为None,默认为sizable
2)构造函数,也叫构造方法,即方法名与类名相同的方法,完成初始化操作 3)Application类的方法:Application.run();Application.exit(); 4)控件三大基本要素:属性、方法、事件 2、基本控件
1)Label标签:Image、Text
2)TextBox文本框:MaxLength、Multiline、PasswordChar、ReadOnly、Text、BorderStyle 3)ComboBox组合框:Items、DropDownStyle、Text、SelectedIndex、SelectedItem 4)Button按钮:Enable、Text、TextAlign、FlagStyle 注:①放置控件时会使用new关键字创建控件对象
②this.Controls.Add()将控件对象添加到窗体的控件集合中,只能这样才能在窗体上显示出来 ③公共属性:ForeColor 3、事件处理
事件驱动:随时响应用户触发的事件并做出相应的响应,windows应用程序是事件驱动的 sender事件源,e事件参数,this代表当前对象 this.BackColor == Color.Red 方法:Show(); Close()
事件:Load、Closed、Closing、MouseMove
注:事件处理方法名是根据控件名生成的,因此最好在生成事件前为控件指定一个有意义的名字 4、MessageBox消息框
1)使用MessageBox对象的Show()方法,并使用DialogResult获得消息框的返回值
DialogResult result = MessageBox.Show(\确认取消登陆吗?\\操作提示\MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes) { this.Close();} 2)用户输入验证
可调用控件的Focus()方法定位光标,如this.txtUserName.Focus();
注:①string.Empty表示空字符串,即不包含任何字符,而null表示空引用,即不引用任何对象
②MessageBoxButtons.YesNo、MessageBoxButtons.OKCancel ③设置Tab键顺序:视图—Tab键顺序,对应属性为TabIndex 4、窗体间跳转
使用窗体的Show()方法和Hide()方法实现窗体的跳转 1)定义窗体对象
FrmAdminMain frmadmin = new FrmAdminMain(); 2)显示窗体 frmadmin.Show(); this.Hide();
注:①当需要使用其他命名空间的类时需要引用其他命名空间using MySchool.AdminForm;
②可以在DBHelper类中定义数据库连接,便于重用
③数据库连接的状态:Open、Closed、Broken等,如Connection.State == ConnectionState.Closed 补充:
1)控件命名要遵循一定的规范
Label:lbl、TextBox:txt、Button:btn、LinkButton:lnkbtn、ImageButton:imgbtn、ListBox:lst、ListView:lv、DropDownList:ddl、DataGridView:dgv、DataList:dl、ComboBox:cmb、CheckBox:chk、CheckBoxList:chkls、RadioButton:rdo、RadioButtonList:rdolt、Image:img、Panel:pnl、Calender:cld、AdRotator:ar、Table:tbl 2)生成随机数
Random r = new Random(); num = r.Next(1, 100);
3)窗体间的数据传递(ACCP7.0强化)
自定义类User.cs,包含userName、userPwd、loginType字段 创建类User.cs对象,保存用户信息 进行窗体间数据的传递
FrmAdminMain frmAdmin = new FrmAdminMain();
frmAdmin.user.UserName = txtUserName.Text.Trim(); //数据传递 frmAdmin.user.UserPwd = txtPwd.Text.Trim(); frmAdmin.user.LoginType = cmbLoginType.Text.Trim(); frmAdmin.Show();
相关推荐: