简体   繁体   English

MessageBox中的ListBox和DisplayMember问题

[英]ListBox and DisplayMember problem in MessageBox

I'm a beginner in C#. 我是C#的初学者。 I have problem with take DisplayMember from ListBox. 我在从ListBox中获取DisplayMember时遇到问题。 I try to write simple application in access control. 我尝试在访问控制中编写简单的应用程序。 RacsSystem. RacsSystem。 is the API to access control server. 是访问控制服务器的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());


        }

    }

It's OK show DisplayMember "Name" in MessageBox. 可以在MessageBox中显示DisplayMember“名称”。

在此处输入图片说明

    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());

    }

It's not ok show me type of data ? 不能告诉我数据类型吗? I don't know. 我不知道。

在此处输入图片说明

I would like the same effect as in the first example. 我想要与第一个示例相同的效果。

Replace strText = strText + x.ToString() + "\\n"; 替换strText = strText + x.ToString() + "\\n"; with following. 与以下。

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

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

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