【问题标题】:Can't figure what's wrong with my code无法弄清楚我的代码有什么问题
【发布时间】:2013-11-21 13:30:52
【问题描述】:

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

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

【问题讨论】:

  • 究竟是什么不起作用?请尽可能准确地描述错误。
  • 你的班级variaveis是按照assembly的方式设计的。你为什么不直接使用属性呢?它更具可读性和可维护性,这就是我们在高级 OOP 编程语言中的做法。
  • 您声明了两次 frm2,我也会在 Form2 上创建公共属性,而不是依赖于公开的控件
  • @germi 有时它会给我错误(“字段未正确填写”),即使所有条件(如果内部)显然都已满足
  • @MarkHall 哦,谢谢,我在编辑代码后忘记删除它

标签: c# winforms


【解决方案1】:

如果你这样设置,我认为更容易处理你的数据。

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;

【讨论】:

  • 您的示例无法编译。另外,您正在访问 Variaveis,就好像它是一个静态类一样,但您没有将它声明为具有静态变量的静态类。
【解决方案2】:

我编辑了一些东西并解决了“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)
    {

    }

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

frm2.textBox1.Text = i;

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

【讨论】:

  • 请不要对您自己的问题进行后续跟进,除非它是一个答案;如果您想更改任何内容,只需编辑您的原始帖子即可。
【解决方案3】:

自己解决了这个问题,添加了这个:

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

}

无论如何感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-16
    • 2012-03-15
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 2017-05-30
    相关资源
    最近更新 更多