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

时间:2017-02-18 06:35:39

标签: c# .net excel visual-studio

我是c#编码的新手。我想知道如何将字符串str1的值传递给另一个表单,并从下面的代码中以另一种窗体形式显示它。好吧,我可以在消息框中显示它" MessageBox.Show(str1);"但我想传递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;

名称空间贷款 {

public partial class Form1 : Form
{
    public Form1()
    {

        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {




        OpenFileDialog fdlg = new OpenFileDialog();
        fdlg.Title = "Select an Excel File";
        fdlg.InitialDirectory = @"d:\test";
        fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
        fdlg.FilterIndex = 2;
        fdlg.RestoreDirectory = true;





        if (fdlg.ShowDialog() == DialogResult.OK)
        {
            MessageBox.Show("selected file is :" + fdlg.FileName);

        }

        MyExcel.Application xlApp;
        MyExcel.Workbook xlWorkBook;
        MyExcel.Worksheet xlWorkSheet;
        MyExcel.Range range;

        string cellValue;
        int rCnt;
        int cCnt;
        int rw = 0;
        int cl = 0;

        Loans.Form2 frm = new Loans.Form2();


        xlApp = new MyExcel.Application();
        xlWorkBook = xlApp.Workbooks.Open(@fdlg.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (MyExcel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        range = xlWorkSheet.UsedRange;
        rw = range.Rows.Count;
        cl = range.Columns.Count;

        //MessageBox.Show("Working");
        for (rCnt = 1; rCnt <= rw; rCnt++)
        {
            for (cCnt = 1; cCnt <= cl; cCnt++)
            {

                string str = Convert.ToString((range.Cells[rCnt, "N"] as MyExcel.Range).Value2);

                if (str == "3" || str == "4" || str == "5" || str == "6" || str == "7")
                {
                    string str1 = Convert.ToString((range.Cells[rCnt, cCnt] as MyExcel.Range).Value2);
                    MessageBox.Show(str1); //want to display the values
                                           //of str1 in a seperate form  


                }


            }
        }



        xlWorkBook.Close(true, null, null);
        xlApp.Quit();

        Marshal.ReleaseComObject(xlWorkSheet);
        Marshal.ReleaseComObject(xlWorkBook);
        Marshal.ReleaseComObject(xlApp);

    }

    public void Show(string text)
    {

        this.Show();
    }




    private void Form1_Load(object sender, EventArgs e)
    {
        InitializeComponent();
    }


}

}

2 个答案:

答案 0 :(得分:1)

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

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

希望它有所帮助。

答案 1 :(得分:-1)

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

在您的第二张表格(将收到该讯息的表格)上添加此内容

public string strVariable { get; set; }

然后在第一个表单上,在显示之前声明变量:

   Form2 frm = new Form2();
   frm.strVariable = "Hello World";
   frm.Show();

加载第二个表单(接收方)后,您可以访问数据并将其显示给用户,在本例中为标签,例如在表单加载时:

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