当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to pass data from form1 to form2

问题描述:

I have a form1 which contains datagridview and the button1(this form load the file which contains data on it). I then created a form2 to use it as a wizard to make it more usable.form2 contains button2 and textbox2.(button2 browse the file and textbox will populate the directory). when the user clicks button1, it will open up form2 and you browse the file and click finish button after browsing the file.

I want to the file you selected from form2 to load to be loaded on the form1. I don't have an idea on how I can do it.

was trying to do this, am just not sure how I can do. Thanks.

 private List<form2> _Listcontrols;

private string _InputText;

private DataGridView _Gridview;

网友答案:

use a new class for that like this:

//project name
namespace data
{
    //class name
    static class data
    {
      //boolean     
      public static Boolean publicbool = false;
      //int      
      public static Int number = 9;
      //list
      public static List<string> printitems = new List<string>();
    }
}

and you would call it like this: data.publicbool = true; to set the bool to true.

To create a new class ,

  • go to the solution explorer
  • right click your project name
  • add
  • new item
  • class
  • give the class a name

once you done that it will look something like this

//project name
namespace data
{
    //class name
    static class data
    {

    }
}

Then you put your variables in the static class, in your case the variables would be like this.

//project name
namespace data
{
    //class name
    static class data
    {
      public static List<form2> _Listcontrols;
      public static string _InputText;
      public static DataGridView _Gridview;
    }
}

NOTE: this could be placed in the program class thats made along with the project if you simply place it above the summary, but I do not recommend that since a new class is just as simple and will allow you to give it a meaningfull name and make it more clear if you have a lot of variables

分享给朋友:
您可能感兴趣的文章:
随机阅读: