我需要保存数据以列出我所做的事情
List<string> users = new List<string>();
然后在按钮上单击users.Add(name);
。我想在此处将名称写入txtBox:
foreach (var user in users)
{
txtBoxUsers.Text += Environment.NewLine + user;
}
但是当我&#34;注册&#34;更多用户输出如下:
1
1
2
2
3
3
当我第一次注册用户时,它只显示一次该名称,然后,它将它乘以我不想要的。我试图不使用foreach,我使用了3种方法,但没有人工作。代码:
public partial class Form1 : Form
{
private string pass;
private string name;
private bool when = false;
private List<string> users = new List<string>();
public void regBtn_Click(object sender, EventArgs e)
{
if (regTxtBoxName.TextLength < 4)
{
when = true;
if (regTxtBoxPass.TextLength < 4)
{
if (when == false)
{
progBar1.Value = 0;
MessageBox.Show("Choose password/name with minimal length 5", "Registration ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
regTxtBoxPass.BackColor = Color.Red;
}
else
{
if (when == false)
{
progBar1.Value = 0;
MessageBox.Show("Choose password/name with minimal length 5", "Registration ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
regTxtBoxPass.BackColor = Color.White;
}
regTxtBoxName.BackColor = Color.Red;
progBar1.Value = 0;
MessageBox.Show("Choose password/name with minimal length 5", "Registration ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
regTxtBoxName.BackColor = Color.White;
if (regTxtBoxPass.TextLength < 4)
{
regTxtBoxPass.BackColor = Color.Red;
progBar1.Value = 0;
MessageBox.Show("Choose password/name with minimal length 5", "Registration ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
regAge.Minimum = 0;
regAge.Maximum = 150;
if (regAge.Value >= 15)
{
if(chkBox1.Checked){
regTxtBoxName.BackColor = Color.White;
regTxtBoxPass.BackColor = Color.White;
regAge.BackColor = Color.White;
pass = regTxtBoxPass.Text;
name = regTxtBoxName.Text;
users.Add(name);
foreach (var user in users) {
txtBoxUsers.Text += Environment.NewLine + user;
}
regTxtBoxPass.Text = "";
regTxtBoxName.Text = "";
regAge.Value = 0;
progBar1.Value = 100;
MessageBox.Show("Your account has been succesfully created.", "Registration FINSIHED", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else{MessageBox.Show("Please, accept condition terms.", "Accept it omg....",MessageBoxButtons.OK, MessageBoxIcon.Warning);
progBar1.Value = 0;
};
}
else
{
regAge.BackColor = Color.Red;
regAge.Value = 0;
progBar1.Value = 0;
MessageBox.Show("You're too young for this :)", "Registration ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
public void logBtn_Click(object sender, EventArgs e)
{
if (logTxtBoxName.Text == name)
{
if (logTxtBoxPass.Text == pass)
{
progBar1.Value = 100;
MessageBox.Show("You have beeon successfully logged in.", "Logged IN", MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
progBar1.Value = 0;
MessageBox.Show("Your username or password is wrong!", "Login failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else {
progBar1.Value = 0;
MessageBox.Show("Your username or password is wrong!","Login failed", MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
}
}
答案 0 :(得分:1)
另一种方法是设置TextBox的Lines
属性:
txtBoxUsers.Lines = users.ToArray();
答案 1 :(得分:0)
您永远不会清除文本框,只需在添加新用户时再次添加所有用户。
这可能是您正在寻找的:
users.Add(name);
txtBoxUsers.Text = string.Empty; // Clear the text first, txtBoxUsers.Clear() works too
foreach (var user in users) {
txtBoxUsers.Text += user + Environment.NewLine;
}
否则,您只需添加新用户而不是添加每个用户,那么您就不必清除。
答案 2 :(得分:0)
我做得很简单 `public partial class Form1:Form { 串传; 字符串名称; bool when = false; 公共Form1() { 的InitializeComponent(); } List users = new List(); private void Form1_Load(object sender,EventArgs e) {
}
public void regBtn_Click(object sender, EventArgs e)
{
if (regTxtBoxName.TextLength < 4)
{
when = true;
if (regTxtBoxPass.TextLength < 4)
{
{
if (when == false)
{
progBar1.Value = 0;
MessageBox.Show("Choose password/name with minimal length 5", "Registration ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
regTxtBoxPass.BackColor = Color.Red;
}
else
{
if (when == false)
{
progBar1.Value = 0;
MessageBox.Show("Choose password/name with minimal length 5", "Registration ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
regTxtBoxPass.BackColor = Color.White;
}
regTxtBoxName.BackColor = Color.Red;
progBar1.Value = 0;
MessageBox.Show("Choose password/name with minimal length 5", "Registration ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
regTxtBoxName.BackColor = Color.White;
if (regTxtBoxPass.TextLength < 4)
{
regTxtBoxPass.BackColor = Color.Red;
progBar1.Value = 0;
MessageBox.Show("Choose password/name with minimal length 5", "Registration ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
regAge.Minimum = 0;
regAge.Maximum = 150;
if (regAge.Value >= 15)
{
if(chkBox1.Checked){
regTxtBoxName.BackColor = Color.White;
regTxtBoxPass.BackColor = Color.White;
regAge.BackColor = Color.White;
pass = regTxtBoxPass.Text;
name = regTxtBoxName.Text;
users.Add(name);
txtBoxUsers.Text += Environment.NewLine + name;
regTxtBoxPass.Text = "";
regTxtBoxName.Text = "";
regAge.Value = 0;
progBar1.Value = 100;
MessageBox.Show("Your account has been succesfully created.", "Registration FINSIHED", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else{MessageBox.Show("Please, accept condition terms.", "Accept it omg....",MessageBoxButtons.OK, MessageBoxIcon.Warning);
progBar1.Value = 0;
};
}
else
{
regAge.BackColor = Color.Red;
regAge.Value = 0;
progBar1.Value = 0;
MessageBox.Show("You're too young for this :)", "Registration ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
public void logBtn_Click(object sender, EventArgs e)
{
if (logTxtBoxName.Text == name)
{
if (logTxtBoxPass.Text == pass)
{
progBar1.Value = 100;
MessageBox.Show("You have beeon successfully logged in.", "Logged IN", MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
progBar1.Value = 0;
MessageBox.Show("Your username or password is wrong!", "Login failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else {
progBar1.Value = 0;
MessageBox.Show("Your username or password is wrong!","Login failed", MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
}
`