简体   繁体   English

在 C# 中将数据从表格 1 传递到表格 2?

[英]Passing data from form 1 to form 2 in C#?

This is the first part of my code, I didn't include all of the button clicks and methods so it wouldn't be as long.这是我的代码的第一部分,我没有包括所有的按钮点击和方法,所以它不会那么长。 The comments included in the code are instructions from my professor.代码中包含的注释是我教授的指示。

When I initialize the ScoresDisplay array in wndTestScores and use the showDialog the values are reset once the wndDisplayScores is opened.当我在 wndTestScores 中初始化 ScoresDisplay 数组并使用 showDialog 时,一旦打开 wndDisplayScores,值就会被重置。 Does anyone have any ideas as to what I'm doing wrong?有人对我做错了什么有任何想法吗? Please go easy on me, I'm very new to C#.请 go 对我放心,我对 C# 很陌生。

 public partial class wndTestScores : Window
 {
        public static int[] scores = new int[21];
        public static int amountOfScores = 1;

    public wndTestScores()
    {
        InitializeComponent();

    }

  


    private void btnAsEntered_Click(object sender, RoutedEventArgs e)
    {
        //Create an instance of the WPF window wndDisplayScores
        wndDisplayScores displayScores = new wndDisplayScores();


        //Code a for statement to loop through each array
        //element of your integer array of test scores. If the
        //test score in your array element is greater than 0 then
        //set ScoresDisplay array element to your array element value.
        //The ScoresDisplay is a public array on the DisplayScores
        //window.
        for(int x = 1; x < amountOfScores; x++)
        {
            if (scores[x] > 0)
                displayScores.ScoresDisplay[x] = scores[x];
        }

        //Display the DisplayScores window by calling the ShowDialog()
        //method


        displayScores.ShowDialog();

    }

    private void BtnAddScore_Click(object sender, RoutedEventArgs e)
    {
        int score;
        bool valid = false;

        if(amountOfScores > 20)
        {
            MessageBox.Show("Max amount of test scores entered. Please select another option.");
            return;
        }
        
        Int32.TryParse(txtTestScore.Text, out score);

        if (Int32.TryParse(txtTestScore.Text, out score) == false)
        {
            MessageBox.Show("Invalid input try again.");
            txtTestScore.Clear();
        }
        
        valid = isValid(valid, score);

        if (valid == true)
        {
            scores[amountOfScores] = score;
            amountOfScores++;
        }


        if(amountOfScores > 2)
        {
            rdoAverage.IsEnabled = true;
            rdoHighest.IsEnabled = true;
            rdoLowest.IsEnabled = true;
            rdoMedian.IsEnabled = true;
        }
        
    }
}

''' '''

public partial class wndDisplayScores : Window
{
    public int[] ScoresDisplay = new int[21];

    public wndDisplayScores()
    {
        InitializeComponent();
        Load_TestScores();

    }
    private void Load_TestScores()
    {
        //Code a foreach statement that will iterate
        //through the array ScoresDisplay. If the array 
        //element contains a test score greater than 0
        //then add the test score to the ListBox control
        foreach(int x in ScoresDisplay)
        {
            if (x > 0)
                lstScores.Items.Add(x);
        }

    }

    private void btnClose_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        
    }
}

} }

I think you mean that lstScores collection in wndDisplayScores form is empty.我认为您的意思是 wndDisplayScores 表单中的 lstScores 集合是空的。

The reason is pretty straight forward:原因很简单:

private void btnAsEntered_Click(object sender, RoutedEventArgs e)
{        
    LINE 1: wndDisplayScores displayScores = new wndDisplayScores();

    LINE 2: for(int x = 1; x < amountOfScores; x++)
    {
        if (scores[x] > 0)
            displayScores.ScoresDisplay[x] = scores[x];
    }

    LINE 3: displayScores.ShowDialog();

}

In this method, you first create an instance of second form at LINE 1, which calls its constructor right away.在此方法中,您首先在第 1 行创建第二个表单的实例,该实例立即调用其构造函数。 In the constructor of wndDisplayScores - you have a call to Load_TestScores();在 wndDisplayScores 的构造函数中 - 您调用了 Load_TestScores();

This runs right away and at this time there is nothing in "public int[] ScoresDisplay = new int[21];"这会立即运行,此时“public int[] ScoresDisplay = new int[21];”中没有任何内容member.成员。 Because that will happen in forloop LINE 2 after wards.因为这将在 forloop LINE 2 之后发生。

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

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