How to delete text between two texts in listbox

Sep 8 2019 8:46 AM
Hi For my application, I need a code that searches for Listbox text that has the beginning and end characters
code for open text to listbox
  1. Stream myStream = null;  
  2. OpenFileDialog openFileDialog1 = new OpenFileDialog();  
  3. openFileDialog1.InitialDirectory = "c:\\";  
  4. openFileDialog1.Filter = "txt files (*.php)|*.php|All files (*.*)|*.*";  
  5. openFileDialog1.FilterIndex = 2;  
  6. openFileDialog1.RestoreDirectory = true;  
  7. if (openFileDialog1.ShowDialog() == DialogResult.OK) {  
  8.  try {  
  9.   if ((myStream = openFileDialog1.OpenFile()) != null) {  
  10.    using(myStream) {  
  11.     textBox1.Text = File.ReadAllText(openFileDialog1.FileName);  
  12.    }  
  13.   }  
  14.  } catch (Exception ex) {  
  15.   MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);  
  16.  }  
  17. }  
  18. // button 2  
  19.   
  20.   
  21. for (int n = listBox1.Items.Count - 1; n >= 0; --n) {  
  22.  string s = listBox1.Items[n].ToString(); {  
  23.   int start = n.IndexOf("<-");  
  24.   int end = n.IndexOf("->"); {  
  25.    listBox1.Items.Remove(s);  
Listbox will have text
 
for example
 
text text text
 
<-- text to be deleted
text to be deleted
text to be deleted ->
 
text text text
 
the program will delete everything written in <- -> and so it will stay
 
text text text
 
text text text
 
thank you

Answers (3)