2012-03-14 45 views
0

我想通過從sql數據庫中輸入他們的pin號碼來登錄的選定的卡號,然後我需要匹配選定的卡號來檢索它們從數據庫中取得平衡並將其顯示在一個富文本框中。我已經能夠從數據庫中獲取PIN碼並將其與用戶cardnumber匹配,以使他們能夠訪問表格2,但是我發現很難從form1上的組合框中選取所選卡號並使用它來檢索所選內容用戶平衡一次我點擊平衡按鈕,並將其顯示在form2 richtextbox上。我有以下代碼:將選定的值CardNumber組合框在form1上形成2

public partial class Form1 : Form 
{ 

    public int logAttempts = 3; 
    public string accountNo;  

    public Form1() 
    { 
     InitializeComponent();    

    } 

    private void btnCancel_Click(object sender, EventArgs e) 
    { 
     Application.Exit(); 
    } 

    private void BtnEnter_Click(object sender, EventArgs e) 
    { 
     int index = cBCardNumber.SelectedIndex; 

     DataRow dpin = dataSet1ATMCards.Tables["ATMCards"].Rows[index]; 
     String pin = dpin.ItemArray.GetValue(1).ToString();   

     if (textBoxPin.Text.Length == 4) 
     { 
      if (String.Equals(pin, textBoxPin.Text)) 
      { 
       Form2 frm2; 
       frm2 = new Form2(); 
       frm2.ShowDialog(); 
      } 
      else 
      { 
       if (loginAttempts == 1) 
       { 

        sqlCommandConfiscated.Parameters["@cardNumber"].Value = cBCardNumber.Text; 
        sqlCommandConfiscated.Parameters["@confiscated"].Value = true; 

        try 
        { 
         sqlCommandConfiscated.Connection.Open(); 
         sqlCommandConfiscated.ExecuteNonQuery(); 
         MessageBox.Show("You have reached your maximum login attempts."); 
         textBoxPin.Clear(); 

        } 
        catch (Exception ex) 
        { 
         MessageBox.Show(ex.Message); 
        } 
        finally 
        { 
         sqlCommandConfiscated.Connection.Close(); 
        } 
       } 
       else 
       { 
        loginAttempts--; 
        MessageBox.Show("You have entered the Wrong Pin. You have " + loginAttempts); 
       } 
      } 
     } 
     else 
     { 
      MessageBox.Show("Please enter your 4 digit Pin Number"); 
     } 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    {   
     sqlDataAdapter1.Fill(dataSet1ATMCards.ATMCards);  

    } 

    private void cBCardNumber_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     loginAttempts = 3; 
    } 

    } 
} 

回答

1

添加PIN屬性FM2

,做

Form2 frm2;     
frm2 = new Form2(); 
frm2.PIN = pin;    
frm2.ShowDialog(); 

,然後是單向的。

基本技巧是讓fm2獨立於fm1。你可以重載ShowDialog,並在那裏傳遞它。 擁有某種常見的結構/類並從兩者中使用它。

1

一種方法是創建一個PIN結構,它的成員,在Form1將其值設置在窗口2使用它。

結構:

public struct Person 
{ 
    public string PIN { get; set; } 
} 

Form1中:

 Person p = new Person(); 
     p.PIN = dpin.ItemArray.GetValue(1).ToString(); 
     Form2 frmPopup = new Form2(); 
     frmPopup.p = p; 
     frmPopup.ShowDialog(); 

窗體2:

 public Person p { get; set; }