Hi, ive generated files from a text file to a checklist box. now i want to delete some of it, example i have anna on the first check box and when i check it and click on the delete command the name anna will be removed from the checklist box together with its name on the text file. help pls. thanks.

Hi,

This is how to delete it from the CheckBoxList :

private void buttonDeleteItem_Click(object sender, EventArgs e)
 {
     for (int i = 0; i < this.checkedListBox1.CheckedItems.Count; i++ )
     { 
       this.checkedListBox1.Items.Remove(this.checkedListBox1.CheckedItems[i]);

      string modified = theStringFromWhereYouLoadTheTextBox.Replace(this.checkedListBox.CheckedItems[i].ToString(), string.Empty );//And then Create a new file with same name that the original from modified
               
       }
  }

the replace part depends on how your text file is formatted.

Regards,
Camilo

hi i have this code in generating the data from the text to the checklist box

public void frmView_Load(object sender, EventArgs e)
        {
            string line;

            using (StreamReader reader = File.OpenText(@"C:\sample.txt"))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    checkbox.Items.Add(line);
                }
            }

but i cant find the line variable in my class string modified here in the example

private void buttonDeleteItem_Click(object sender, EventArgs e)
 {
     for (int i = 0; i < this.checkedListBox1.CheckedItems.Count; i++ )
     { 
       this.checkedListBox1.Items.Remove(this.checkedListBox1.CheckedItems[i]);

      string modified = theStringFromWhereYouLoadTheTextBox.Replace(this.checkedListBox.CheckedItems[i].ToString(), string.Empty );//And then Create a new file with same name that the original from modified
               
       }
  }

still i cant save the items in the check list box to the txt file thanks.

This article has been dead for over six months. Start a new discussion instead.