首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# ToolStripComboBox if语句无效吗?

C# ToolStripComboBox if语句无效吗?
EN

Stack Overflow用户
提问于 2017-06-23 02:07:36
回答 2查看 86关注 0票数 0

我使用cmd打开程序来解压缩文件。在我的ToolStripMenu中,我做了一个WarningToggleComboBox和一个DebugToggleComboBox。

因此,在另一个类中,我创建了一个函数,它将运行cmd并在RichTextBox中获取错误和输出。

在错误上面,我做了一个If语句,声明如果DebugToggleComboBox = "On“,那么将特定的文本附加到RichTextBox中。

错误发生后,我做了一个If语句,声明如果WarningToggleComboBox = "Off“,它将在错误中搜索包含随机整数的特定行,并将警告文本替换为空字符串。

问题是,每次我选择DebugToggleComboBox值为"On“,选择WarningToggleComboBox "Off”时,它打印警告5次,而不是用空字符串替换警告,但是当DebugToggleComboBox值"Off“而WarningToggleComboBox为"Off”时,它将将警告替换为空字符串。

下面是我的代码,以获得更多的理解:

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-23 03:31:28

基于这些链接:

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

Get the combobox text in C#

而且,由于您将DropDownList用于ComboBoxes的DropDownStyle,因此获取所选文本的最佳方法实际上是.SelectedItem.ToString()而不是.Text

因此,您的if语句看起来如下:

代码语言:javascript
复制
if(debug.SelectedItem.ToString() == "On")

代码语言:javascript
复制
if(warning.SelectedItem.ToString() == "Off")
票数 0
EN

Stack Overflow用户

发布于 2017-06-23 03:21:23

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44711967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档