winforms 为什么新的元素不添加到列表中?

vmpqdwk3  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(142)

我有一个C# WinForms数独游戏。Form1是主菜单窗体,Form2是游戏窗体。我创建球员姓名和得分列表。我试图使它工作,这样在一场胜利后,球员的分数被添加到列表中,然后它将被添加到Form1.listBox1。但是新球员的分数并没有增加到名单上,我不明白为什么。
这是Form1代码:

public partial class Form1 : Form
{
    public Form2 frm2;
    public Form1 frm1;
    
    public Form1()
    {
        InitializeComponent(); 
    }
    private void button1_Click(object sender, EventArgs e)
    {
        frm2.players.Add(textBox1.Text);

        textBox1.Text = "";
        frm2.Show();
        this.Hide();
    }
    private void Form1_Load(object sender, EventArgs e)
    {  
        frm2 = new Form2();
        frm2.frm1 = this;     
    }

这是简化的Form2代码:

public partial class Form2 : Form
{
    public Form1 frm1;
    int s;
    public List<string> players = new List<string>(5);
    public List<int> score = new List<int>(5) ;

    public Form2()
    {
        InitializeComponent();
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        frm1 = new Form1();
        frm1.frm2 = this;
        s = 0;
        timer1.Start();
        label1.Text = Convert.ToString(s);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        
        timer1.Stop();
        label1.Text = " ";
        MessageBox.Show("Верно!");
        score.Add(GetScoreForLevel());
        this.Close();
        frm1.listBox1.BeginUpdate();
        foreach (string o in players)
        {
            foreach (int j in score)
            {
                frm1.listBox1.Items.Add($"{o} {j}");
            }
        }
        players.ForEach(Console.WriteLine);
        score.ForEach(Console.WriteLine);
        
        frm1.listBox1.EndUpdate();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        s = s + 1;
        label1.Text = Convert.ToString(s);
    }
    private int GetScoreForLevel()
    {
        return s;
    }
}

我需要在我的C# WinForms数独游戏中创建一个高分表,我需要当玩家单击播放按钮时,textBox1中的文本添加到玩家列表中,并且在玩家获胜后,他的分数(他获胜的时间)添加到分数列表中,然后两个列表都需要显示在listBox1中。

nxagd54h

nxagd54h1#

查看代码:

public partial class Form1 : Form {
  public Form2 frm2;
  public Form1 frm1;

  public Form1() {
     InitializeComponent();
  }
  private void button1_Click(object sender, EventArgs e) {
     frm2.players.Add(textBox1.Text);

     textBox1.Text = "";
     frm2.Show();
     this.Hide();
  }
  private void Form1_Load(object sender, EventArgs e) {
     frm2 = new Form2();
     frm2.frm1 = this;
  }

}
和形式2:

public Form1 frm1;
  int s;
  public List<string> players = new List<string>(5);
  public List<int> score = new List<int>(5);

  public Form2() {
     InitializeComponent();
  }
  // New event, not added in the designer
  protected override void OnActivated(EventArgs e) { 
     base.OnActivated(e);
     if (!this.timer1.Enabled) {
        this.timer1.Start();
        s = 0;
     }
  }
  private void Form2_Load(object sender, EventArgs e) {
     //frm1 = new Form1();
     frm1.frm2 = this;
     s = 0;
     timer1.Start();
     label1.Text = Convert.ToString(s);
  }
  private void button1_Click(object sender, EventArgs e) {

     timer1.Stop();
     label1.Text = " ";
     MessageBox.Show("Верно!");
     score.Add(GetScoreForLevel());
     this.Hide();
     frm1.listBox1.BeginUpdate();
     // No understand the visualization XD
     foreach (string o in players) {
        foreach (int j in score) {
           frm1.listBox1.Items.Add($"{o} {j}");
        }
     }
     players.ForEach(Console.WriteLine);
     score.ForEach(Console.WriteLine);

     frm1.listBox1.EndUpdate();
     frm1.Show();
  }
  private void timer1_Tick(object sender, EventArgs e) {
     s = s + 1;
     label1.Text = Convert.ToString(s);
  }
  private int GetScoreForLevel() {
     return s;
  }

}
你的问题出在Form2_Load,因为你创建了一个新的Form 1示例,当你重新显示窗体时,你没有显示前一个!我已经实现了新的事件时,形式是激活的,以这种方式,我重新启动计时器。

相关问题