【问题标题】:C# listbox add data from another formC# 列表框从另一个表单添加数据
【发布时间】:2015-11-27 00:15:27
【问题描述】:

我是一名 C# 学生,但我的期中项目有点卡住了。

我在此处放弃了我的项目和规范:https://www.dropbox.com/sh/eo5ishsvz4vn6uz/CE3F4nvgDf

如果你运行程序,它会来到我离开的最后一个区域..

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) + " ";
}

有一个主窗体,一个辅助添加窗体,还有一个第三和第四个窗体,所有控件都到位,剩下的就是接线。

(1) 在上面的代码中,应该有 3 个分数传递给主窗体,这些分数与一个学生姓名字符串一起填充主窗体上的 ListBox。我不知道如何访问那个 ListBox,只要我输入“listStudents”,什么都没有发生。

(2) 当我单击“添加”按钮 1 次时,我也不确定如何将输入限制为仅 3 个分数,这意味着我知道我的 for 循环可能完全错误。我不知道是否应该将这些分数保存到数组、列表或单个变量中,因为它可以是 3 个(或更多,但 3 个也可以)分数。

(3) 当我在 AddNewStudent 表单上点击“OK”时,我是在那里编写代码来填充主表单 ListBox,还是进入主表单?

更新:

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|"

在我看来,这段代码是正确的,用于填充 ListBox,但我不确定如何将列表的全部内容添加到字符串,然后将其添加到列表框。

【问题讨论】:

    标签: c# forms listbox


    【解决方案1】:

    这将使您的代码构建和运行。

    在form1中更改以下声明

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

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

    并将您的 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);
    
    }
    

    其余的都不是太难,你应该可以解决的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 2020-09-15
      • 2017-09-13
      相关资源
      最近更新 更多