2010-04-20 78 views
0

我在c#中创建一个多形式的应用程序。我将用户的值存储在表单1中的列表中,我想访问表单2中相同列表的相同存储值...我无法访问存储的值...错误I' m到处表明,有列表中没有价值,我正在从形式2访问...请帮我...访问表单元素

public Form1() 
{ 
    InitializeComponent(); 
} 

public List<string> sub = new List<string>(); 
public int clickcounter = 1; 

public void additems() 
{ 
    sub.Add("Java"); 
    sub.Add("Web Technology"); 
    sub.Add("Software Engineering"); 
    sub.Add("Networks"); 
    sub.Add("ADO.net"); 
} 

public void show() 
{ 
    int x = 10; 
    int y = 10; 
    int m = sub.Count; 

    for (int i = 0; i < m; i++) 
    { 
     string name = "txtBox_" + (i + 1).ToString("00"); 
     TextBox txt = new TextBox(); 
     txt.Name = name; 
     this.Controls.Add(txt); 
     txt.Text = sub[i]; 
     txt.ReadOnly = true; 
     y += 20; 
     txt.Location = new Point(x, y); 
     txt.Width = 120; 
    } 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    if (clickcounter == 1) 
    { 
     additems(); 
     show(); 
    } 
} 
+1

你能分享一些代码吗? – rahul 2010-04-20 04:32:41

+0

public Form1() { InitializeComponent(); } public list sub = new List (); public int clickcounter = 1; public void additems() { sub.Add(“Java”); sub.Add(“Web Technology”); sub.Add(“软件工程”); sub.Add(“Networks”); sub.Add(“ADO.net”); } – Shiv 2010-04-20 04:40:15

+0

public void show() { int x = 10; int y = 10; int m = sub.Count; (int i = 0; i Shiv 2010-04-20 04:41:48

回答

0

不要访问从Form2的存储在Form1的对象,但通该对象作为Form2的一个属性。

例如: 定义属性:

public partial class Form2 : Form 
    { 
     public List<String> PersonNames { get; set; } 

     public Form2() 
     { 
      InitializeComponent(); 
     } 
    } 

从Form1中传递对象到窗体2:

private void button1_Click(object sender, EventArgs e) 
{ 
     List<String> PersonNames = new List<String>() { "Harald", "Thomas", "Markus" }; 
     ObjektBinaerSerialisieren form2 = new ObjektBinaerSerialisieren(); 
     form2.PersonNames = PersonNames; 
} 
0

添加本地列表变量为你的窗体2和一个构造函数,像

Public List<String> locaList; 
public form2(List aList) 
{ 
    InitializeComponent(); 
    localList = aList; 
} 

然后只是通过您的名单当您创建您的表单2.

希望帮助。