我使用cmd打开程序来解压缩文件。在我的ToolStripMenu中,我做了一个WarningToggleComboBox和一个DebugToggleComboBox。
因此,在另一个类中,我创建了一个函数,它将运行cmd并在RichTextBox中获取错误和输出。
在错误上面,我做了一个If语句,声明如果DebugToggleComboBox = "On“,那么将特定的文本附加到RichTextBox中。
错误发生后,我做了一个If语句,声明如果WarningToggleComboBox = "Off“,它将在错误中搜索包含随机整数的特定行,并将警告文本替换为空字符串。
问题是,每次我选择DebugToggleComboBox值为"On“,选择WarningToggleComboBox "Off”时,它打印警告5次,而不是用空字符串替换警告,但是当DebugToggleComboBox值"Off“而WarningToggleComboBox为"Off”时,它将将警告替换为空字符串。
下面是我的代码,以获得更多的理解:
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();
}
}
}发布于 2017-06-23 03:31:28
基于这些链接:
Combobox.Text Vs combobox.Selecteditem Vs combobox.selectValue? [closed]
而且,由于您将DropDownList用于ComboBoxes的DropDownStyle,因此获取所选文本的最佳方法实际上是.SelectedItem.ToString()而不是.Text。
因此,您的if语句看起来如下:
if(debug.SelectedItem.ToString() == "On")和
if(warning.SelectedItem.ToString() == "Off")发布于 2017-06-23 03:21:23
感谢Keyur PATEL帮助我修复了ComboBoxes的if语句
if(debug.SelectedItem.ToString() == "On") if(warning.SelectedItem.ToString() == "Off")
https://stackoverflow.com/questions/44711967
复制相似问题