2012-04-24 49 views
0

我使用dosnt的API响应Form_Load事件。所以我想用包含在我用来调用包含CheckedlistBox1的对话框的按钮中的代码填充CheckedListBox1。这是我第一次尝试。从前一个对话框填充CheckedBoxList1

private void button3_Click(object sender, EventArgs e) 
    { 
     TextSelectorForm textSelectionForm = new TextSelectorForm(); 

     CheckedListBox checkedListBox1; 

     string line; 
     StreamReader file = new StreamReader("test.txt"); 
     while ((line = file.ReadLine()) != null) 
     { 
      TextSelectorForm.checkedListBox1.Items.Add(line); 
     } 
     file.Close(); 

     textSelectionForm.Show(); 
    } 

想法,想法,例子?谢谢!


我收到错误“对象引用未设置为对象的实例”。我在慢慢学习。这是我的代码。

public partial class Form1 : System.Windows.Forms.Form 
{ 
    public Form1(ExternalCommandData commandData) 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     CheckedListBox.ObjectCollection data = null; 

     string line; 
     StreamReader file = new StreamReader(@"C:\test.txt"); 

     while ((line = file.ReadLine()) != null) 
     { 
      data.Add(line); 
     } 

     file.Close(); 

     Form2 form2 = new Form2(data); 
     form2.Show(); 
    } 
} 

    public partial class Form2 : System.Windows.Forms.Form 
{ 
    public Form2(CheckedListBox.ObjectCollection formdata) 
    { 
     InitializeComponent(); 

     if (formdata != null) 
     { 
      this.checkedListBox1.Items.AddRange(formdata); 
     } 
    } 
} 

(PS。如果我想要添加到我的问题?)

回答

0

我不会说英语。我正在与Google翻译员打交道。

如果我理解你的问题,你要设定的以下几点:1。 从文本文件中恢复数据来填充CheckedListBox 2.恢复的数据发送到一个表单,将显示即可。

我建议如下: 1.创建一个ListBox.ObjectCollection类型的对象,存储您需要的信息。 2.以ListBox.ObjectCollection接受的形式创建一个构造函数作为参数。 3.在表单的构造函数中,将该参数分配给ListBox。

//CONSTRUCTOR IN TEXTSELECTORFORM 
public TextSelectorForm(ListBox.ObjectCollection dataFromOtherForm) { 
    InitializeComponents(); 
    //Add this code after InitializeComponents(); 
    if (dataFromOtherForm != null) { 
     this.listBoxInThisForm.AddRange(dataFromOtherForm); 
    } 
} 


//CODE FOR BUTTON IN OTHER FORM 
private void button3_Click(object sender, EventArgs e) { 
    //Stores the values ​​to display in the ListBox 
    ListBox.ObjectCollection data = null; 

    //Your code from retrieve data 
    string line; 
    StreamReader file = new StreamReader("test.txt"); 
    while ((line = file.ReadLine()) != null) { 
     data.Add(line); 
    } 
    file.Close(); 

    //Form to send the data 
    TextSelectorForm textSelectionForm = new TextSelectorForm(data); 
    textSelectionForm.Show(); 
} 

我希望能回答你的问题。

+0

我收到错误“Object reference not set to a instance of a object”。我在慢慢学习。这是我的代码。 – topofsteel 2012-04-24 15:37:49

+0

非常感谢你! – topofsteel 2012-04-24 19:06:24

0

对不起,没有测试过的代码。

确实启动NullReference是因为我没有创建类的新实例(立即赋值为空值),因此Add方法失败。

使用ListBox.ObjectCollection不是解决这个问题的正确方法,请问我的歉意。这种情况最好使用通用集合List。重写代码:

public partial class Form1 : System.Windows.Forms.Form { 
    public Form1(ExternalCommandData commandData) { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) { 
     List<string> data = new List<string>(); 

     string line; 
     StreamReader file = new StreamReader(@"C:\test.txt"); 

     while ((line = file.ReadLine()) != null) { 
      data.Add(line); 
     } 

     file.Close(); 

     Form2 form2 = new Form2(data); 
     form2.Show(); 
    } 
} 

public partial class Form2 : System.Windows.Forms.Form { 
    public Form2(List<string> formdata) { 
     InitializeComponent(); 

     if (formdata != null) { 
      this.checkedListBox1.Items.AddRange(formdata.ToArray()); 
     } 
    } 
}