将选定值CardNumber在form1上的组合框转换为form2

时间:2012-03-14 15:16:15

标签: c# sql-server visual-studio-2010

我正在尝试通过从sql数据库输入其密码来获取已登录的所选卡号,然后我需要匹配所选的卡号以从数据库中检索其余额并将其显示为富文本框。我已经能够从数据库中获取PIN并将其与用户卡号相匹配,以便他们访问表单2,但我发现很难从form1上的组合框中取出所选的卡号并使用它来检索所选的用户平衡一旦我点击余额按钮并将其显示在form2 richtextbox上。我有以下代码:

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;
    }   

    }
}

2 个答案:

答案 0 :(得分:1)

一种方法是创建一个以PIN为成员的结构,在form1中设置其值并在form2中使用它。

结构:

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; }

答案 1 :(得分:1)

将PIN属性添加到fm2

并做

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

然后是一种方式。

基本技巧是让fm2独立于fm1。您可以重载ShowDialog并将其传递到那里。 有一些常见的结构/类,并从两者中使用它。