无法弄清楚我的代码有什么问题

时间:2013-11-09 15:35:52

标签: c# winforms

首先,对不起我的英语,这不是我的第一语言。 我在高中一年级,在我的大学,我们有一个三年的系统分析和开发证书课程,以及正常的课程。 我们刚开始在课程中学习c#,但由于我们在实验室有一些空闲时间,我们开始用winforms“玩”。我更喜欢看代码或谷歌错误而不是问,但这次我真的无法弄清楚什么是错的。

当我只使用一个表单时,为了获得输入并实时显示所有用户的信息(没有组合框,只是标签),一切正常。我想要做的是,输入五个用户,如果字段填写正确,将用户的名字作为项目添加到组合框中,他可以看到之前用户浏览的名称/性别/年龄组合框项目。

Form1的代码:

    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();
    }
}

Form2的代码:

 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];
    }

变量类(如果我使用多种形式,如果我错了,我认为会更容易这样工作,纠正我):

 class variaveis
{
    public static string[] nome = new string[5]; //name
    public static string[] sobrenome = new string[5]; //last name
    public static string[] sexo = new string[5]; //gender
    public static string[] idade = new string[5]; //age(string, checked with tryparse)
    public static int i = 0;
}

很抱歉,如果这是一个noob问题,或者错误很明显,但我几周前就开始使用WinForms了。

编辑: 所以现在的问题是: - 有时程序会把错误告诉我,即使所有条件都显然已经完成。

- 不要以另一种形式将项目添加到组合框中。试过这个:

    public void AddItem(object item)
    {
        comboBox1.Items.Add(variaveis.nome[variaveis.i]);
    }

在Form1中调用它:

frm2.AddItem(variaveis.nome[variaveis.i]);

语法似乎正确,但没有任何反应。

3 个答案:

答案 0 :(得分:0)

如果你这样设置,我认为解决你的数据会更容易。

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;

答案 1 :(得分:0)

我编辑了一些东西并解决了“if”问题。新代码:

 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”。我该如何解决这个问题?

答案 2 :(得分:0)

通过添加以下内容解决了这个问题:

public partial class Form2 : Form
{
    Form1 f1;
    public Form2(Form1 f1)
    {
        InitializeComponent();
        this.f1 = f1;

此:

public partial class Form1 : Form
{
    private Form2 frm2;

和此:

public Form1()
    {

        InitializeComponent();
        frm2 = new Form2(this);


    }

完整的代码有一些改进:

Form1中:

 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 = " ";
        }
    }

}

无论如何,谢谢你的帮助。

相关问题