How do I pass variables to a buttons event method?

前端 未结 8 1816
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 08:29

I need to be able to pass along two objects to the method being fired when I click a button. How do I do this?

So far I\'ve been looking at creating a changed E

8条回答
  •  青春惊慌失措
    2020-12-14 09:23

    Not sure if this exists in Winforms but it does in WPF: There is a "tag" object on all controls which you can attach any object to. You could save the object that you want to pass and then in the event handler read it back out of the sender object.

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var note = (sender as FrameworkElement).Tag as Note;
        //Do something with note here
    }
    

提交回复
热议问题