1.

Solve : Get Button Content in RoutedEventHandler c#?

Answer»

So i'm making a calculator and would like the same event HANDLER for all the numbers.
I've already tried things like sender.source.Content but I'm UNABLE to find it
My code so far:
public MainWindow()
{
InitializeComponent();
oneButton.Click += new RoutedEventHandler(oneClick);
twoButton.Click += new RoutedEventHandler(oneClick);
threeButton.Click += new RoutedEventHandler(oneClick);
fourButton.Click += new RoutedEventHandler(oneClick);
fiveButton.Click += new RoutedEventHandler(oneClick);
sixButton.Click += new RoutedEventHandler(oneClick);
sevenButton.Click += new RoutedEventHandler(oneClick);
eightButton.Click += new RoutedEventHandler(oneClick);
nineButton.Click += new RoutedEventHandler(oneClick);
}

private void oneClick(object sender, RoutedEventArgs e)
{
resultTextBox.Text += .........;

}Is this from a published source code?
Or is it it fully original work?

From:
http://www.instructables.com/id/Creating-a-Calculator-Visual-Studio-C/
There are loss of samples out there.
https://www.youtube.com/watch?v=iJqB6UsM-hsQuick Answer: You should be able to use (String)((Button)sender).Content and test the string to see what button was pressed.

Longer answer: You can set the event directly in the WPF designer, too.

Even longer answer:

When you use WPF, you should aim to have as little C# code as possible, and do as much as possible with DataBindings and RESOURCES within the xaml.

I wrote a program that has a pinpad interface and it uses the same event handler for all buttons.

Here is the XAML FILE in question.


First it defines a "StandardEntryButtonStyle" on line 22. It defines the event handler to use for elements that will use this style:

Code: [Select]<EventSetter Event="Button.Click" Handler="EntryButtonClick"></EventSetter>
Then, later in the XAML, I use the RESOURCE to set the Style of each of the Entry buttons

Code: [Select]<Button Name="EntryButton7" Grid.Row="0" Grid.Column="0" Style="{StaticResource StandardEntryButtonStyle}" Margin="5">7</Button>
<Button Name="EntryButton8" Grid.Row="0" Grid.Column="1" Style="{StaticResource StandardEntryButtonStyle}" Margin="5">8</Button>
<Button Name="EntryButton9" Grid.Row="0" Grid.Column="2" Style="{StaticResource StandardEntryButtonStyle}" Margin="5">9</Button>
<Button Name="EntryButton4" Grid.Row="1" Grid.Column="0" Style="{StaticResource StandardEntryButtonStyle}" Margin="5">4</Button>
<Button Name="EntryButton5" Grid.Row="1" Grid.Column="1" Style="{StaticResource StandardEntryButtonStyle}" Margin="5">5</Button>
<Button Name="EntryButton6" Grid.Row="1" Grid.Column="2" Style="{StaticResource StandardEntryButtonStyle}" Margin="5">6</Button>
<Button Name="EntryButton1" Grid.Row="2" Grid.Column="0" Style="{StaticResource StandardEntryButtonStyle}" Margin="5">1</Button>
<Button Name="EntryButton2" Grid.Row="2" Grid.Column="1" Style="{StaticResource StandardEntryButtonStyle}" Margin="5">2</Button>
<Button Name="EntryButton3" Grid.Row="2" Grid.Column="2" Style="{StaticResource StandardEntryButtonStyle}" Margin="5">3</Button>
<Button Name="EntryButtonC" Grid.Row="3" Grid.Column="0" Foreground="Red" Style="{StaticResource StandardEntryButtonStyle}" Margin="3">C</Button>
<Button Name="EntryButton0" Grid.Row="3" Grid.Column="1" Style="{StaticResource StandardEntryButtonStyle}" Margin="5">0</Button>
<Button Name="EntryButtonOK" Grid.Row="3" Grid.Column="2" Foreground="Green" Style="{StaticResource StandardEntryButtonStyle}" FontFamily="WingDings" Margin="3">ü</Button>

This sets the event handler for all the buttons.

Optionally, of course, you can set the event handler for the buttons yourself by editing the XAML and setting the 'Click' attribute to point at your event method, but using a static resource allows you to make changes across all of the elements easily. The event that handles the button clicks is found here; remember this is a PIN entry, rather than a calculator, so the logic itself is definitely different than what would be used for a calculator. As you can see it uses (String)((Button)sender).Content to retrieve the text content of the button; if it is numeric it will add it to the built PIN, otherwise it will check for the wingdings font text to see if it is the Cancel or OK buttons that were pressed and act accordingly.Thanks for the resources, this was the solution:

Button button = (Button)sender;
resultTextBox.Text += button.Content;

The reason I don't like to attach the handlers in the xaml code is because when I use ValueChanged events there I get nulreferences, unless you know how to fix that (without changing the order of the Objects)Quote from: verystrengt on March 08, 2015, 05:37:57 AM

The reason I don't like to attach the handlers in the xaml code is because when I use ValueChanged events there I get nulreferences, unless you know how to fix that (without changing the order of the Objects)

I do not know what you are talking about. Since I have no idea what your code is, I cannot guess why you are getting Exceptions.


Discussion

No Comment Found