简体   繁体   English

C#列表框从其他表单添加数据

[英]C# listbox add data from another form

I'm a C# student and I'm a little stuck at on my midterm project. 我是C#学生,对中期项目有些犹豫。

I dropped my project and spec here: https://www.dropbox.com/sh/eo5ishsvz4vn6uz/CE3F4nvgDf 我将项目和规格放在这里: https : //www.dropbox.com/sh/eo5ishsvz4vn6uz/CE3F4nvgDf

If you run the program, it will come to the last area I left off at.. 如果您运行该程序,它将到达我上次离开的最后一个区域。

private void btnAddScore_Click(object sender, EventArgs e)
{
  for (int i = 0; i < 3; i++)
  {  
  tempScore = Convert.ToDecimal(txtScore.Text);
  Form1.scoreList = tempScore; (was Form1.scoreList[i] = tempScore;)
  }
  txtScoresList.Text += Convert.ToString(tempScore) + " ";
}

There's a main form, a secondary add form, and a third and fourth form, all the controls are in place, just the wiring is what's left over. 有一个主窗体,一个辅助添加窗体,以及一个第三和第四个窗体,所有控件都就位,仅剩下的就是接线。

(1) In the above code, there are supposed to be 3 scores passed to the main form, which, along with a student name string, are to populate the ListBox on the main form. (1)在上面的代码中,应该将3个分数传递给主表单,这些分数连同一个学生姓名字符串一起将填充到主表单的ListBox中。 I can't figure out how to access that ListBox, anytime I type "listStudents" nothing happens. 我不知道如何访问该ListBox,每当我键入“ listStudents”时,都不会发生任何事情。

(2) I'm also not sure how to limit an input of only 3 scores when I'm clicking the "add" button 1 time, which means I know my for loop is probably completely wrong. (2)当我单击“添加”按钮1次时,我也不确定如何将输入的分数限制为仅3个,这意味着我知道for循环可能是完全错误的。 I don't know if I should save those scores to an array, list, or individual vars, being that it can be 3 (or more, but 3 is fine) scores. 我不知道是否应该将这些分数保存到数组,列表或单个变量中,因为它可以是3分(或更高,但3分就可以了)。

(3) When I hit "OK" on the AddNewStudent form, do I write my code there to populate the main form ListBox, or does it go in the main form? (3)当我在AddNewStudent窗体上单击“确定”时,是否在此处编写代码以填充主窗体ListBox,还是将其放入主窗体中?

Update: 更新:

private void Form1_Load(object sender, EventArgs e)
{
    lbStudents.Items.Clear();
    //something like
    foreach (decimal i in scoreList2)
        {
            scoreList = scoreList2.ToString(); //gives me a cannot implicitly convert error
        }
    lbStudents.Items.Add(tempInfo1 + " " + scoreList2);
}
//I want the listbox to populate like "Name - |100| |90| |80|"

This code seems to me, to be correct, for getting the ListBox populated, but I'm unsure of how to add the entire contents of the list to a string, and then add that to the listbox. 在我看来,这段代码是正确的,用于填充ListBox,但是我不确定如何将列表的全部内容添加到字符串中,然后将其添加到列表框中。

This will get your code building and running. 这将使您的代码建立并运行。

Change the following declaration in form1 在form1中更改以下声明

public static decimal[] scoreList = new decimal[3];

to

public static List<decimal> scoreList = new List<decimal>();

and update your btnAddScore_Click handler to 并将您的btnAddScore_Click处理程序更新为

 //save scores to temp static var, populate noread txtbox txtScoresList with scores
for (int i = 0; i < 3; i++)
{   
    //save score to static var for trans-form data sending      
    tempScore = Convert.ToDecimal(txtScore.Text);
    Form1.scoreList.Add(tempScore);

}

The rest is not too difficult, you should be able to work it out. 其余的不太困难,您应该可以解决。

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

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