C# 方法调用以显示在c中单击的每个按钮的名称#

C# 方法调用以显示在c中单击的每个按钮的名称#,c#,C#,我想通过调用一个方法来显示我单击的每个按钮的文本 private void btn_Click(object sender, EventArgs e) { Button button = (Button)sender; disply(); } private int disply() { MessageBox.Show(??????); return 0; } 我在c#完全是个新手。你的方法

我想通过调用一个方法来显示我单击的每个按钮的文本

private void btn_Click(object sender, EventArgs e)
    {
        Button button = (Button)sender;
        disply();
    }
    private int disply()
    {
        MessageBox.Show(??????);
        return 0;
    }

我在c#完全是个新手。

你的方法
显示
没有任何关于
按钮的线索。您需要将其作为参数传递

private int disply(Button button)
{
    MessageBox.Show(button.Text);
    return 0;
}
然后像这样称呼它:

private void btn_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    disply(button);
}

您的方法
display
没有关于
按钮的任何线索。您需要将其作为参数传递

private int disply(Button button)
{
    MessageBox.Show(button.Text);
    return 0;
}
然后像这样称呼它:

private void btn_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    disply(button);
}

您不需要
display
方法,您可以在
单击事件中执行此操作:

private void btn_Click(object sender, EventArgs e)
{
    Button button = sender as Button;

    if(button != null)
         MessageBox.Show(button.Text);
}

您不需要
display
方法,您可以在
单击事件中执行此操作:

private void btn_Click(object sender, EventArgs e)
{
    Button button = sender as Button;

    if(button != null)
         MessageBox.Show(button.Text);
}

如果您想显示内容,可以通过传递按钮(内容)的文本来完成,如果您想显示名称,可以通过pasing名称来完成:

private void btn_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    displayName(button.Name);
    displayText(button.Text);
}
private int displayName(string name)
{
    MessageBox.Show(name);
    return 0;
}
private int displayText(string text)
{
    MessageBox.Show(text);
    return 0;
}

如果您想显示内容,可以通过传递按钮(内容)的文本来完成,如果您想显示名称,可以通过pasing名称来完成:

private void btn_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    displayName(button.Name);
    displayText(button.Text);
}
private int displayName(string name)
{
    MessageBox.Show(name);
    return 0;
}
private int displayText(string text)
{
    MessageBox.Show(text);
    return 0;
}

这是一些好建议。谢谢我是一名学生,对此我有太多的问题。有点新鲜。这是一些很好的建议。谢谢我是一名学生,对此我有太多的问题。有点新鲜。