1)理解窗口程序代码结构
2)事件处理机制
3)控件使用
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
Form1.Designer.cs
事件源:触发事件组件
事件:消息
事件侦听者(传递者):
事件处理函数:
1)button1事件源:点击button1,触发消息
2)
this.button1.Click += new System.EventHandler(this.button1_Click);
private void button1_Click(object sender, EventArgs e){
…
}
Form1.cs
Form1.Designer.cs
1)Button,TextBox,Label,ComboBox,RadioButton,CheckBox,PictureBox
2)ListBox,ListView,DateTimePicker,TreeView
3)Menu,ContextMenu,ToolBar,StatusBar
容器组件、非容器组件
ComboBox Items项集合
SelectedItem,SelectedIndex 选中项的值、索引
RadioButton,CheckBox:
private void initListView()
{
int width=this.listView1.Width;
/*表头定义*/
this.listView1.Columns.Add(
"学号", width / 4, HorizontalAlignment.Left);
this.listView1.Columns.Add(
"姓名", width / 4, HorizontalAlignment.Left);
this.listView1.Columns.Add(
"学院", width / 2, HorizontalAlignment.Left);
/*行数据*/
for (int i = 1; i < 100; i++)
{
ListViewItem item = new ListViewItem("2010000"+i);
item.SubItems.Add("张军");
item.SubItems.Add("计算机学院");
this.listView1.Items.Add(item);
}
this.listView1.View = View.Details;//设置显示样式
this.listView1.GridLines = true;//设置网格线
this.listView1.FullRowSelect = true;//设置是否全行选择
}
如何显示数据库中表的内容?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ex2013_07_22
{
class SQLHelper
{
private static string connstr = @"Data Source=.\zpsqlserver;Initial Catalog=mydb;User ID=sa;Password=4846";
private static SqlConnection conn = new SqlConnection(connstr);
private static SqlCommand cmd = new SqlCommand("", conn);
/*执行查询语句,返回DataReader*/
public static SqlDataReader ExecuteQuery(string sql)
{
if(conn.State==ConnectionState.Closed)
conn.Open();
cmd.CommandText = sql;
SqlDataReader dr=cmd.ExecuteReader();
return dr;
}
/*执行insert,update,delete语句*/
public static int ExecuteUpdate(string sql)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.CommandText = sql;
int x= cmd.ExecuteNonQuery();
conn.Close();
return x;
}
/*关闭连接对象*/
public static void CloseConnection()
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ex2013_07_22
{
class Customer
{
private string cid, cname, cphone;
public Customer() { }
public Customer(string cid, string cname, string cphone)
{
this.cid = cid; this.cname = cname; this.cphone = cphone;
}
/*属性访问器*/
public string Cid
{
get
{
return this.cid;
}
}
public string Cname
{
get
{
return this.cname;
}
}
public string Cphone
{
get
{
return this.cphone;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Data.SqlClient;
namespace ex2013_07_22
{
class CustomerDao
{
public void addCustomer(Customer cus)
{
string sql = "insert into customers values('{0}','{1}','{2}')";
sql = string.Format(sql, cus.Cid, cus.Cname, cus.Cphone);
SQLHelper.ExecuteUpdate(sql);
}
public void deleteCustomer(string cid)
{
string sql = "delete from customers where customerid='" + cid + "'";
SQLHelper.ExecuteUpdate(sql);
}
public List<Customer> queryAllCustomers()
{
List<Customer> clist = new List<Customer>();
string sql = "select * from customers";
SqlDataReader dr = SQLHelper.ExecuteQuery(sql);
while (dr.Read())
{
Customer cus = new Customer((string)dr[0],(string)dr[1],(string)dr[2]);
clist.Add(cus);
}
SQLHelper.CloseConnection();
return clist;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyApp
{
public partial class ListViewForm : Form
{
private CustomerDao dao = new CustomerDao();
public ListViewForm()
{
InitializeComponent();
initListViewColunms();
}
private void initListViewColunms()
{
int width=this.listView1.Width;
/*表头定义*/
this.listView1.Columns.Add(
"编号", width / 3, HorizontalAlignment.Left);
this.listView1.Columns.Add(
"姓名", width / 3, HorizontalAlignment.Left);
this.listView1.Columns.Add(
"电话", width / 3, HorizontalAlignment.Left);
this.listView1.View = View.Details;//设置显示样式
this.listView1.GridLines = true;//设置网格线
this.listView1.FullRowSelect = true;//设置是否全行选择
}
private void btnQuery_Click(object sender, EventArgs e)
{
List<Customer> clist = dao.queryAllCustomers();
foreach(Customer cus in clist){
ListViewItem item = new ListViewItem(cus.Cid);
item.SubItems.Add(cus.Cname.Trim());
item.SubItems.Add(cus.Cphone);
this.listView1.Items.Add(item);
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
ListViewItem item = this.listView1.SelectedItems[0];
string cid=item.Text;
if (dao.deleteCustomer(cid) > 0)
{
MessageBox.Show("已成功删除一位顾客");
this.listView1.Items.Remove(item);
}
}
}
}
本章练习题下载地址:点此下载