Resolved C# listbox update

Katonas

Member
Joined
Sep 6, 2022
Messages
6
Programming Experience
Beginner
Hello
I have code which search in directories for specific .ini files and depending on its content adds it to listbox 1 or listbox2. I also have button who can change content in .ini files I selected in my listbox1 or listbox2. But once I change content in .ini file it should move between my listboxes, basicaly I sort .ini files by true or false statement and then i change that statement I want it to move. I tried listox1.update(); or listbox1.refresh() comands. it doesnt work for me.
my code


C#:
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)
        {
            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";

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

            }
            var items1 = listBox1.SelectedItems;
            foreach (var item in items1)
            {
                string fileName = listBox1.GetItemText(item);
                string text = File.ReadAllText(fileName);
                const string PATTERN = @"OTPM              = (?<Number>[true]+)";
                Match match = Regex.Match(text, PATTERN, RegexOptions.IgnoreCase);
                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, otpmF.ToString());

                    File.WriteAllText(fileName, text);
                    Process.Start(fileName);
                }
            }
        }
 
Of course Update and Refresh are useless. They simply redraw the controls on the screen. That has nothing to do with the data in the controls.

If you expect an item to move from one ListBox to the other then you need to remove the item from the first list and then add it to the second. I don't see any code to remove or add items in that Click event handler so of course nothing is going to move.
 
Of course Update and Refresh are useless. They simply redraw the controls on the screen. That has nothing to do with the data in the controls.

If you expect an item to move from one ListBox to the other then you need to remove the item from the first list and then add it to the second. I don't see any code to remove or add items in that Click event handler so of course nothing is going to move.
I see, i just started doing some work with coding and c# last week so I have a lot of dumb questions which i dont find on internet. Thanks I will try do as you suggested.
 
Some other possible points of attention:
1) Have you verified Regex.Match() is always returning the expected result ? In other words, that your pattern is correct ?
2) You have two blocks of code in your click trigger which are identical except for the listbox they use and the pattern to match. By all means put that code in a function with listbox and pattern as parameters, and call that function twice from your trigger. Avoid redundancy and reduce maintenance.
3) The Process.Start() statement launches an editor for each matching file, returning instantly. You end up with a bunch of new windows on your screen. Is that what you want ?
4) Instead of text.Remove() and then text.Insert(), I would do text.Replace(). It might be more efficient, and even if not, it better reflects what you are doing.
 
Some other possible points of attention:
1) Have you verified Regex.Match() is always returning the expected result ? In other words, that your pattern is correct ?
2) You have two blocks of code in your click trigger which are identical except for the listbox they use and the pattern to match. By all means put that code in a function with listbox and pattern as parameters, and call that function twice from your trigger. Avoid redundancy and reduce maintenance.
3) The Process.Start() statement launches an editor for each matching file, returning instantly. You end up with a bunch of new windows on your screen. Is that what you want ?
4) Instead of text.Remove() and then text.Insert(), I would do text.Replace(). It might be more efficient, and even if not, it better reflects what you are doing.
3. it was only to check if my code works and if it changes specific word. Already removed it.
4. thanks ill try it.
2. il try to do it, as i said i am very new to coding so for now i do thing as i understand, thanks for advice.
1. yes
 
3. it was only to check if my code works and if it changes specific word. Already removed it.
You should consider Console.WriteLine() for such checks.
2. il try to do it, as i said i am very new to coding so for now i do thing as i understand, thanks for advice.
I understand that copying and pasting code is an attractive idea to the novice programmer. But it's almost never a good idea, because you are likely to change something in one copy and forget (or do something different) in the other. As soon as it's more than a couple of lines, write a function for it.
 
Back
Top Bottom