Solved

C# Sending values from XAML to code behind page

Posted on 2014-09-24
3
194 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 32
How to join on ID, with prefix? 15 60
Jquery/JSON passing value from one function to another 12 38
Hey!!!! 1 22
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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