0

我一直在尝试在多个表单之间共享一个变量,但没有成功。我对 c# 很陌生,所以尽管阅读了一些关于它的东西,但我还是失败了。下面是程序代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.OleDb;    

namespace login
{
    public partial class LoginScreen : Form
    {
        public LoginScreen()
        {
            InitializeComponent();
        }

        // Variables
        int count = 0;
        public static System.Data.OleDb.OleDbConnection con = 
                new System.Data.OleDb.OleDbConnection();//database connection

        string dbProvider;
        string dbSource;
        OleDbDataAdapter da; // create database adapter 'da'

        // CREATE DATASET VARIABLE ds1 TO HOLD THE DATABASE 
        public static DataSet ds1 = new DataSet();

        string accountNo;
        string sql;
        string password;
        int rownum = 0;
        bool valid = false;

        private void btnLogin_Click(object sender, EventArgs e)
        {
            accountNo = txtBoxAccntNo.Text;
            valid = validate();  //uses validate() method to check validity 
            if (valid == true && accountNo == "11111111")
            {
                ManagerScreen Manager = new ManagerScreen();
                this.Hide();
                Manager.Show();
            }
            else if (valid == true)
            {
                s customer = new s();
                this.Hide();
                customer.Show();
            }
            else
            {
                if (count == 2)
                {
                    this.Close();
                }
                count += 1;
                txtBoxAccntNo.Clear();
                txtBoxPinNo.Clear();
            }

        }

        private void txtBoxAccntNo_TextChanged(object sender, EventArgs e)
        {

        }

        private void LoginScreen_Load(object sender, EventArgs e)
        {   
            // open database connection and load contents 

            // database connection
            dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"; // this is the database provider
            dbSource = "Data Source = 'C:\\Bank.accdb'"; // navigation path
            con.ConnectionString = dbProvider + dbSource;         

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // If button exit selected hide this form and open the welcome screen
            WelcomeForm Welcome = new WelcomeForm();
            this.Hide();
            Welcome.Show();
        }

        // IsValid method checks that pass and login are valid 
        private bool validate()
        {
            ds1 = new DataSet();
            con.Open();

            // Validate Account number
            sql = "SELECT * FROM tblCustomers WHERE ((tblCustomers.AccountNo) = '" + txtBoxAccntNo.Text + "')";
            da = new OleDbDataAdapter(sql, con);
            rownum = da.Fill(ds1, "tblCustomers");
            con.Close();


            if (rownum != 1)
            {
                MessageBox.Show("Not a valid Account number! - Try Again ");
                return false;
            }
            else
            {
                // validate the pin
                password = ds1.Tables["tblCustomers"].Rows[0][4].ToString();
                if (password == txtBoxPinNo.Text)
                {
                    MessageBox.Show("valid");
                    return true;
                }
                else
                {
                    MessageBox.Show("Not a valid password - please try again ");
                    return false;
                }
            }
        }           
    }
}

我想与所有其他表单共享变量 accountNo。请告知,因为我真的需要继续这样做。感谢您的任何帮助。

4

3 回答 3

1

The right way to go about this is to use the form to retrieve the information and then store it somewhere else to be accessed as you need it. Don't access it directly in the form from elsewhere - this will require you to keep the login form in scope for the whole application lifecycle. This probably isn't what you want.

In practice, this means creating something like a Global static class that everything has access to:

public static class Globals {
    public static string AccountNumber {
        get;
        set;
    }
}

From in your login form, after validating the login as correct, you would simply do:

Globals.AccountNumber = txtBoxAcctNo.Text;

Then, anywhere else you need the AccountNumber, you can access it as Globals.AccountNumber.

于 2013-03-05T11:37:29.767 回答
1

您可以将该 accountNo 属性设为静态,或者您也可以使用一些 getter 方法来访问它。

如果您将 accountNo 设置为静态,您可以通过调用 ClassName.PropertyName 在您的情况下访问它 LoginScreen.accountNo 将是帐号属性。

简单的代码示例

public partial class LoginScreen : Form
{
    public LoginScreen()
    {
        InitializeComponent();
    }
    public static string accountNo;
}

public class AnotherClass
{
    string accountNo = LoginScreen.accountNo;
}
于 2013-03-05T11:31:45.657 回答
0

我可以推荐三种方法之一来实现你想要的:

  1. accountNo一个public static变量。然后,其他表单可以访问它LoginScreen.accountNo(最好有一个属性来控制可见性)。如果您可能有许多活动实例LoginScreen并且它们都可能更新 accountNo 并且您希望访问此字段的任何表单获取最新值,这是一个很好的方法。
  2. 为整个表单实现一个单例模式,并将accountNo其作为一个public变量。如果您在任何时候都只有一个公司实例处于活动状态,这是一种很好的方法。
  3. 已经accountNostatic另一个班级的成员并且LoginScreen可以通过UtilClass.accountNo. 如果其他表单/类可能想要更新该字段和/或它是一个不应与此表单关联的字段,这是一个很好的方法。
于 2013-03-05T11:39:15.967 回答