C# 如何在c中通过事件处理程序调用函数?
我正在做一个小小的单词搜索程序。因此,当用户单击搜索按钮时,他会被告知他搜索的单词出现了多少次。为此,我有一个函数listToDict,我以前使用过它,它创建了一个字典,其中string是一个单词,int是该单词出现的次数。然后我创建了一个word_搜索方法,如下所示C# 如何在c中通过事件处理程序调用函数?,c#,C#,我正在做一个小小的单词搜索程序。因此,当用户单击搜索按钮时,他会被告知他搜索的单词出现了多少次。为此,我有一个函数listToDict,我以前使用过它,它创建了一个字典,其中string是一个单词,int是该单词出现的次数。然后我创建了一个word_搜索方法,如下所示 public void word_search(Dictionary<string, int> myDict) { if (myDict.ContainsKey(wordSea
public void word_search(Dictionary<string, int> myDict)
{
if (myDict.ContainsKey(wordSearch.Text))
{
int value = myDict[wordSearch.Text];
wordSearchResult.Text = "Number of occurrences: " + value;
}
else
{
wordSearchResult.Text = "Cannot find this word sorry";
}
}
最简单的方法是将occurrenceDict设置为字段或属性,以便用于搜索值:
public YourClass
{
Dictionary<string, int> MyDictionary {get; set;}
public void wordSearchButton_Click(object sender, EventArgs e)
{
if(MyDictionary != null )
{
word_search(MyDictionary );
}
}
}
您需要声明一个可以存储字典的。
public YourClass
{
Dictionary<string, int> MyDictionary {get; set;}
public void wordSearchButton_Click(object sender, EventArgs e)
{
if(MyDictionary != null )
{
word_search(MyDictionary );
}
}
}