具有独特文本的不同按钮,1 次点击功能。我如何让它工作以减少我的代码?

Different buttons with unique text, 1 clickfunction. How do I make it work to reduce my code?

我有两个带有不同文本的不同按钮,我想使用一个单击函数并从他们单击的那个特定按钮中获取 info/text。

到目前为止,这是我的代码:

String myString; //creating a string so I can store the texts on the same string.

myString = Button1.Text; 
myString = Button2.Text;  

Button1.Clicked += OnButtonClicked;
Button2.Clicked += OnButtonClicked;

void OnButtonClicked (object s, EventArgs e)
    {   
        myString = s as TextCell; //I want that the myString to now get the value depending on the clicked button. I am trying to do something with "text".
    }

该对象很可能是被单击的按钮,因此您需要将发送者对象转换为 Button 并提取该按钮的文本

void OnButtonClicked(object s, EventArgs e)
{
  Button btn=(Button)s;
  myString = btn.Text;
}