Solved

C# Sending values from XAML to code behind page

Posted on 2014-09-24
3
195 Views
Last Modified: 2014-09-24
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.

For example:
        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            clicked(this.btn1);
        }

        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
        }

Open in new window


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"/>

Open in new window


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!

-Steve
0
Comment
Question by:slightlyoff
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40342198
Below is some sample code.  Note I wrote it by hand so it may not compile, but shows the general intent.

You can cast the object as a sender, and that way multiple controls can be wired to the same event.

This is how I would do it:
//wire up all buttons to button_click
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            Button myButton = (Button) sender;
            //no need for clicked() function . ..  you can handle right here.

            //RULE: all numeric buttons must begin with "BUTTON" that way you can do:
            // no non-numeric buttons must start with "BUTTON"
             //EG:  BUTTON0     BUTTON1          BUTTON2  . ..  etc
     
               int number_pressed = -1; // default non-possible value.

               //if I'm a numeric button, get the button pressed
               if (myButton.Name.StartsWith("BUTTON"))
              {
                      number_pressed = int.Parse(myButton.Name.Replace("BUTTON",""));
                       myTextBox.Text += number_pressed.ToString();
               }
             else
                   //CLR
                   myTextBox.Text = "";
             

        }

Open in new window

0
 
LVL 1

Author Closing Comment

by:slightlyoff
ID: 40342215
Thank you for taking the time to answer.  That makes a lot of sense - and it works :)
I appreciate the help!
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40342233
Any time.  Thanks for taking the time to learn :-).  We've all been there, stuff can be confusing when first coming across it.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question