Solved

C# Sending values from XAML to code behind page

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

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
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
Comment Utility
Thank you for taking the time to answer.  That makes a lot of sense - and it works :)
I appreciate the help!
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now