繁体   English   中英

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

[英]Can't figure what's wrong with my code

首先,对不起我的英语,这不是我的母语。 我正在读高中一年级,在我的大学里,我们有一个为期三年的系统分析和开发证书课程以及普​​通班。 我们只是在课程中开始学习c#,但是由于我们在实验室有一些空闲时间,因此我们开始使用winforms“玩”游戏。 我更喜欢看代码或Google错误而不是询问,但是这次我真的不知道出了什么问题。

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

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

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

如果您这样进行设置,我认为处理您的数据会更容易。

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

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM