I'm playing around with a simple number pad program in XAML. I created a grid of buttons 0-9 (named btn1, btn2, etc), as well as a CLR and GO button.
When a button is clicked, I want the value to display in a textbox, added on to whatever number is already there.
I could create a function for each button, but that would be 12 different functions. It seems there must be a better way.
private void btn1_Click(object sender, RoutedEventArgs e)
private void clicked(Control myButton)
string btnName = myButton.Name;
MessageBox.Show("BtnName = " + btnName);
//At this point I could do a switch to determine what button was pushed and add to textbox
But shouldn't I be able to send the button directly to one function? Isn't that what "sender" does?
The thing is, I'm not sure how to get the value of the button at that point.
Also, I'm not sure how that would look in XAML. Here's what I have:
<Button x:Name="btn1" Content="1" HorizontalAlignment="Left" Margin="92,240,0,0" VerticalAlignment="Top" Width="65" Height="65" FontSize="20" Click="btn1_Click"/>
<Button x:Name="btn2" Content="2" HorizontalAlignment="Left" Margin="183,240,0,0" VerticalAlignment="Top" Width="65" Height="65" FontSize="20" Click="checkSender"/>
Btn1 works. Btn2 does not. I tried to set up checkSender as a function that would tell me the name of the button that was clicked, so that I could set Click="checkSender" for all the buttons - but I'm stuck.
Just trying to teach myself as I go - so I appreciate any help you can provide!