老師的小作業,一個登入和主界面

 

LoginForm.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data .SqlClient ;

namespace WindowsApplication2
{
    
/// <summary>
    
/// Form1 的摘要說明。
    
/// </summary>

    
    
public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.Button button2;
        
private System.Windows.Forms.TextBox textBox1;
        
private System.Windows.Forms.TextBox textBox2;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label2;
        
private System.Data.SqlClient.SqlConnection sqlConnection1;
        
private System.Data.SqlClient.SqlCommand sqlCommand1;
        
/// <summary>
        
/// 必需的設計器變量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
//
            
// Windows 窗體設計器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的資源。
        
/// </summary>

        static void Main() 
        
{
            Form1 f
=new Form1();
            f.ShowDialog();
            
if(Form1.ShowForm==true)
            
{
                Application.Run(
new MainForm());
            }

            
        }

        
protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗體設計器生成的代碼
        
public static bool ShowForm=false;
        
/// <summary>
        
/// 應用程序的主入口點。
        
/// </summary>

        [STAThread]
        

        
private void button1_Click(object sender, System.EventArgs e)
        
{
            
if (this.textBox1 .Text.ToString () =="" |this.textBox2 .Text.ToString () =="")
            
{
                MessageBox.Show (
"請把您的帳號和密碼輸入完整!");
            }

            
else
            
{
                
try
                
{
                    sqlCommand1
=new SqlCommand ();
                    sqlCommand1.Connection 
=sqlConnection1;
                    
this.sqlCommand1.CommandText="SELECT PWD FROM usertable WHERE userid='"+this.textBox1 .Text .ToString ().Trim ()+"'";
                    
this.sqlConnection1 .Open ();
                    SqlDataReader
                    reader
= sqlCommand1.ExecuteReader ();
                    reader.Read ();
                    
string textpwd=reader.GetString (0);
                    
if (this.textBox2 .Text .ToString ().Trim ()==textpwd.Trim ())
                    
{
                        ShowForm
=true;
                        
this.Close ();
                    }

                    
else
                    
{
                        MessageBox.Show (
"密碼輸入錯誤!");
                        ShowForm
=false;
                    }

                    
                }

                
catch(System.Exception)
                
{
                    MessageBox.Show (
"沒有該用戶""錯誤",MessageBoxButtons.OK ,MessageBoxIcon.Error );
                }

            }

            
this.sqlConnection1 .Close ();
        }


        
private void Form1_Load(object sender, System.EventArgs e)
        
{
        
        }



    }

}

Mainform .cs

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace 圖書管理
{
    
/// <summary>
    
/// Form1 的摘要說明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.ComponentModel.IContainer components;
        
private System.Windows.Forms.ToolBar toolBar1;
        
private System.Windows.Forms.ToolBarButton tBtnFirst;
        
private System.Windows.Forms.ToolBarButton tBtnPre;
        
private System.Windows.Forms.ToolBarButton tBtnNext;
        
private System.Windows.Forms.ToolBarButton tBtnLast;
        
private System.Windows.Forms.ToolBarButton tBtnNew;
        
private System.Windows.Forms.ToolBarButton tBtnEdit;
        
private System.Windows.Forms.ToolBarButton tBtnDelete;
        
private System.Windows.Forms.ToolBarButton tBtnSubmit;
        
private System.Windows.Forms.ToolBarButton tBtnCancel;
        
private System.Windows.Forms.ToolBarButton tBtnQuit;
        
private System.Windows.Forms.Button btnSearch;
        
private System.Windows.Forms.TextBox txt1;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.DataGrid dataGrid1;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label3;
        
private System.Windows.Forms.Label label4;
        
private System.Windows.Forms.Label label5;
        
private System.Windows.Forms.TextBox txt2;
        
private System.Windows.Forms.TextBox txt3;
        
private System.Windows.Forms.TextBox txt4;
        
private System.Windows.Forms.TextBox txt5;
        
private System.Windows.Forms.GroupBox groupBox3;
        
private System.Windows.Forms.CheckBox checkBox1;
        
private System.Windows.Forms.CheckBox checkBox2;
        
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
        
private System.Data.SqlClient.SqlConnection sqlConnection1;
        
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
        
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
        
private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
        
private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
        
private 圖書管理.DataSet1 dataSet11;
        
private System.Windows.Forms.ImageList imageList1;
        
private CurrencyManager cmOrders;//數據導航
        public string EditOrAdd="";

        
public Form1()
        
{
            
//
            
// Windows 窗體設計器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的資源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗體設計器生成的代碼

        
/// <summary>
        
/// 應用程序的主入口點。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
//--------------創建窗體時讀入全部數據--------------
        private void Form1_Load(object sender, System.EventArgs e)
        
{
            
this.sqlDataAdapter1.SelectCommand.Parameters[0].Value="%%";
            
this.sqlConnection1.Open();
            
this.sqlDataAdapter1.Fill(dataSet11);
            
this.sqlConnection1.Close();
            
//爲數據集添加數據項瀏覽控制
            cmOrders=(CurrencyManager) BindingContext[dataSet11,"讀者類型"];
        }



        
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
        
{
            
if (e.Button.ToolTipText == "首記錄")
            
{
                
this.dataGrid1.UnSelect(cmOrders.Position); //取消原選中的行
                cmOrders.Position = 0;
                
this.dataGrid1.Select(cmOrders.Position); //選中當前行
                this.dataGrid1.CurrentRowIndex = cmOrders.Position; //移動表頭指示圖標
                return;
                
            }

            
if (e.Button.ToolTipText == "上一記錄")
            
{
                
if (cmOrders.Position >= 0)
                
{
                    
this.dataGrid1.UnSelect(cmOrders.Position); 
                    cmOrders.Position
--;
                    
this.dataGrid1.Select(cmOrders.Position);      
                    
this.dataGrid1.CurrentRowIndex = cmOrders.Position; 
                }

                
return;
            }

            
if (e.Button.ToolTipText == "下一記錄")
            
{
                
if (cmOrders.Position <= cmOrders.Count-1)
                
{
                    
this.dataGrid1.UnSelect(cmOrders.Position); 
                    cmOrders.Position
++;
                    
this.dataGrid1.Select(cmOrders.Position);       
                    
this.dataGrid1.CurrentRowIndex = cmOrders.Position; 
                }

                
return;
            }

            
if (e.Button.ToolTipText == "尾記錄")
            
{
                
this.dataGrid1.UnSelect(cmOrders.Position); 
                cmOrders.Position 
= cmOrders.Count-1;
                
this.dataGrid1.Select(cmOrders.Position);       
                
this.dataGrid1.CurrentRowIndex = cmOrders.Position; 
                
return;
            }

            
if(e.Button.ToolTipText=="新增")
            
{
                EditOrAdd
="add";
                SetModifyMode(
true);
                
//設置默認值
                txt3.Text="1";
                txt4.Text
="1";
                txt5.Text
="1";
                }

            
if(e.Button.ToolTipText=="修改")
            
{
                EditOrAdd
="edit";
                
this.txt2.Text=this.dataSet11.Tables[0].Rows[this.dataGrid1.CurrentRowIndex][0].ToString().Trim();
                
this.txt3.Text=this.dataSet11.Tables[0].Rows[this.dataGrid1.CurrentRowIndex][1].ToString().Trim();
                
this.txt4.Text=this.dataSet11.Tables[0].Rows[this.dataGrid1.CurrentRowIndex][2].ToString().Trim();
                
this.txt5.Text=this.dataSet11.Tables[0].Rows[this.dataGrid1.CurrentRowIndex][3].ToString().Trim();
                
if(this.dataSet11.Tables[0].Rows[this.dataGrid1.CurrentRowIndex][4].ToString().Trim()=="True")
                
{
    
                    
this.checkBox1.Checked=true;
                }

                
if(this.dataSet11.Tables[0].Rows[this.dataGrid1.CurrentRowIndex][5].ToString().Trim()=="True")
                
{
                    
this.checkBox2.Checked=true;
                }

                SetModifyMode(
true);
            }

            
if(e.Button.ToolTipText=="刪除")
            
{
                    
                DialogResult result
=MessageBox.Show("確認刪除?","刪除數據",MessageBoxButtons.OKCancel);
                
if(result==DialogResult.OK)
                    
if(cmOrders.Count>0)//立即從數據集中刪除
                    {
                        cmOrders.RemoveAt(cmOrders.Position);                        
                        
this.sqlDataAdapter1.Update(dataSet11);
                    }

                    
else
                        MessageBox.Show(
"表中爲空,已無可刪除數據","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
//                if(MessageBox.Show("刪除的行不可恢復,你確定要刪除嗎?","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.Yes)
//                {
//                    int ReCount=this.dataSet11.Tables["讀者類型"].Rows.Count;
//                    for(int i=0;i<ReCount;i++)
//                    {
//                        if(this.dataGrid1.IsSelected(i))
//                        {
//                            this.dataSet11.Tables["讀者類型"].Rows[i]["類型"]="*";
//                        }
//    
//                    }
//                    DataRow[] findRows=this.dataSet11.Tables["讀者類型"].Select("類型='*'");
//                    for(int i=0;i<findRows.Length;i++)
//                    {
//                        findRows[i].Delete();
//                        this.sqlDataAdapter1.Update(this.dataSet11,"讀者類型");
//                    }
//                    this.dataSet11.Tables["讀者類型"].AcceptChanges();
//                
//                }
            }

            
if(e.Button.ToolTipText=="提交")
            
{
                
if(EditOrAdd=="add")
                
{
                    
if(txt2.Text.Trim()=="")//檢查非空字段
                    {
                        MessageBox.Show(
"類型名稱不能爲空","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                        
return;
                    }
                
                    DataRow newRow
=this.dataSet11.Tables[0].NewRow();
                    newRow[
0]=this.txt2.Text.ToString().Trim();
                    newRow[
1]=this.txt3.Text.ToString().Trim();
                    newRow[
2]=this.txt4.Text.ToString().Trim();
                    newRow[
3]=this.txt5.Text.ToString().Trim();
                    
if(this.checkBox1.Checked)
                    
{
                        newRow[
4]=1;
                    }

                    
if(this.checkBox2.Checked)
                    
{
                        newRow[
5]=1;
                    }

                    
this.dataSet11.Tables[0].Rows.Add(newRow);
                    
//MessageBox.Show(this.dataSet11.Tables[0].Rows.Count.ToString());
                
                    
if(dataSet11.GetChanges()!=null)
                    
{
                        
try
                        
{
                            
this.sqlDataAdapter1.Update(dataSet11);
                            SetModifyMode(
false);
                        }

                        
catch(Exception express)
                        
{
                            MessageBox.Show(express.ToString(),
"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                            dataSet11.RejectChanges();
                        }

                    }

                }

                
else if(EditOrAdd=="edit")
                
{
                    
                }

                EditOrAdd
="";
                
return;
            }


            
if (e.Button.ToolTipText == "取消")
            
{
                
try
                
{
                    cmOrders.CancelCurrentEdit();  
//取消編輯
                    SetModifyMode(false);
                }

                
catch(Exception express)
                
{
                    MessageBox.Show(express.ToString(),
"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }

                
return;
            }


            
if(e.Button.ToolTipText=="退出")
            
{
                
this.Close();
            }

        }


        
private void btnSearch_Click(object sender, System.EventArgs e)
        
{
            
this.sqlDataAdapter1.SelectCommand.Parameters[0].Value="%%";
            
if(txt1.Text.Trim()!="")
            
{
                
this.sqlDataAdapter1.SelectCommand.Parameters[0].Value="%"+txt1.Text.Trim()+"%";
            }

            
//清空數據表,並根據新設置的查詢參數重新填充
            dataSet11.讀者類型.Clear();
            
this.sqlDataAdapter1.Fill(dataSet11);
        }

        
private void SetModifyMode(bool blnEdit)
        
{
            
//設置文本框和下拉列表框屬性
            txt1.ReadOnly=!blnEdit;
            txt2.ReadOnly
=!blnEdit;
            txt3.ReadOnly
=!blnEdit;
            txt4.ReadOnly
=!blnEdit;
            txt5.ReadOnly
=!blnEdit;
            
//設置搜索按鈕屬性
            btnSearch.Enabled=!blnEdit;
        }


        
private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
        
{
        
        }
        
        }

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章