0

T 我在尝试让我的代码的语音合成类从 ListBoxItem.toString() 中背诵单词时遇到了很多麻烦。btnStart_Clicked() 方法中的 foreach 循环是我认为问题开始的地方:

test = testLstSet.lstWordlist.Items.ToString();
speech.SpeakAsync(test);

语音合成器告诉我:

“system.windows.forms.listbox + object.collections”

有人可以帮忙吗?对不起所有的代码,但我想给你尽可能多的信息。

我究竟做错了什么?

public partial class MyClass : Form
{
    private bool testStarted = false;
    private SpeechSynthesizer speech;
    private string evalWord = null;
    string test;
    bool testBit = false;

    TestList testLstSet;

    public SpellingBee(TestList tstLst)
    {
        InitializeComponent();
        testLstSet = tstLst;

        speech = new SpeechSynthesizer();
        speech.SpeakAsync("Hello! Welcome to The test. Shall we begin?");
    }

    private void btnStart_Click(object sender, EventArgs e)
    {

        if (testStarted)
            return;
        else
        {
            testStarted = true;
            foreach(var item in wrdLstSet.lstWordlist.Items)
            {
                test = testLstSet.lstWordlist.Items.ToString();
                speech.SpeakAsync(test);
                while(!testBit)
                {
                }     
            }
        }           
    }

    private void btnSubmit_Click(object sender, EventArgs e)
    {
        string evalWord = this.txtAnswer.Text;
        bool answer = string.Equals(evalWord, test, StringComparison.OrdinalIgnoreCase);
        if (answer)
        {
            speech.SpeakAsync("That's right! Good Job!");
            testbit = true;
        }
        else
        {
            speech.SpeakAsync("That is incorrect.");
            testbit = true;
        }         
    }

}       

}

4

1 回答 1

2

我不太了解您想要实现的目标,但我认为您需要部分代码来实现

foreach(var item in testLstSet.lstWordlist.Items)
        {
            speech.SpeakAsync(item.ToString());
        }

当您致电时testLstSet.lstWordlist.Items.ToString();-您将获得 Items 的类型,即object.collections. 如果你想获取这个集合的元素,你应该像这样使用索引器:testLstSet.lstWordlist.Items[0].ToString();

于 2013-07-06T22:00:14.737 回答