C# Can';我不知道是什么';我的密码有问题吗
首先,对不起我的英语,它不是我的第一语言。 我上的是高中一年级,在我的大学里,我们有一个为期三年的系统分析和开发证书课程,还有普通课程。 我们刚开始在课程中学习c#,但由于我们在实验室有一些空闲时间,我们开始“玩”winforms。我更喜欢看代码或者用谷歌搜索错误,而不是询问,但是这次我真的不知道是什么错了 当我只使用一个表单来获取输入并实时显示所有用户的信息(没有组合框,只有标签)时,一切都很好。我想做的是,输入五个用户,如果字段填写正确,将用户的名字作为项目添加到组合框中,他可以看到以前浏览组合框项目的用户的姓名/性别/年龄 表格1的代码:C# Can';我不知道是什么';我的密码有问题吗,c#,winforms,C#,Winforms,首先,对不起我的英语,它不是我的第一语言。 我上的是高中一年级,在我的大学里,我们有一个为期三年的系统分析和开发证书课程,还有普通课程。 我们刚开始在课程中学习c#,但由于我们在实验室有一些空闲时间,我们开始“玩”winforms。我更喜欢看代码或者用谷歌搜索错误,而不是询问,但是这次我真的不知道是什么错了 当我只使用一个表单来获取输入并实时显示所有用户的信息(没有组合框,只有标签)时,一切都很好。我想做的是,输入五个用户,如果字段填写正确,将用户的名字作为项目添加到组合框中,他可以看到以前浏览
public partial class Form1 : Form
{
Form2 frm2 = new Form2();
public Form1()
{
InitializeComponent();
}
private void label9_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
variaveis.i++;
variaveis.nome[variaveis.i] = textBox1.Text;
variaveis.sobrenome[variaveis.i] = textBox2.Text;
variaveis.sexo[variaveis.i] = comboBox1.Text;
if (textBox3.Text != null)
variaveis.idade[variaveis.i] = textBox3.Text;
double num;
bool isnum = double.TryParse(variaveis.idade[variaveis.i], out num);
Form2 frm2 = new Form2();
frm2.Update();
if (variaveis.nome[variaveis.i]!=null && variaveis.sobrenome!=null && variaveis.idade[variaveis.i] != null && isnum)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
comboBox1.Refresh();
frm2.comboBox1.Items.Add(variaveis.nome[variaveis.i]); //Only works with the first input
if (variaveis.i == 1)
{
frm2.Show();
frm2.Location = new Point(this.Left + this.Width, this.Top);
frm2.Height = this.Height;
frm2.label6.Text = variaveis.nome[variaveis.i];
frm2.label7.Text = variaveis.sobrenome[variaveis.i];
frm2.label8.Text = variaveis.sexo[variaveis.i];
frm2.label9.Text = variaveis.idade[variaveis.i];
}
}
else
{
variaveis.i--;
MessageBox.Show("Preencha todos os campos",
"Erro");
}
if (variaveis.i >= 5)
{
button1.Enabled = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
comboBox1.Refresh();
}
}
表格2的代码:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label6.Text = variaveis.nome[comboBox1.SelectedIndex + 1]; // i don't even know if i can use an array like this
label7.Text = variaveis.sobrenome[comboBox1.SelectedIndex + 1];
label8.Text =variaveis.sexo[comboBox1.SelectedIndex + 1];
label9.Text = variaveis.idade[comboBox1.SelectedIndex + 1];
}
variables类(我认为如果我使用多个表单,这样做会更容易,如果我错了,请纠正我):
很抱歉,如果这是一个noob问题或者错误很明显,但是我几周前就开始使用WinForms了
编辑:
因此,现在的问题是:
-有时,即使所有条件都明显满足,程序也会向我抛出错误
-无法将项目添加到其他表单中的组合框。我试过这个:
public void AddItem(object item)
{
comboBox1.Items.Add(variaveis.nome[variaveis.i]);
}
将其命名为Form1:
frm2.AddItem(variaveis.nome[variaveis.i]);
语法似乎正确,但什么也没发生。如果您这样设置,我认为处理数据更容易
public class Person
{
public string Nome {get; set;}
public string Sobrenome {get; set;}
public string Sexo {get; set;}
public string Idade {get; set;}
}
public class Variaveis
{
public Person[] People {get; set;}
public Variaveis
{
People = new Person[5];
}
}
现在,您可以像这样访问数据:
var myName = Variaveis.People[2].Nome;
我编辑了一些东西,解决了“如果”的问题。新守则:
public partial class Form1 : Form
{
public string[] nome = new string[5];
public string[] sobrenome = new string[5];
public string[] sexo = new string[5];
public string[] idade = new string[5];
public int i = 1;
public Form1()
{
InitializeComponent();
}
private void label9_Click(object sender, EventArgs e)
{
}
public void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
nome[i] = textBox1.Text;
sobrenome[i] = textBox2.Text;
sexo[i] = comboBox1.Text;
idade[i] = textBox3.Text;
double num;
bool isnum = double.TryParse(idade[i], out num);
if (textBox1.Text !=null && textBox2.Text!=null && textBox3 != null && isnum == true)
{
frm2.comboBox1.Items.Add(textBox1.Text);
frm2.comboBox1.Update();
comboBox1.Update();
textBox4.Text = Convert.ToString(i); // just to check the variable's value, since they dont appear in the debugger tool
if (i == 1)
{
frm2.Show();
frm2.Location = new Point(this.Left + this.Width, this.Top);
frm2.Height = this.Height;
}
frm2.label6.Text = nome[i];
frm2.label7.Text = sobrenome[i];
frm2.label8.Text = sexo[i];
frm2.label9.Text = idade[i];
frm2.comboBox1.SelectedIndex = frm2.comboBox1.SelectedIndex + 1;
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
++i;
}
if(isnum == false && textBox1.Text == null && textBox2.Text == null && textBox3 == null)
{
MessageBox.Show("Preencha todos os campos", "Erro");
}
if (i >= 5)
{
button1.Enabled = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
comboBox1.Refresh();
}
private void Form1_Load(object sender, EventArgs e)
{
}
我注意到所有控件(不仅仅是combobox1)只响应来自另一个表单的第一个命令。如果我在form2中放置一个文本框并调用:
frm2.textBox1.Text = i;
它将永远显示“1”。如何解决此问题?通过添加以下内容,我自己解决了此问题:
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
InitializeComponent();
this.f1 = f1;
这:
这是:
public Form1()
{
InitializeComponent();
frm2 = new Form2(this);
}
完整的代码和一些改进:
表格1:
public partial class Form1 : Form
{
private Form2 frm2;
public string[] nome = new string[6];
public string[] sobrenome = new string[6];
public string[] sexo = new string[6];
public string[] idade = new string[6];
public int i = 0;
public Form1()
{
InitializeComponent();
frm2 = new Form2(this);
}
public void button1_Click(object sender, EventArgs e)
{
++i;
nome[i] = textBox1.Text;
sobrenome[i] = textBox2.Text;
sexo[i] = comboBox1.Text;
idade[i] = textBox3.Text;
double num;
bool isnum = double.TryParse(idade[i], out num);
if (textBox1.Text !=null && textBox2.Text!=null && textBox3 != null && isnum == true)
{
frm2.comboBox1.Items.Add(textBox1.Text);
frm2.comboBox1.Update();
comboBox1.Update();
if (i == 1)
{
frm2.Show();
frm2.Location = new Point(this.Left + this.Width, this.Top);
frm2.Height = this.Height;
}
frm2.label6.Text = nome[i] + " " + sobrenome[i];
frm2.label8.Text = sexo[i];
frm2.label9.Text = idade[i];
frm2.comboBox1.SelectedIndex = frm2.comboBox1.SelectedIndex + 1;
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
if(isnum == false && textBox1.Text == null && textBox2.Text == null && textBox3 == null)
{
MessageBox.Show("Preencha todos os campos", "Erro");
--i;
}
if (i >= 5)
{
button1.Enabled = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
comboBox1.Refresh();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
表格2:
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
InitializeComponent();
this.f1 = f1;
comboBox1.Items.Add("Usuários");
}
private void Form2_Load(object sender, EventArgs e)
{
}
public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex != 0)
{
label6.Text = f1.nome[comboBox1.SelectedIndex] + " " + f1.sobrenome[comboBox1.SelectedIndex];
label8.Text = f1.sexo[comboBox1.SelectedIndex];
label9.Text = f1.idade[comboBox1.SelectedIndex];
}
else
{
label6.Text = " ";
label8.Text = " ";
label9.Text = " ";
}
}
}
无论如何,谢谢你的帮助。到底是什么不起作用?请尽可能准确地描述错误。您的类
variaveis
是以assembly
的方式设计的。为什么不直接使用属性呢?它更具可读性和可维护性,这也是我们在高级OOP编程语言中的做法。您两次声明frm2,我也会在Form2上创建公共属性,而不是依赖正在使用的控件public@germi有时它会给我错误(“字段填写不正确”),甚至包括所有条件(在if中)显然是fulfilled@MarkHall哦,谢谢,我在编辑代码后忘了删除它。你的例子没有编译。另外,您正在访问Variaveis,就好像它是一个静态类一样,但是您没有将它声明为一个带有静态变量的静态类。请不要对您自己的问题发表后续文章,除非它是一个答案;如果你想更改任何内容,只需编辑你原来的帖子。
public partial class Form1 : Form
{
private Form2 frm2;
public string[] nome = new string[6];
public string[] sobrenome = new string[6];
public string[] sexo = new string[6];
public string[] idade = new string[6];
public int i = 0;
public Form1()
{
InitializeComponent();
frm2 = new Form2(this);
}
public void button1_Click(object sender, EventArgs e)
{
++i;
nome[i] = textBox1.Text;
sobrenome[i] = textBox2.Text;
sexo[i] = comboBox1.Text;
idade[i] = textBox3.Text;
double num;
bool isnum = double.TryParse(idade[i], out num);
if (textBox1.Text !=null && textBox2.Text!=null && textBox3 != null && isnum == true)
{
frm2.comboBox1.Items.Add(textBox1.Text);
frm2.comboBox1.Update();
comboBox1.Update();
if (i == 1)
{
frm2.Show();
frm2.Location = new Point(this.Left + this.Width, this.Top);
frm2.Height = this.Height;
}
frm2.label6.Text = nome[i] + " " + sobrenome[i];
frm2.label8.Text = sexo[i];
frm2.label9.Text = idade[i];
frm2.comboBox1.SelectedIndex = frm2.comboBox1.SelectedIndex + 1;
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
if(isnum == false && textBox1.Text == null && textBox2.Text == null && textBox3 == null)
{
MessageBox.Show("Preencha todos os campos", "Erro");
--i;
}
if (i >= 5)
{
button1.Enabled = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
comboBox1.Refresh();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
InitializeComponent();
this.f1 = f1;
comboBox1.Items.Add("Usuários");
}
private void Form2_Load(object sender, EventArgs e)
{
}
public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex != 0)
{
label6.Text = f1.nome[comboBox1.SelectedIndex] + " " + f1.sobrenome[comboBox1.SelectedIndex];
label8.Text = f1.sexo[comboBox1.SelectedIndex];
label9.Text = f1.idade[comboBox1.SelectedIndex];
}
else
{
label6.Text = " ";
label8.Text = " ";
label9.Text = " ";
}
}
}