?
Solved

C# Sending values from XAML to code behind page

Posted on 2014-09-24
3
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 2000 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 To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

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…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

764 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