2014-02-05 35 views
-2

「給出下面的代碼,我將如何訪問Form2中的listBox1?我敢肯定我錯過了愚蠢!先謝謝了。」從另一個表格訪問表單數據

錯誤1 'WindowsFormsApplication1.Form2.listBox1' 不可訪問由於 其保護 水平C:\用戶\ dugaj0 \桌面\發展\ GlobalUser \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs中24 19 WindowsFormsApplication1

using System; 
using System.IO; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

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

     private void button1_Click(object sender, EventArgs e) 
     { 
      String value1 = File.ReadAllText(textBox1.Text); 
      foreach (string line in value1.Split('\n')); 
      Form2.listBox1.Items.Add(); 
     } 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form2 : Form 
    { 
     public Form2() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 
    } 
} 

namespace WindowsFormsApplication1 
{ 
    partial class Form2 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.listBox1 = new System.Windows.Forms.ListBox(); 
      this.button1 = new System.Windows.Forms.Button(); 
      this.SuspendLayout(); 
      // 
      // listBox1 
      // 
      this.listBox1.FormattingEnabled = true; 
      this.listBox1.Location = new System.Drawing.Point(13, 13); 
      this.listBox1.Name = "listBox1"; 
      this.listBox1.Size = new System.Drawing.Size(259, 212); 
      this.listBox1.TabIndex = 0; 
      // 
      // button1 
      // 
      this.button1.Location = new System.Drawing.Point(105, 231); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(75, 23); 
      this.button1.TabIndex = 1; 
      this.button1.Text = "Exit"; 
      this.button1.UseVisualStyleBackColor = true; 
      this.button1.Click += new System.EventHandler(this.button1_Click); 
      // 
      // Form2 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(284, 262); 
      this.Controls.Add(this.button1); 
      this.Controls.Add(this.listBox1); 
      this.Name = "Form2"; 
      this.Text = "Form2"; 
      this.ResumeLayout(false); 

     } 

     #endregion 

     private System.Windows.Forms.ListBox listBox1; 
     private System.Windows.Forms.Button button1; 
    } 
} 
+1

你甚至搜索在谷歌你的錯誤信息? –

+0

將listBox1的訪問器更改爲'public' –

+0

是的。我做了,它說要將listBox1更改爲Public。但是,一旦我這樣做,我得到 - >錯誤非靜態字段,方法或屬性'WindowsFormsApplication1.Form2.listBox1' – user3254596

回答

-1

首先您必須創建Form2的實例。

namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
    private Form2 form2; 

    public Form1() 
    { 
     InitializeComponent(); 
     form2 = new Form2(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     String value1 = File.ReadAllText(textBox1.Text); 
     foreach (string line in value1.Split('\n')) 
     { 
      form2.listBox1.Items.Add(line); 
     } 
    } 
} 

}

您的特定錯誤是因爲listBox1中正在私人的。將其更改爲公開,您可以訪問它。

public System.Windows.Forms.ListBox listBox1; 

而且既然你已經有了:

using System.Windows.Forms; 

你可以寫

public ListBox listBox1; 
+0

謝謝。我在Form2中將ListBox的ListBox1從Private改爲Public,但是在Form1(我試圖訪問它)中,我得到了「非靜態字段,方法或屬性'WindowsFormsApplication1.Form2需要Object引用。 listBox1' – user3254596

+0

檢查我的更新,你需要創建一個Form2的實例 – ohlmar

+0

好吧...我創建了form2的實例,並且我能夠從form1填充我的列表框,感謝您的幫助! – user3254596

相關問題