none
how to delete a special line of text file that selected from listbox??? RRS feed

  • Question

  • hello everyone  :)

    I opened a text file in the listbox , now i can see the contents of the text file in list box

    now i want to know how can i do to delete a special line of text file that selected from listbox???

    thank you for your guidance

    Saturday, January 27, 2018 12:50 PM

Answers

  • Based on your question we can guess for 2 different solutions

            string TextFile = "WhateverText";
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                TextFile = TextFile.Replace(listBox1.SelectedItem.ToString(), "");
            }

            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string TextFile = System.IO.File.ReadAllText(listBox1.SelectedItem.ToString());
                TextFile = TextFile.Replace("WhatEver", "WhatElse";
            }


    Success Cor

    • Marked as answer by ashkan4818 Saturday, January 27, 2018 2:01 PM
    Saturday, January 27, 2018 1:38 PM
  • This method might be of interest also. The BindingSource allows a item to be removed then either the item above or below becomes the current item.

    using System;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace DeleteTextFileLine
    {
    
        public partial class Form1 : Form
        {
            private string fileName = 
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt");
    
            private BindingSource bs = new BindingSource();
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                bs.DataSource = File.ReadAllLines(fileName).ToList();
                button1.Enabled = bs.Count > 0;
                listBox1.DataSource = bs;
                listBox1.DisplayMember = "LineText";
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                File.WriteAllLines(fileName, File.ReadAllLines(fileName)
                    .Select((line) => line).Where(line => line != ((string)bs.Current))
                    .Select(item => item).ToArray());
    
                bs.RemoveCurrent();
                button1.Enabled = bs.Count > 0;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ashkan4818 Tuesday, January 30, 2018 8:39 AM
    Saturday, January 27, 2018 2:44 PM
    Moderator

All replies

  • Based on your question we can guess for 2 different solutions

            string TextFile = "WhateverText";
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                TextFile = TextFile.Replace(listBox1.SelectedItem.ToString(), "");
            }

            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string TextFile = System.IO.File.ReadAllText(listBox1.SelectedItem.ToString());
                TextFile = TextFile.Replace("WhatEver", "WhatElse";
            }


    Success Cor

    • Marked as answer by ashkan4818 Saturday, January 27, 2018 2:01 PM
    Saturday, January 27, 2018 1:38 PM
  • This method might be of interest also. The BindingSource allows a item to be removed then either the item above or below becomes the current item.

    using System;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace DeleteTextFileLine
    {
    
        public partial class Form1 : Form
        {
            private string fileName = 
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt");
    
            private BindingSource bs = new BindingSource();
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                bs.DataSource = File.ReadAllLines(fileName).ToList();
                button1.Enabled = bs.Count > 0;
                listBox1.DataSource = bs;
                listBox1.DisplayMember = "LineText";
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                File.WriteAllLines(fileName, File.ReadAllLines(fileName)
                    .Select((line) => line).Where(line => line != ((string)bs.Current))
                    .Select(item => item).ToArray());
    
                bs.RemoveCurrent();
                button1.Enabled = bs.Count > 0;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ashkan4818 Tuesday, January 30, 2018 8:39 AM
    Saturday, January 27, 2018 2:44 PM
    Moderator