2014-02-23 61 views
0

在窗体1中我打开了一个xml文件。现在我想能够在Form2中使用字符串文件名,这样我就可以执行额外的功能和/或使用文件内的内容。从窗体1通过字符串文件名从窗体1到窗体2

 Form1 

       DialogResult result; 
       string filename; 

      private void openToolStripMenuItem_Click(object sender, EventArgs e) 
      { 
      using (OpenFileDialog fileChooser = new OpenFileDialog()) 
       { 
       fileChooser.Filter = "Xml Files (*.xml)|*.xml"; 
       fileChooser.CheckFileExists = false; 
       result = fileChooser.ShowDialog(); 
       filename = fileChooser.FileName; 
       } 
      } 


     Form2 
    namespace PreviewForm 
    { 
    public partial class Preview : Form 
    { 
     int ind1 = 1; 

     public Preview() 
    { 
     InitializeComponent(); 
    } 

    private void Preview_Load(object sender, EventArgs e) 
    { 

     //I want to able to do something like this 

     //XmlDocument xDoc = new XmlDocument(); 
     //xDoc.Load(filename); 

     //foreach(XmlNode xNode in xdoc.SelectNode(People/People)) 
     //{ 
     // Label lable1 = new Label(); 
     // label1.Text = xNode.SelectingSingleNode("Name").InnerText; 
     // label1.Name = "label_" + ind1; 
     // label1.Location = new Point(code); 
     // ind1++; 
     // this.Controls.Add(label1); 


    } 

    } 
} 

所以我想能够使用Form1中的字符串文件名form2。

回答

0

只需使用一个私有字段实例,并将其添加到构造

namespace PreviewForm 
    { 
    private string _filename = string.Empty; 
    public partial class Preview : Form 
    { 
     int ind1 = 1; 

     public Preview(string filename) 
    { 
     InitializeComponent(); 
      _filename = filename; 
    } 

使用的字符串的文件名在窗体2

private void openToolStripMenuItem_Click(object sender, EventArgs e) 
      { 
      using (OpenFileDialog fileChooser = new OpenFileDialog()) 
       { 
       fileChooser.Filter = "Xml Files (*.xml)|*.xml"; 
       fileChooser.CheckFileExists = false; 
       result = fileChooser.ShowDialog(); 
       filename = fileChooser.FileName; 
       //for example here 
       PreviewForm form2=new PreviewForm(filepath); 
       form2.ShowDialog(this); 
       } 

      } 
0

解决方案1:您可以从Form1发送FilePathForm2使用constructor如下:

Form2 form2=new Fomr2(filepath); 
form2.ShowDialog(); 

,并创建窗体2字符串参数的构造函数如下:

 string filepath; 

     public Preview(string filepath) 
     { 
     InitializeComponent(); 
     this.filepath=filepath; 
     } 

解决方案2:制作的文件路径变量publicstaticForm1,以便它可以使用它的类名访问。

试试这个:

 public static string filepath; 
     private void openToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
     using (OpenFileDialog fileChooser = new OpenFileDialog()) 
      { 
      fileChooser.Filter = "Xml Files (*.xml)|*.xml"; 
      fileChooser.CheckFileExists = false; 
      result = fileChooser.ShowDialog(); 
      filename = fileChooser.FileName; 
      Form2 form2=new Fomr2(filepath); 
      form2.ShowDialog(); 
      } 
     } 

从窗体2:

您可以从如下访问:

String filepath = Form1.filepath; 
+0

我不得不移动一些东西,但它工作得很好。谢谢..... – user3329960

0

一些已发布的工作的例子,但我想给你当你需要将数据从一个类传递到另一个时,可以做些什么的总体概述。

我介绍你与3层基本的方法来从第一类(发件人类)第二(接收机类)将您的数据:

  1. 构造器注入。这是大多数人在这里描述的,它基本上意味着在接收者类中有一个“特殊的”构造函数,当接收者类对象被实例化时,它将从发送者类中被调用来传递必要的数据作为参数。数据然后可以保存到接收机类的本地成员。

  2. 安装员注射。在这种情况下,您可以在发送者类中调用的接收者类上定义一个公共属性,但是在接收者已经实例化之后。与构造函数注入一样,您可以将数据保存到接收者的本地成员。

  3. 方法注射。在这种情况下,您可以在接收器类中定义一个方法,该方法将定义一个用于接收数据的参数。在这里您可以决定将数据存储到接收者的成员,但是您也可以决定直接在方法主体中使用数据,并在完成时放弃数据。

这绝不意味着如何在类之间传递数据的详尽列表,但它会给你一个很好的起点。

干杯

相关问题