1. 程式人生 > >做完四個小專案的收穫

做完四個小專案的收穫

一、員工資訊管理系統

    1)繫結下拉列表的知識

1         private void Form1_Load(object sender, EventArgs e)
2         {
3             string sql = "select * from DeptInfo";
4             this.comboBox1.DataSource = s.CHA(sql);
5             this.comboBox1.DisplayMember = "Deptname";
6             this.comboBox1.ValueMember = "DeptID
"; 7 int ID = int.Parse(this.comboBox1.SelectedValue.ToString()); 8 }

    2)驗證輸入框中輸入的數字

1                 foreach (char chr in this.txtAeg.Text)
2                 { 
3                     if(char.IsDigit(chr))
4                     {
5                     }
6                     else
{MessageBox.Show("年齡只能為整數型別");return;}; 7 }

  二、圖書管理系統

    1)初始化時間,統計表中的行數

1         //初始化時的資料
2         private void groupBox1_Enter(object sender, EventArgs e)
3         {
4             string sql = "select BookID,BookName,BookAuthor,BookConcert,BookDates,BookCount,TypeName from TblBookInfo 
                  inner join TblBookType on TblBookInfo.BookTypeID=TblBookType.ID
"; 5 dataGridView1.DataSource = S.CHA(sql); 6 this.label3.Text = "共有 " + (dataGridView1.RowCount-1).ToString() + " 條資料"; 7 this.label2.Text = "當前時間為:" + DateTime.Now.ToString(); 8 timer1.Start(); 9 }

    2)精確查詢和模糊查詢,發揮到極致(用拼接)

 1        //選擇精確查詢或模糊查詢
 2             string tiaojian = this.textBox1.Text;//預設精確
 3             if (radioButton2.Checked)
 4             {
 5                 tiaojian = "%" + this.textBox1.Text + "%";
 6             }
 7             //查詢的欄位:編號,書名,作者,型別
 8             string ziduan = "";
 9             switch (comboBox1.SelectedIndex)
10             {
11                 case 0:
12                     ziduan = "BookID like'" + tiaojian +"'";
13                     break;
14                 case 1:
15                     ziduan = "BookName like'" + tiaojian + "'";
16                     break;
17                 case 2:
18                     ziduan = "BookAuthor like'" + tiaojian + "'";
19                     break;
20                 case 3:
21                     ziduan = "TypeName like'" + tiaojian + "'";
22                     break;
23             }
24             string sql = "select BookID,BookName,BookAuthor,BookConcert,BookDates,BookCount,TypeName from TblBookInfo 
                  inner join TblBookType on TblBookInfo.BookTypeID=TblBookType.ID where
"+ziduan; 25 dataGridView1.DataSource = S.CHA(sql); 26 this.label3.Text = "共有 " + (dataGridView1.RowCount - 1).ToString() + " 條資料"; 27 }

    三、簡易的銷售管理系統

      1)時間的模糊查詢

1        if (radioButton3.Checked)
2             {
3                 //獲得,年,月,日
4                 string r1 = dateTimePicker3.Value.ToShortDateString();
5                 string r2 = dateTimePicker2.Value.ToShortDateString();
6                 //日期的模糊查詢
7                 sqlc = string.Format("select * from biao2 where riqi>='{0}' and riqi<='{1}'", r1,r2);
8             }

      2)控制元件是否能使用

1         //載入時
2         private void Form3_Load(object sender, EventArgs e)
3         {
4             this.textBox3.ReadOnly = true;
5             this.dateTimePicker3.Enabled = false;
6             this.dateTimePicker2.Enabled = false;
7         }

    四、學員資訊管理系統

      1)執行程式後過一段時間開啟另一個窗體

 1         //載入時開啟計時器
 2         private void Form1_Load(object sender, EventArgs e)
 3         {
 4             timer1.Start();
 5         }
 6         //調計時器的Interval屬性,每兩秒執行一次
 7         private void timer1_Tick(object sender, EventArgs e)
 8         {
 9             frmxx f = new frmxx();   
10             f.Show();       //開啟另一個窗體
11             timer1.Stop();  //然後關掉計時器
12         }

      2)第一條,上一條,下一條,最後一條

 1         //上一條,下一條
 2         public void xiayi(int i)
 3         {
 4             this.comboBox1.SelectedIndex = i;  //返回或設定哪個選項被選取,組合框現行選中項,索引 
 5             //根據索引,和列取得表中的值
 6             this.textBox2.Text = this.dataGridView1.Rows[i].Cells[1].Value.ToString();
 7             this.textBox3.Text = this.dataGridView1.Rows[i].Cells[2].Value.ToString();
 8             this.textBox4.Text = this.dataGridView1.Rows[i].Cells[6].Value.ToString();
 9             this.textBox5.Text = this.dataGridView1.Rows[i].Cells[7].Value.ToString();
10             this.textBox6.Text = this.dataGridView1.Rows[i].Cells[3].Value.ToString();
11             this.textBox7.Text = this.dataGridView1.Rows[i].Cells[5].Value.ToString();
12             this.textBox8.Text = this.dataGridView1.Rows[i].Cells[4].Value.ToString();
13         }
14         //第一條
15         private void button2_Click(object sender, EventArgs e)
16         {
17             xiayi(0);//索引為0
18         }
19         //最後一條
20         private void button5_Click(object sender, EventArgs e)
21         {
22             //先獲得表中的行數,減去一個無資料行,索引從0開始的所以再減一
23             int z = dataGridView1.Rows.Count-2;
24             xiayi(z);
25         }
26         //上一條
27         private void button3_Click(object sender, EventArgs e)
28         {
29             //上一條,也就是索引減一
30             int i = this.comboBox1.SelectedIndex-1;
31             //當索引減到小於0時,將索引賦值為最大
32             if (i<0)
33             {
34                 i = dataGridView1.Rows.Count-2;
35             }
36             xiayi(i);
37         }
38         //下一條
39         private void button4_Click(object sender, EventArgs e)
40         {
41             int i = this.comboBox1.SelectedIndex + 1;
42             //當索引增加到最大值時,將索引賦值為0
43             if (i > dataGridView1.Rows.Count - 2)
44             {
45                 i = 0;
46             }
47             xiayi(i);
48         }