基本控件:
label(標簽控件)
主要屬性:Image(在標簽上顯示的圖像)
Text(在標簽上顯示的文本)
TextBox(文本框控件)
主要屬性:PasswordChar(指示在作為密碼框時,文本框中顯示的字符,而不是實際輸入的文本)
Button(按鈕控件)
ComboBox(下拉框)屬性:SelectedItem:獲取當前選定的項
事件:Click(單擊控件時發生)
private void butStyle_Click(object sender, EventArgs e) { string str = "Data source=.;Initial catalog=Myschool;uid=sa"; SqlConnection con = new SqlConnection(str); string sql = "select count(1) from student where studentName='" + txtUserName.Text + "' and LoginPwd='" + txtPwd.Text + "'"; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); int count = Convert.ToInt32(cmd.ExecuteScalar()); if (count > 0) { MessageBox.Show("登陸成功"); this.Hide(); FormMain frm = new FormMain(); frm.Show(); } } catch (Exception) { MessageBox.Show("退出"); } finally { con.Close(); }
Sender是事件源,表示發生了這個事件的對象,事件發生中,事件源就是按鈕。
e是事件參數(EventArgs)對象,不同的事件會有不同的參數。
Close()方法是窗體類Form的一個方法,作用是關閉窗體。
01.給菜單欄中的“新增學生”菜單項添加事件處理程序,代碼如下
private void 新增學生ToolStripMenuItem_Click(object sender, EventArgs e) { FormStudent formStudent = new FormStudent(); formStudent.Show(); }
02.添加學生信息
public void Save() { //添加學生 string pwd = txtpwd.Text; string stuname = textname.Text; //性別 string stugender = string.Empty; if (radioman.Checked) { stugender = "1"; } else { stugender = "0"; } //下拉框綁定數據 int gid = GeadIdName(); //聯系電話 string StuPhone = textphone.Text; //地址 string StuAddress = textAddress.Text; //日期 DateTime dt = dateBirthday.Value; //郵箱 string StuEmail = textEmail.Text; //LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Email string sql = "insert into Student values('" + pwd + "','" + stuname + "','" + stugender + "'," + gid + ",'" + StuPhone + "','" + StuAddress + "','" + dt + "','" + StuEmail + "')"; string str = "Data source=.;Initial catalog=Myschool;uid=sa;"; SqlConnection con = new SqlConnection(str); SqlCommand cmd = new SqlCommand(sql, con); con.Open(); int count = cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("添加成功"); } con.Close(); }
//查詢學生信息 public void LodaDataListView(string sql) { string str = "data source=.;initial catalog=Myschool;uid=sa;"; SqlConnection con = new SqlConnection(str); SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr != null) { if (dr.HasRows) { while (dr.Read()) { int stuNo = Convert.ToInt32(dr["studentNo"]); //姓名 string stuname = Convert.ToString(dr["studentName"]); //性別 string stuGender = Convert.ToString(dr["Gender"]); //年級名次 string stuGname = Convert.ToString(dr["Gradename"]); ListViewItem LvItem = new ListViewItem(stuNo.ToString()); LvItem.SubItems.Add(stuname); LvItem.SubItems.Add(stuGender); LvItem.SubItems.Add(stuGname); //讓lvItem和ListView關聯 lvlist.Items.Add(LvItem); } dr.Close(); } } } catch (Exception) { throw; } finally { con.Close(); }
//窗體Load的事件中調用
private void Formselect_Load(object sender, EventArgs e) { string sql = "select StudentNO,StudentName,Gender,GradeName from Student,Grade where Student.GradeId=Grade.GradeId"; LodaDataListView(sql); }
修改學生信息
public void upatae() { //添加學生 string pwd = txtpwd.Text; string stuname = textname.Text; //性別 string stugender = string.Empty; if (radioman.Checked) { stugender = "1"; } else { stugender = "0"; } //下拉框綁定數據 int gid = GeadIdName(); //聯系電話 string StuPhone = textphone.Text; //地址 string StuAddress = textAddress.Text; //日期 DateTime dt = dateBirthday.Value; //郵箱 string StuEmail = textEmail.Text; //LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Email string sql = @"update Student set StudentName='" + stuname + "',Gender=" + stugender + ",GradeId='" + gid + "',phone='" + StuPhone + "',Address='" + StuAddress + "',Birthday='" + dt + "',Email='" + StuEmail + "' where studentNo='" + textNo.Text + "'"; string str = "Data source=.;Initial catalog=Myschool;uid=sa;"; SqlConnection con = new SqlConnection(str); SqlCommand cmd = new SqlCommand(sql, con); con.Open(); int count = cmd.ExecuteNonQuery(); if (count > 0) { frmselect.selectData(); MessageBox.Show("修改成功"); } con.Close(); }
//性別if判定:性別男為1 為女0.