简体   繁体   中英

C# ToolStripComboBox if statement not working?

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

感谢Keyur PATEL帮助我修复了ComboBoxes的if语句

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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM