Solved

C# Sending values from XAML to code behind page

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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