Home > database >  c# writing text to different selected listboxes and refresh
c# writing text to different selected listboxes and refresh

Time:09-13

I am pretty new to c# and have small task to do. At the moment I have code which reads directories and add.ini files to different listboxes depending on statement in it (this part seems to work perfectly for me). Now then I select item in my listbox2 and press button it write specific word in my selected .ini file. (this part seems to work as well), but here comes my problem... I want to write other word to .ini file with same button from listbox1. I cant wrap my head around how to make it with same button. I figure my problem is somewhere here. Also maybe you know how to update my listboxes after I change statement in .ini files? Thanks

        private void button1_Click(object sender, EventArgs e)
    {
        var items = listBox2.SelectedItems;
        //var items1 = listBox1.SelectedItems;
        foreach (var item in items)
        { 
            string fileName = listBox2.GetItemText(item);
            string text = File.ReadAllText(fileName);

My entire code

namespace WindowsFormsApp5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

    private void Form1_Load(object sender, EventArgs e)
    {
        string rootdir = @"C:\Users\isaced1\Desktop\test";
        string[] files = Directory.GetFiles(rootdir, "*.ini", SearchOption.AllDirectories);

        foreach (string item in files)
        {
            string fileContents = File.ReadAllText(item);
            const string PATTERN = @"OTPM              = true";
            Match match = Regex.Match(fileContents, PATTERN, RegexOptions.IgnoreCase);
            if (match.Success)
            {
                listBox1.Items.Add(item);
                listBox1.ForeColor = Color.Green;
            }
            else
            {
                listBox2.Items.Add(item);
                listBox2.ForeColor = Color.Red;
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //listBox2.SelectedItem = listBox1.SelectedItem;
        var items = listBox2.SelectedItems;
        //var items1 = listBox1.SelectedItems;
        foreach (var item in items)
        { 
            string fileName = listBox2.GetItemText(item);
            string text = File.ReadAllText(fileName);
            const string PATTERN = @"OTPM              = (?<Number>[false] )";
            Match match = Regex.Match(text, PATTERN, RegexOptions.IgnoreCase);
            string otpmT = "true";
            string otpmF = "false";
             if (match.Success)
                {
                    int index = match.Groups["Number"].Index;
                    int length = match.Groups["Number"].Length;

                    text = text.Remove(index, length);
                    text = text.Insert(index, otpmT.ToString());

                    File.WriteAllText(fileName, text);
                    Process.Start(fileName);
                    
                }
             else
                {
                    int index = match.Groups["Number"].Index;
                    int length = match.Groups["Number"].Length;

                    text = text.Remove(index, length);
                    text = text.Insert(index, otpmF.ToString());

                    File.WriteAllText(fileName, text);
                    Process.Start(fileName);
                }
        }
    }
}

CodePudding user response:

If i understand correctly, you just have to do the same thing again but for your other listbox :

private void button1_Click(object sender, EventArgs e)
{
    // ------------------------------ Listbox2
    var items = listBox2.SelectedItems;        
    foreach (var item in items)
    { 
        string fileName = listBox2.GetItemText(item);
        string text = File.ReadAllText(fileName);
        const string PATTERN = @"OTPM              = (?<Number>[false] )";
        Match match = Regex.Match(text, PATTERN, RegexOptions.IgnoreCase);
        string otpmT = "true";
        string otpmF = "false";
        if (match.Success)
        {
            int index = match.Groups["Number"].Index;
            int length = match.Groups["Number"].Length;

            text = text.Remove(index, length);
            text = text.Insert(index, otpmT.ToString());

            File.WriteAllText(fileName, text);
            Process.Start(fileName);
                
        }
         else
        {
            int index = match.Groups["Number"].Index;
            int length = match.Groups["Number"].Length;

            text = text.Remove(index, length);
            text = text.Insert(index, otpmF.ToString());

            File.WriteAllText(fileName, text);
            Process.Start(fileName);
        }
    }

    // ------------------------------ Listbox1
    var items1 = listBox1.SelectedItems;        
    foreach (var item in items1)
    { 
        string fileName = listBox1.GetItemText(item);
        string text = File.ReadAllText(fileName);
        const string PATTERN = @"OTPM              = (?<Number>[false] )";
        Match match = Regex.Match(text, PATTERN, RegexOptions.IgnoreCase);
        string otpmT = "true";
        string otpmF = "false";
        if (match.Success)
        {
            int index = match.Groups["Number"].Index;
            int length = match.Groups["Number"].Length;

            text = text.Remove(index, length);
            text = text.Insert(index, otpmT.ToString());

            File.WriteAllText(fileName, text);
            Process.Start(fileName);
                
        }
         else
        {
            int index = match.Groups["Number"].Index;
            int length = match.Groups["Number"].Length;

            text = text.Remove(index, length);
            text = text.Insert(index, otpmF.ToString());

            File.WriteAllText(fileName, text);
            Process.Start(fileName);
        }
    }

}

In this code I duplicate the code for listbox2, you just have to change what to check / what to write.

When the button is clicked both listboxes are proceed.

  •  Tags:  
  • c#
  • Related