Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将listbox的选定项显示到消息框中_C#_.net_Listbox_Messagebox - Fatal编程技术网

C# 将listbox的选定项显示到消息框中

C# 将listbox的选定项显示到消息框中,c#,.net,listbox,messagebox,C#,.net,Listbox,Messagebox,我可以通过单击按钮将列表框中的多个选定项目显示到文本框中,但如何在消息框中显示相同的项目?我的意思是,在messagebox上显示第一个项目不是问题,但同时显示多个项目才是问题。建议请 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; u

我可以通过单击按钮将列表框中的多个选定项目显示到文本框中,但如何在消息框中显示相同的项目?我的意思是,在messagebox上显示第一个项目不是问题,但同时显示多个项目才是问题。建议请

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cities
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            foreach (object selectedItem in listBox1.SelectedItems)
            {
               textBox1.AppendText(selectedItem.ToString() + Environment.NewLine);
            }


        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

您可以创建临时变量来保存其中的文本,然后创建messagebox

StringBuilder message = new StringBuilder();
foreach (object selectedItem in listBox1.SelectedItems)
{
    message.AppendLine(selectedItem.ToString());
}
MessageBox.Show(message.ToString());
在您的按钮中单击-

        textBox1.Clear();
        string str = string.Empty;
        foreach (object selectedItem in listBox1.SelectedItems)
        {
            str += selectedItem.ToString() + Environment.NewLine;
        }

        textBox1.Text = str;
        MessageBox.Show(str);

您可以基于所有
SelectedItems
创建一个字符串,然后在消息框中显示该字符串。像

string str = string.Join(",",
                        listBox1.SelectedItems.Cast<object>().Select(r => r.ToString()));
MessageBox.Show(str);
string str=string.Join(“,”,
listBox1.SelectedItems.Cast().Select(r=>r.ToString());
MessageBox.Show(str);

我认为如果选择的项目太多,那么“确定”按钮和顶部栏可能会从屏幕上消失:)这是一个非常好的解决方案!谢谢