c# - 将以一种形式输入的数据显示到另一种形式

标签 c# .net winforms listbox

我想要另一种形式将所选项目从一种形式的列表框中显示到另一种形式的富文本框,该文本框在单击按钮时打开。我在表单 1 中使用了以下代码片段来显示消息框中的内容,但现在我想要另一种形式。需要帮助...

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)
        {



            StringBuilder message = new StringBuilder();

            foreach (object selectedItem in listBox1.SelectedItems)
            {
               message.Append(selectedItem.ToString() + Environment.NewLine);
            }
            MessageBox.Show("You selected: \n" +message.ToString());         


        }

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

    }
}

最佳答案

我使用下面的代码来实现我想要的。它做得很好! :)

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)
        {



            StringBuilder message = new StringBuilder();


            foreach (object selectedItem in listBox1.SelectedItems)
            {
               message.Append(selectedItem.ToString() + Environment.NewLine);
            }
            MessageBox.Show("Your Selected Cities :\n" + message.ToString() + "\n");

            Form2 childForm = new Form2();
            childForm.Controls["richTextBox1"].Text = message.ToString();
            childForm.Show();
        }

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

    }
}

关于c# - 将以一种形式输入的数据显示到另一种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15105914/

相关文章:

c# - 在 C# 中,从字符串中解析出此值的最佳方法是什么?

c# - 在任务中处理异常的正确方法 continuewith

c# - 反射和泛型获取属性值

c# - 跨线程操作问题

winforms - 您对 NetAdvantage 或 EXperience 的体验如何?

c# - 打印图片框

c# - 如何在我的字符串中插入 '-'?

c# - 将 JSON 字符串中的数据覆盖到现有对象实例

c# - Visual Studio 2015 100% 的 CPU 使用率与 Node

c# - 按长度变量将字符串拆分为更小的字符串