首页 > 解决方案 > 切换到不同的表单(C#、Windows 表单)时,列表的元素不会更新

问题描述

满足所有要求并在注册表单中注册的客户无法通过在登录表中输入相同的信息进入系统。我想在表单之间切换时必须经历不同的过程,但我不知道该怎么做。

在注册表格中:

private void button1_Click(object sender, EventArgs e)
    {
        List<Customer> customers = customerManager.getAll();
        if (CheckIfExist(IDtext.Text))     
        {
            label4.Text = "this username is already exist";
            label4.BackColor = Color.IndianRed;
        }
        if (!checkFieldsFilled())
        {
            label4.Text = "please fill the fields above and try again";
            label4.ForeColor = Color.White;
            label4.BackColor = Color.Gold;
        }
        if (!checkLongEnough(IDtext.Text,PasswordText.Text))
        {
            label4.Text = "username and password must have at least 6 characters!";
            label4.ForeColor = Color.White;
            label4.BackColor = Color.Gold;
        }
        if (!checkIfPasswordsSame(PasswordText.Text, ConfirmPasswordText.Text))
        {
            label4.Text = "passwords are not match!";
            label4.BackColor = Color.IndianRed;
        }
        if (!CheckIfExist(IDtext.Text) && checkFieldsFilled() &&checkLongEnough(IDtext.Text,PasswordText.Text) &&
            checkIfPasswordsSame(PasswordText.Text, ConfirmPasswordText.Text))
        {
            label4.Text = "Signed up successfully!";
            label4.ForeColor = Color.Black;
            label4.BackColor = Color.LightGreen;
            customerManager.AddCustomer(IDtext.Text, PasswordText.Text);
        }
        
    }

在登录表单中:

List<Admin> admins = adminManager.getAll();
        List<Customer> customers = customerManager.getAll();
        
        if (checkBox1.Checked)
        {
            //adminsin
            for (int i = 0; i < admins.Count; i++)
            {
                if(IdText.Text == admins[i].ID && passwordText.Text ==admins[i].Password)
                {
                    denemeTahtası.Text = "başarılı!";
                    break;
                }
                if (IdText.Text!= admins[i].ID || passwordText.Text != admins[i].Password)
                {
                    denemeTahtası.Text = "Please check your username or password and try again";
                    
                }
            }
        }
        else
        {
            for (int i = 0; i < customers.Count; i++)
            {
                if (IdText.Text == customers[i].ID && passwordText.Text ==customers[i].Password)
                {
                    denemeTahtası.Text = "success!";
                    break;
                }
                if (IdText.Text != customers[i].ID || passwordText.Text != customers[i].Password)
                {
                    denemeTahtası.Text = "Please check your username or password and try again";
                }

                if(IdText.Text == "" || passwordText.Text == "")
                {
                    denemeTahtası.Text = "You must fill the username and password fields";
                }
                
            }
        }

我们这里没有管理员的操作,你最好看看其他的内容。

稍后我将需要一个解决方案,因为当管理员在他们使用的面板中更改产品的属性时,这些产品也需要在客户的产品选择屏幕上进行更新。即,主要问题是两种形式同时变化对彼此的影响。

标签: c#formswinforms

解决方案


每个“windows 窗体”都有一个名为“Program.cs”的文件,在调用窗体时会引用该文件。该文件具有以下代码:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

要将值从一个表单发送到另一个表单,您需要将目标表单的“Program.cs”文件的代码更改为以下代码:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        if (args != null && args.Length > 0)
        {
            Application.Run(new Form1(args[]));
        }
        else
        {
            Application.Run(new Form1(""));
        }
    }
}

并在目标表单中将构造函数方法更改为以下:

public Form1(string[] args)
    {
        InitializeComponent();
        if (args != null)
        {
            // Here recieve argumans from other forms
        }
    }

并且在调用目标表单(例如 Form1)时:

Form1 obj = new Form1(arguman1, arguman2, ...)
obj.Show();

推荐阅读