Search code examples
c#wpflistboxlistboxitem

C# WPF help in either 1. removing first two entries in listbox or 2. preventing it from displaying the command syntax from batch in output


Here is the code snippet I have that I am calling during window_loaded event in my wpf. It will execute a batch file, and output the results in listbox. However, the first entry within listbox is showing the command itself from within the batch file. I would like to remove the first entry, or if that can't be done in an efficient manner then to prevent it from displaying it in the first place.

I have tried using item count remove, but it throws an outofargument exception. My educated guess would be that despite the fact that it is trying to add items in window_loaded, it does not finish adding it until the window has actually finished loading. As such when I call the syntax to remove item from index count, it throws that error since the listbox is still empty at this point. So how can I add data while window is loading, and once it is finished adding the data, to remove the first entry that includes syntax from batch or to prevent it from displaying the first entry.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var info = new ProcessStartInfo("extractServiceList.bat") { UseShellExecute = false, RedirectStandardOutput = true };
    var proc = new Process { StartInfo = info, EnableRaisingEvents = true };

    proc.OutputDataReceived += (obj, args) =>
    {
        if (args.Data != null)
        {
            this.Dispatcher.Invoke(() =>
            {
                listBoxServices.Items.Add(args.Data);

                //below logic for removing whitespaces from listbox view
                int count = listBoxServices.Items.Count;

                for (int i = count - 1; i >= 0; i--)
                {
                    //Condition evaluating to see if it return true for = listbox items empty and
                    //looping through each entry to remove the whitespace entry
                    if (String.IsNullOrWhiteSpace(listBoxServices.Items[i].ToString()))
                    {
                        listBoxServices.Items.RemoveAt(i);
                    }
                }
                //end of logic above for removing whitespaces from listbox view
            });

            //listBoxServices.Items.RemoveAt(listBoxServices.Items.Count - 1);
        }
    };

    proc.Start();
    proc.BeginOutputReadLine();
}

https://i.sstatic.net/U1axP.png


Solution

  • Perhaps you can collect all output, check first and add to output if not whitespace. Then, in Exited event add to listbox. This is a command line example, but it should work in WPF if you replace Exited event handler.

    List<string> output = new List<string>();
    proc.OutputDataReceived += (obj, args) =>
    {
        if (args.Data != null)
        {
            if (String.IsNullOrWhiteSpace(args.Data))
                return;
            output.Add(args.Data);
        }
    };
    
    proc.Exited += (s, args) =>
    {
        foreach (var o in output)
            Console.WriteLine(o);
    }