C# 如何以另一种形式显示结果?

C# 如何以另一种形式显示结果?,c#,.net,excel,visual-studio,C#,.net,Excel,Visual Studio,我是c代码的新手。我想知道如何通过下面的代码将字符串str1的值传递到另一个窗体并在另一个windows窗体中显示它。我可以在messagebox中显示它作为messagebox.Showstr1;但是我想传递str1的值并以另一种形式显示它 enter code here <pre> <code> using System; using System.Collections.Generic; using System.ComponentModel; using Sy

我是c代码的新手。我想知道如何通过下面的代码将字符串str1的值传递到另一个窗体并在另一个windows窗体中显示它。我可以在messagebox中显示它作为messagebox.Showstr1;但是我想传递str1的值并以另一种形式显示它

enter code here

<pre> <code>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using MyExcel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Microsoft.Vbe.Interop;
using System.Diagnostics;
命名空间贷款 {

}

您可以在第二个表单上声明一个变量,该变量将在第一个表单上设置:

将此添加到将接收消息的第二个表单上

   Form2 frm = new Form2();
   frm.strVariable = "Hello World";
   frm.Show();
然后在第一个窗体上,在显示变量之前声明它:

 private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = strVariable;
    }
加载第二个表单接收器时,您可以访问数据并将其显示给用户,在这种情况下,标签(例如表单加载时):

public partial class Form2 : Form
{
    private string _stringToShow;
    public Form2(string stringToShow)
    {
        _stringToShow = stringToShow;
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = _stringToShow;
    }
}

您可以在第二个窗体上声明一个变量,该变量将在第一个窗体上设置:

将此添加到将接收消息的第二个表单上

   Form2 frm = new Form2();
   frm.strVariable = "Hello World";
   frm.Show();
然后在第一个窗体上,在显示变量之前声明它:

 private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = strVariable;
    }
加载第二个表单接收器时,您可以访问数据并将其显示给用户,在这种情况下,标签(例如表单加载时):

public partial class Form2 : Form
{
    private string _stringToShow;
    public Form2(string stringToShow)
    {
        _stringToShow = stringToShow;
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = _stringToShow;
    }
}

一个好的解决方案是在第二种形式的构造函数中发送字符串。此外,您还可以将标签放入第二个表单中,并将字符串保存在该标签中:

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

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        List<string> Ll = new List<string>() { "one", "two", "three" };
        string concatenate = string.Join(" ", Ll.ToArray());
        new Form2(concatenate ).Show();
    }
}
从第一种形式我称之为第二种形式:


希望能有所帮助。

一个好的解决方案是在第二种形式的构造函数中发送字符串。此外,您还可以将标签放入第二个表单中,并将字符串保存在该标签中:

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

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        List<string> Ll = new List<string>() { "one", "two", "three" };
        string concatenate = string.Join(" ", Ll.ToArray());
        new Form2(concatenate ).Show();
    }
}
从第一种形式我称之为第二种形式:


希望有帮助。

感谢您提供的解决方案。我确实试过了,但我得到了一个错误->无法从“System.Collections.Generic.List”转换为“string”,您在那里发送了什么值?看起来您在列表和字符串之间进行了错误的转换。我还在解决方案中添加了第一个窗口的代码。我正在从excel文件中检索单元格值,并将它们存储在字符串列表中。现在,我想以第二种形式显示检索到的值。我没有使用文本框。您可以将列表中的值串联在一个字符串中,然后将该字符串发送到第二个表单的构造函数中,然后按照我在表单2的解决方案中的示例进行操作。我用一个连接示例编辑了Form1,并将结果字符串发送到Form2。感谢您提供的解决方案。我确实试过了,但我得到了一个错误->无法从“System.Collections.Generic.List”转换为“string”,您在那里发送了什么值?看起来您在列表和字符串之间进行了错误的转换。我还在解决方案中添加了第一个窗口的代码。我正在从excel文件中检索单元格值,并将它们存储在字符串列表中。现在,我想以第二种形式显示检索到的值。我没有使用文本框。您可以将列表中的值串联在一个字符串中,然后将该字符串发送到第二个表单的构造函数中,然后按照我在表单2的解决方案中的示例进行操作。我用一个连接示例编辑了Form1,并将结果字符串发送到Form2。