Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中通过事件处理程序调用函数?_C# - Fatal编程技术网

C# 如何在c中通过事件处理程序调用函数?

C# 如何在c中通过事件处理程序调用函数?,c#,C#,我正在做一个小小的单词搜索程序。因此,当用户单击搜索按钮时,他会被告知他搜索的单词出现了多少次。为此,我有一个函数listToDict,我以前使用过它,它创建了一个字典,其中string是一个单词,int是该单词出现的次数。然后我创建了一个word_搜索方法,如下所示 public void word_search(Dictionary<string, int> myDict) { if (myDict.ContainsKey(wordSea

我正在做一个小小的单词搜索程序。因此,当用户单击搜索按钮时,他会被告知他搜索的单词出现了多少次。为此,我有一个函数listToDict,我以前使用过它,它创建了一个字典,其中string是一个单词,int是该单词出现的次数。然后我创建了一个word_搜索方法,如下所示

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 );
        }
     }
}