0

首先,对不起我的英语,它不是我的母语。我在高中一年级,在我的大学里,我们有一个为期三年的系统分析和开发证书课程,以及普通课程。我们刚刚开始在课程中学习 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;
}

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

编辑:所以现在的问题是: - 有时即使所有条件都明显满足,程序也会向我抛出错误。

- 不能以其他形式将项目添加到组合框。试过这个:

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

在 Form1 中调用它:

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

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

4

3 回答 3

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


    }

带有一些改进的完整代码:

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

}

无论如何感谢您的帮助。

于 2013-11-10T15:10:14.847 回答
0

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

 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)
    {

    }

我注意到所有控件(不仅仅是组合框1)只响应来自另一个表单的第一个命令。如果我在 form2 中放置一个文本框并调用:

frm2.textBox1.Text = i;

它将永远显示“1”。我该如何解决这个问题?

于 2013-11-10T00:03:15.473 回答
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;
于 2013-11-09T16:34:29.083 回答