0

I'm using cmd to open up programs to unpack a file. In my ToolStripMenu I made a WarningToggleComboBox and a DebugToggleComboBox.

So in another class I made a function that will run the cmd and get the error and output in a RichTextBox.

Above the error I made an If statement stating that if DebugToggleComboBox = "On" then Append specific text to RichTextBox.

After the error I made an If statement stating that if WarningToggleComboBox = "Off" it will search for a specific line within the error containing a random integer and replace the warning text with an empty string.

The problem is every time I select the DebugToggleComboBox Value to "On" and I select the WarningToggleComboBox to "Off" it prints out the warning 5 times instead of replacing it with an empty string, but when the DebugToggleComboBox Value is "Off" and the WarningToggleComboBox is "Off" it will replace the warning with an empty string.

Here is my code for more understanding:

public static void RunProgram(string file, string outdirectory, RichTextBox rtfReport, ToolStripComboBox debug, ToolStripComboBox warning, bool addNewLine)
{
    Process procUnpackFile = new Process();
    ProcessStartInfo procStartInfo1 = new ProcessStartInfo();
    procStartInfo1.RedirectStandardOutput = true;
    procStartInfo1.RedirectStandardError = true;
    procStartInfo1.UseShellExecute = false;
    procStartInfo1.CreateNoWindow = true;
    AppendText(rtfReport, "Using (" + program + ") to extract (" + Path.GetFileName(file) + ")..." + Environment.NewLine, Color.Yellow, true);
    AppendText(rtfReport, "TOOL.EXE [OUTPUT]", Color.Cyan, true);
    AppendText(rtfReport, "======================================================================================================================", Color.Teal, true);
    if (debug.Text.ToString() == "On") //If Statement for DebugComboBox
    {
        AppendText(rtfReport, "#################### DEBUG LOG ####################", Color.DarkMagenta, true);
        AppendText(rtfReport, "Command Prompt Input: (" + program + " " + file + " " + outdirectory + ")", Color.Magenta, true);
        AppendText(rtfReport, "###################################################", Color.DarkMagenta, true);
    }
    procStartInfo1.FileName = "cmd";
    procStartInfo1.WorkingDirectory = tooldir;
    procStartInfo1.Arguments = "/c " + program + " " + file + " " + outdirectory;
    procUnpackFile.StartInfo = procStartInfo1;
    try
    {
        procUnpackFile.Start();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        if (!procUnpackFile.HasExited)
        {
            procUnpackFile.Kill();
        }
    }
    if (procUnpackFile.Responding)
    {
        string output = procUnpackFile.StandardOutput.ReadToEnd();
        string error = procUnpackFile.StandardError.ReadToEnd();
        procUnpackFile.WaitForExit();

        if (warning.Text.ToString() == "Off") //If Statement for WarningComboBox
        {
            for (int i = 0; i < 200000; i++)
            {
                if (error.Contains("0 [main] " + program + " " + i))
                {
                    error.Replace("      0 [main] " + program + " " + i + " find_fast_cwd: WARNING: Couldn't compute FAST_CWD Pointer. Please report this problem to the public mailing list cygwin@cygwin.com", "");
                    AppendText(rtfReport, error, Color.Red, true);
                }
            }
        }
        else
        {
            AppendText(rtfReport, error, Color.Red, true);
        }
        AppendText(rtfReport, output, Color.White, true);
        AppendText(rtfReport, "======================================================================================================================", Color.Teal, true);
    }
    else
    {
        if (!procUnpackFile.HasExited)
        {
            procUnpackFile.Kill();
        }
    }
}
  • Could you verify using breakpoints/console logging that when `debug.Text` is "On" and `warning.Text` is "Off" it even passes the second if statement? Then you can re-check if its a problem with your `error.Replace`, from its [MSDN](https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx) it does NOT modify `error`, if you want to change the error, try `error = error.Replace(" 0 [ma...`. – Keyur PATEL Jun 23 '17 at 02:28
  • The error is a string which is given the ability to replace. It should be in MSDN because when using it, the function is used like this [string.Replace(string-to-find, string-to-replace)], and I just got done using your code to see if it would make a difference and now no matter what the warning or error prints 5 times in the RichTextBox. Having it the other way replaced the string but only when the DebugComboBox value is "Off". – DeathlyPlague Jun 23 '17 at 02:48
  • Well nevermind I tried switching the code back to normal and no matter what I do the error prints 5 times. – DeathlyPlague Jun 23 '17 at 02:54
  • I am currently looking into `warning.Text.ToString()`, as maybe it is not updating the value as you think, from [here](https://stackoverflow.com/questions/21552789/combobox-text-vs-combobox-selecteditem-vs-combobox-selectvalue) – Keyur PATEL Jun 23 '17 at 02:56
  • Could you try using `if (debug.SelectedItem != null && debug.SelectedItem.ToString() == "On")` and ``if (warning.SelectedItem != null && warning.SelectedItem.ToString() == "On")``. – Keyur PATEL Jun 23 '17 at 03:01
  • I did change the property (DropDownStyle) of the ComboBox in the ToolStripMenu to DropDownList. So what would be the best way using SelectedItem or SelectedValue? – DeathlyPlague Jun 23 '17 at 03:04
  • Depending on whether the options are simply strings, [this answer](https://stackoverflow.com/a/315307/6741868) suggests you can just use `SelectedItem.ToString()` since only whole text can be selected when you specify `DropDownList`. – Keyur PATEL Jun 23 '17 at 03:11
  • Thank you Keyur PATEL it worked perfectly using your method. I understand I should stay away from using .Text for ComboBoxes when using an if statement. I was going to do Selected Item for my next go, and you already thought of it before I did. :P – DeathlyPlague Jun 23 '17 at 03:13

2 Answers2

0

Thanks to Keyur PATEL for helping me fix my if statements for the ComboBoxes

if(debug.SelectedItem.ToString() == "On") if(warning.SelectedItem.ToString() == "Off")

0

Based on these links:

Combobox.Text Vs combobox.Selecteditem Vs combobox.selectValue? [closed]

Get the combobox text in C#

And since you're using DropDownList for the DropDownStyle of your ComboBoxes, the best way to get the selected text is in fact .SelectedItem.ToString() and not .Text.

So your if statements would look like:

if(debug.SelectedItem.ToString() == "On")

and

if(warning.SelectedItem.ToString() == "Off")
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41