Search code examples
c#winformsdialogcheckedlistbox

Populate CheckedBoxList1 from previous dialog


The API I'm using dosnt respond to Form_Load events. So i'd like to populate the CheckedListBox1 with code included in the button I use to call the dialog containing the CheckedlistBox1. Here was my first attempt.

    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();
    }

Thoughts, Ideas, examples? Thank you!


I'm getting the error "Object reference not set to instance of an object". I'm learning, slowly. Here is my code.

    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. What if I want to add to my question?)


Solution

  • I do not speak English. I am dealing with Google translator.

    If I understand your question, you want to program the following: 1. Recover data from a text file to populate a CheckedListBox 2. Send the recovered data to a form that will show then.

    I suggest the following: 1. Create an object of type ListBox.ObjectCollection that stores information you need. 2. Create a constructor in the form ListBox.ObjectCollection accept as a parameter. 3. In the form's constructor, assign the parameter to the 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();
    }
    

    I hope to answer your question.