找不到如何将变量从一个窗体传递到另一个窗体 C#



我似乎找不到任何资源来专门帮助我解决问题。我希望能够将从表中检索到的变量传递到另一个窗体,以便我可以知道谁已登录。

我希望能够将名为 signedin(string) 的变量或变量 x(int) 传递给名为 AddStudentAccForm 的表单.cs这取决于传递整数还是字符串更容易。

我将非常感谢任何人可以提供的任何帮助!谢谢!

这是我的代码,我在表单中创建名为 StartMenu.cs 的变量:

private void TeacherLoginButton_Click(object sender, EventArgs e)
    {

        string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
        SqlConnection connect = new SqlConnection(connectionString);
        connect.Open();

        SqlCommand command10 = new SqlCommand(@"SELECT [Username], [Password] FROM TeacherDetails WHERE ([Username]=@username  AND [Password]=@password);", connect);

        command10.Parameters.AddWithValue("@username", usernameTlogin.Text);
        command10.Parameters.AddWithValue("@password", passwordTlogin.Text);
        SqlDataReader reader;
        reader = command10.ExecuteReader();
        int count = 0;
            while (reader.Read())
            {
                count = count + 1;

            }
            if( count == 1)
            {
                MessageBox.Show("Usename and password is correct");
                this.Hide();
                TeacherDashboardForm TeacherDashboard = new TeacherDashboardForm();
                TeacherDashboard.Show();
            }
            else if (count > 1)
            {
                MessageBox.Show("BEEP BOOP ERROR");
            }
            else
            {
                MessageBox.Show("Username or password is incorrect");
            }
            connect.Close();
            connect.Open();
            SqlCommand command11 = new SqlCommand(@"SELECT [TeacherID] FROM TeacherDetails WHERE ( [Username]=@username AND [Password]= @password)", connect);
            command11.Parameters.AddWithValue("@username", usernameTlogin.Text);
            command11.Parameters.AddWithValue("@password", passwordTlogin.Text);
            reader = command11.ExecuteReader();
            int x = 0;
                while (reader.Read())
                {
                    x = reader.GetInt32(0);
                }
                String signedin;
                signedin = x.ToString();
                MessageBox.Show(signedin);
    }

您需要在代表软件体系结构的类/模型中组织代码。

就像你必须创建类一样:

    public static class Sql
        {
     public static AuthenticationModel Login(string userName, string password)
            {
                DataTable dt = GetResponseTable("Select UserID,Password from User where userName=" + userName);
                AuthenticationModel details = new AuthenticationModel{LoginStatus = "Failed"};
                if(dt..Rows.Count > 0 )
                  details = new AuthenticationModel{ LoginStatus = "Success", UserName = userName, UserId = dt["UserID"].toString();
                return details;
            }
private static DataTable GetResponseTable(string StoredProcedureName)
        {
            SqlConnection Con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            DataTable dt = new DataTable();
            Con.Open();
            SqlCommand cmd = new SqlCommand(StoredProcedureName, Con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            Con.Close();
            return dt;
        }
    }

并在整个应用程序中使用此上述模型来检查有效用户。这不是唯一的方法,但您可以有更多方法来组织软件模型。

AddStudentAccForm.cs开始菜单.cs

在"AddStudentAccForm.cs"中为"StartMenu.cs"创建一个对象,一旦您获得状态"登录"为

StartMenu objStart = new StartMenu(signedIn);

在 StartMenu 中.cs您可以在参数化构造函数中检索此值,然后可以相应地使用它

Class StartMenu
{
    StartMenu(string signedIn)
    {
        //use your value 
    }
}
例如,

如果要将名称的值从 Form1 传递到 Form2,只需在 Form2 上添加一个静态字段声明以保存 Name 值,从 Form1 可以访问这些静态字段,就像这样 Form2.Name

public partial class Form2 : Form
{
    public static string Name;
    public static int value;
    public Form2()
    {
        InitializeComponent();
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = Name+ value.ToString();
    }
}

在 Form1 中,如果我只想传递值,请这样做。

就我而言,我将传递按钮事件单击

    private void button1_Click(object sender, EventArgs e)
    {
        Form2.value = 10;
        Form2.Name = "Felicio";
        Form2 form = new Form2();
        form.Show();
    }

相关内容

  • 没有找到相关文章