簡體   English   中英

如何使用 C# windows form .NET 框架獲取僅顯示在 MessageBox 中的名稱?

[英]How can I get the name only to show in the MessageBox using C# windows form .NET framework?

我已經有一個包含名稱和 id 的類,並且我也在使用列表方法並包括每個學生的名稱和 id。我還從組合框中選擇一門課程,並從包含名稱的列表框中選擇一個索引和身份證。 i.Name 正在生成錯誤,我也嘗試過編寫 All students.SelectedItem 而不是 i.Name (這有效,但它顯示了名稱和 id )下面附加的是那個 [![在此處輸入圖像描述][ 1]][1] 這是我下面的代碼

 if (comboBox1.SelectedIndex != -1 && Allstudents.SelectedIndex != -1)

        {

            var query3 = from i in prod
                         where i == Allstudents.SelectedItem
                         select i.Name;
            foreach(var i in query3)
            {
                var b = query3  ;
            }

            MessageBox.Show("The student" + i.Name +
                "Has registered in the course" + comboBox1.SelectedItem);

我怎樣才能只顯示名字? [1]: https ://i.stack.imgur.com/ehQ6K.png

您可以使用 split string 方法來拆分選定的字符串。

假設中間有空格,請使用以下代碼作為演示:

if (listBox1.SelectedIndex != -1)
{
    string[] s = listBox1.SelectedItem.ToString().Split(' ');
    MessageBox.Show(s[1]);
}

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM