简体   繁体   中英

How do I pass a 2D string array to a click event?

I am trying to pass a 2d string array as a parameter to a click event (searchButton_Click) which is generated within a click event so I can perform a search function. However, I get the error No overload for 'searchButton_Click' matches delegate 'RoutedEventHandler'.

I have tried several solutions as seen on stackoverflow and other sources but none has worked. I'm new to C# programming, any help will be appreciated.

private string[,] LongRunningTask()
        {
            workSheetName = getWorkSheetName();
            var sourceFile = OpenExcelFile(filePath);
            var sourceWorkSheet = GetWorkSheet(sourceFile.Item1, sourceFile.Item2, workSheetName);  //instantiating the object.
            var doc_Max = GetDocRow_Col(sourceWorkSheet.Item1);
            var maxRow_Col = GetMaxRow_Col(sourceWorkSheet.Item1, doc_Max.Item1, doc_Max.Item2);
            int maxRowCount = maxRow_Col.Item1;
            int maxColCount = maxRow_Col.Item2;
            WriteLine("Last column with content is Row {0} ", maxRowCount);
            WriteLine("Last column with content is Column {0} ", maxColCount);
            var getItemsResult = getItems(sourceWorkSheet.Item1, maxRow_Col);

            string[,] itemsArr = getItemsResult;

            Bindng2DArrayToListview2(listview, itemsArr, maxColCount);
            //enableItems();
            GarbageCollector(sourceWorkSheet.Item1, sourceWorkSheet.Item2, sourceWorkSheet.Item3, sourceWorkSheet.Item4);


            //searchButton.Click += (sender2, e2) => { searchButton_Click(sender2, e2, itemsArr); };

            return itemsArr;
        }

        private async void StartTask()
        {
            this.progressLabel.Content = "Getting Sheets..";
            try
            {
                await Task.Run(() => LongRunningTask());
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            this.progressLabel.Content = "Done";
        }

You could put the result of LongRunningTask into a field on the class (which I assume is the Window itself), then just access that field from searchButton_Click .

For example...

public partial class TheWindow : Window
{
   private string[,] LongRunningTask()
   {
      // Your LongRunningTask implementation.
   }

   private async Task StartTask()
   {
      progressLabel.Content = "Getting Sheets...";
      try
      {
         _itemsArr = await Task.Run(() => LongRunningTask());
      }
      catch (Exception e)
      {
         MessageBox.Show(e.Message);
      }
      progressLabel.Content = "Done!";
   }

   private void searchButton_Click(object sender, RoutedEventArgs e)
   {
      if (_itemsArr == null)
      {
         // The field is null for some reason. You'll have to decide what to do in this case, but
         // naturally you can't use it if it's null.
         return;
      }
      // Do the search using the field _itemsArr.
   }

   private string[,] _itemsArr = null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM