【问题标题】:c# winforms reading combobox items one by onec# winforms 一个一个地读取组合框项目
【发布时间】:2018-10-16 07:39:11
【问题描述】:

让我简要介绍一下我的工作。我正在开发 Winform。

我想做什么:我想选择一个文件,并根据我在 combobox 中的选定值从中提取特定段落。

我做了什么:

private void ExtrctBtn_Click(object sender, EventArgs e)
    {
        //I have another button to select file
        string sourceFile = "", resultFile = "";

        if (sourceFile == null || !(File.Exists(sourceFile)))
        {
            MessageBox.Show("Please select a file to continue", "File Error");
        }
        else
        {
            sw = Stopwatch.StartNew();      //start the timer
            ExtrctBtn.Enabled = false;

            resultFile = Path.Combine(Path.GetDirectoryName(sourceFile), "Results_" + DateTime.Now.ToString("yyMMddHHmmss") + ".txt");
            WriteReport(resultFile);
            sw.Stop();              //stop the timer
        }

private void WriteReport(string dest)
    {
        try
        {
            int n = 0;
            string key = "";
            string[] keys = new string[10];

            Found:
            key = CmboBox.SelectedItem.ToString();
            if (!keys.Contains(key))
            {
                //copy the required data from source file to result file

                if (n < keys.Length)
                    keys[n++] = key;

                DialogResult dialogResult = MessageBox.Show("Select next key", "Continue?", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    goto Found;
                }
                else //if (dialogResult == DialogResult.No)
                {
                    goto Finish;
                }
            }
            else
            {
                MessageBox.Show("You have already added this Key","Error");
            }

            Finish:
            SaveFile();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString(), "Exception");
        }
   }

一切正常。 我无法做的是,在我从组合框中选择第一项并完成复制粘贴操作后,我无法选择下一项。它只是转到 Found 并抛出错误消息框。我不想自动选择组合框项目。相反,我希望它等到用户从组合框中选择另一个键并根据键选择提取下一段

我确实意识到我错过了一些东西。但我不知道是什么! 当我从消息框中单击“是”时,我应该怎么做才能让用户选择其他组合框项目?我可以使用线程概念并使用 Sleep() 等待几秒钟供用户输入,但我认为它不可行。

有人有其他想法吗?任何帮助表示赞赏。提前致谢。

【问题讨论】:

  • 非常重要:不要使用goto:它只会让你的代码很难阅读和维护。
  • 您永远不会填充keys,因此不包含将始终为真。
  • 你知道你可以把你的整个对象放在组合框中,对吧?它只需要一个 ToString 函数。然后你只需将SelectedItem 转换回原来的类型就可以了。
  • @Richard:我用if (n &lt; keys.Length) keys[n++] = key;填充它
  • @Nyerguds:是的,我知道。我有另一个功能可以将项目添加到组合框并且工作正常。没问题。

标签: c# winforms combobox


【解决方案1】:

(考虑到您的代码)为此,您必须在阅读后更改所选项目:

key = CmboBox.SelectedItem.ToString();
if(CmboBox.SelectedIndex < ComboBox.Items.Length -1)
      CmboBox.SelectedIndex;

但是,使用CmboBox.SelectedItem 并一一更改以获取其项目并不是一个好主意。您可以遍历所有项目。一个简单的 for 循环将是最好的选择,尽管可能还有其他方法,如 Linq,...

for(int i=0; i<ComboBox.Items.Length;i++)
{
     key = ComboBox.Items[i].ToString();
     //rest of your code
}

更新考虑您的评论:

DialogResult current = DialogResult.No;
do
{
    key = CmboBox.SelectedItem.ToString();
    DialogResult dialogResult = MessageBox.Show("Select next key", "Continue?", MessageBoxButtons.YesNo);
    if (!keys.Contains(key))
    {
         if (n < keys.Length)
              keys[n++] = key;
    }
    else
    {
        MessageBox.Show("You have already added this Key","Error");
    }
}while(dialogResult == DialogResult.Yes);

【讨论】:

  • 但我不想自动选择它。我希望用户从组合框中选择项目。
  • 是的,但不是自动的。我在查询正文中提到了它
  • 我突出显示了它。
  • 如何在 if 循环之外使用 dialogResult?它会抛出错误。您的意思是指派current = dialogResult
  • 无论哪种方式,它都会陷入无限循环并抛出消息:您已经添加了此密钥
猜你喜欢
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多