如何以另一种形式显示结果? [英] How to display the result in another form?

查看:59
本文介绍了如何以另一种形式显示结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我尝试了什么:



使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.IO;
使用System.Linq;使用System.Reflection
;
使用System.Text;
使用System.Threading.Tasks;
使用System.Runtime.InteropServices;
使用MyExcel = Microsoft.Office.Interop.Excel;
使用System.Windows.Forms;
使用静态System.Windows.Forms.VisualStyles.VisualStyleElement;
使用Microsoft.Vbe.Interop;
使用System.Diagnostics;


名称空间贷款
{

公共部分类Form1:表格
{
public Form1()
{

InitializeComponent();
}

private void button1_Click(object sender,EventArgs e)
{




OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title =选择一个Excel文件;
fdlg.InitialDirectory = @d:\ test;
fdlg.Filter =所有文件(*。*)| *。* |所有文件(*。*)| *。*;
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;





if(fdlg.ShowDialog()== DialogResult.OK)
{
MessageBox.Show( 所选文件是:+ fdlg.FileName);

}

MyExcel.Application xlApp;
MyExcel.Workbook xlWorkBook;
MyExcel.Worksheet xlWorkSheet;
MyExcel.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]为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); //想要以单独的形式显示str1的值
//


}


}
}



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

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

}

public void显示(字符串文本)
{

this.Show();
}




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


}
}

解决方案

这取决于究竟是什么形式你想要显示它!

如果你还没有表格,那么它很容易。创建一个新表单(通过将其添加到您的项目 - 右键单击​​解决方案资源管理器窗格中的项目,然后在下拉菜单中)在设计器中为表单添加标签或文本框并为其指定一个合适的名称:theControlIWantToDisplayIn将做这个讨论。然后在表单中添加一个属性:

  public   string  DisplayThis 
{
get { return theControlIWantToDisplayIn.Text; }
set {theControlIWantToDisplayIn.Text = value ; }
}

然后,当您想要显示原始表单中的消息时:

 MyNewForm f =  new  MyNewForm(); 
f.DisplayThis = str1;
f.ShowDialog();





如果您的表单已经可见,并且您想要向其添加信息,那么它更复杂,但其中一个应该有所帮助:

在两个表格之间传递信息,第1部分:父母与子女 [ ^ ]

在两种形式之间传递信息,第2部分:儿童到父母 [ ^ ]

在两种形式之间转移信息,第3部分:儿童到儿童 [<小时ef =https://www.codeproject.com/Tips/548160/Transferring-information-between-two-forms-Part\"target =_ blanktitle =New Window> ^ ]


hi guys, I am a newbie to c# coding. I wanted to know that how can i pass the values of the string str1 to another form and display it in another windows form from the below code. Well i can display it in a messagebox as "MessageBox.Show(str1);" but i want to pass the value of str1 and display it in another form.

What I have tried:

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;


namespace Loans
{

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


    }
}

解决方案

That depends on exactly what form you want to display it in!
If you don't have a form yet, then it's pretty easy. Create a new form (by adding it to your Project - right click the Project in the Solution Explorer pane and it's in the drop down menu) Add a label or text box to your form in the designer and give it an appropriate name: theControlIWantToDisplayIn will do for this discussion. Then add a property to teh form:

public string DisplayThis
   {
   get { return theControlIWantToDisplayIn.Text; }
   set { theControlIWantToDisplayIn.Text = value; }
   }

Then, when you want to display the message from your original form:

MyNewForm f = new MyNewForm();
f.DisplayThis = str1;
f.ShowDialog();



If your form is already visible, and you want to add info to it, then it's more complex, but one of these should help:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]


这篇关于如何以另一种形式显示结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆