MessageBox中的ListBox和DisplayMember问题

时间:2018-12-21 08:48:56

标签: c#

我是C#的初学者。我在从ListBox中获取DisplayMember时遇到问题。 我尝试在访问控制中编写简单的应用程序。 RacsSystem。是访问控制服务器的API。

 private void libStrefy_SelectedIndexChanged(object sender, EventArgs e)
    {
        var attendanceZone = libStrefy.SelectedItem as AttendanceZoneData;
        if (attendanceZone != null)
        {
            CalculateAndShowZoneOccupancy(attendanceZone.ID);
            //-----------------------
            var  occupancies = RacsSystem.Instance.Integration.GetAttendanceZoneOccupancies(attendanceZone.ID,
               RacsSystem.Instance.SessionToken);
            //libOsoby.ValueMember = "ID";
           libOsoby.DisplayMember = "Name"; // bez tego nazwa obiektu
           libOsoby.DataSource = occupancies;
           libOsoby.SelectedIndex = 0;

           System.Text.StringBuilder sb = new System.Text.StringBuilder();

            foreach (var item in occupancies)
            {
                sb.Append(item.Name);
                sb.Append(" ---- ");
                sb.Append(item.Description);
                sb.Append(" ++ ");       
            }
            MessageBox.Show(sb.ToString());


        }

    }

可以在MessageBox中显示DisplayMember“名称”。

enter image description here

    private void button1_Click_1(object sender, EventArgs e)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

         libOsoby.SelectionMode = SelectionMode.MultiSimple;

                 for (int val = 0; val < libOsoby.Items.Count; val++)
                 {
                     libOsoby.SetSelected(val, true);
                 }


                 string strText = "";
                      foreach (var x in libOsoby.SelectedItems)
                          {
                              strText = strText + x.ToString() + "\n";
                          }

                         myReader = new System.IO.StringReader(strText);


                 MessageBox.Show(strText.ToString());

    }

不能告诉我数据类型吗?我不知道。

enter image description here

我希望获得与第一个示例相同的效果。

1 个答案:

答案 0 :(得分:1)

用以下内容替换strText = strText + x.ToString() + "\n";

strText = strText + x.Text.ToString() + "\n";