c# - 切换到不同的表单(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";
}
}
}
我们这里没有管理员的操作,你最好看看其他的内容。
稍后我将需要一个解决方案,因为当管理员在他们使用的面板中更改产品的属性时,这些产品也需要在客户的产品选择屏幕上进行更新。即,主要问题是两种形式同时变化对彼此的影响。
解决方案
每个“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();
推荐阅读
- html - 使用 reactjs 更改主 index.html 上的语言
- php - 带有种子的 php 随机 int 生成器
- python - Python 代码返回无法识别的错误
- google-sheets - 如何将查询中的不同列堆叠到一列 Google 表格中
- database - 如何从 Typeorm 中的连接实体中仅选择单个/多个字段
- postman - 如何在不是查询参数的邮递员端点 URL 中使用数据变量?
- python - 使用python进行高斯拟合
- amazon-web-services - AWS CDK、ecs-patterns、ApplicationLoadBalancedFargateService、设置标签
- oop - 为什么 Dart 更喜欢省略局部变量的类型注释?
- c# - 如何格式化 DataGrid 中的单元格以在之后显示 % 符号