Simple Keypad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fishercraigj
    New Member
    • Jan 2010
    • 16

    Simple Keypad

    Once again, I'm still very new but learning a ton through online tutorials using c sharp in visual studio 2008. I'm trying to build a calculator but I don't need help with the math as you might think. I'm looking forward to the challenge of figuring that out myself.

    What I need help with will probably seem easy to you guys. I have a text box being used as a display and a keypad with all number 0-9 on them. I want to make the keys interactive with the display. If I hit "2" then "0" then "6" I want the display to show "206".

    I'm sure it's totally easy but for some reason it's escaping me. How do I do this? Is there some sample code that someone can show me to learn from? :)

    Thanks!
    Craig
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    A textbox has a property of .Text
    It contains (as a string) the text that is in the textbox.
    You can get and set this text.

    Code:
    String RightNow = myTextBox.Text;
    myTextBox.Text = "5";
    strings are additive. You can
    s
    Code:
    tring One = "1";
    string Two = "2"
    string Both = One + Two; // Both now equals "12"
    So for your calculator display you could do something as simple as getting the value currently displayed, and tack onto the end of it the key that was just pressed.

    Code:
    myDisplayTextbox.Text = myDisplayTextbox.Text + "5";
    Code to the right side of an = sign is evaluated then stored in the object on the left side. So the compiler will first get the value of the text, concatenate "5" to the end of it, then store it in... the display textbox.

    There are lots of good explanations and code samples on the MSDN website.

    The easiest way to find them (to me) is to google "MSDN" plus what you are looking for... "MSDN Textbox" .... "MSDN button.click" .... and so on

    Comment

    • fishercraigj
      New Member
      • Jan 2010
      • 16

      #3
      You rock! I don't know how I missed that. I tried EVERYTHING except:

      myDisplayTextbo x.Text = myDisplayTextbo x.Text + "5";

      -Craig

      PS - This site is amazing!

      Comment

      • fishercraigj
        New Member
        • Jan 2010
        • 16

        #4
        PS - If there is a quick and easy way of programming the keys to work on multiple text boxes then I'd love to see it. A Point of Sale system would be a good example where you would use ONE keypad to enter numbers in multiple text boxes. I have a feeling it's some setting that can tell what text box is currently active with the program running and then enters text there.

        I haven't done any research on it yet so no worries if not. :)

        Thanks again,
        Craig

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          It has nothing to do with which one is "active." ("Has focus" would be a more accurate term, btw).

          You put it in the second or third or fourth or nth the same way you put it in the first:
          myDisplayTextbo x.Text = myDisplayTextbo x.Text + "5";
          myDisplayTextbo x2.Text = myDisplayTextbo x.Text + "5";

          Comment

          • fishercraigj
            New Member
            • Jan 2010
            • 16

            #6
            hmmm....do i need to lump all the text boxes in a variable and add some sort of "has focus" property to them? I've never done something like that so i don't even know if that's possible. Just thinking out loud.

            IE:

            displayBoxes.Te xt = displayBoxes.so mething-something-that-has-focus.Text + "5"; //????

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              I'm not sure I understand your question, and I think I may have confused you more.

              If your question is how to you append the same string to two different text boxes, then you can do it like this:
              Code:
              string textToAppend = "5";
              textBox1.Text = textBox1.Text + textToAppend;
              textBox2.Text = textBox2.Text + textToAppend;
              I think I need some clarification from you...when you say "keys" and "keypad" do you mean physical keys on the keyboard? Or Buttons on the form? There's ways to handle both, but I need to know which you mean.

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                I think I would go a different route.
                Try to use a property to hold the current displayed value.
                Clicking one of the 10key buttons makes a change... the set method then updates all the boxes.

                Code:
                namespace POScalc
                {
                    public partial class Form1 : Form
                    {
                        public Form1()
                        {
                            InitializeComponent();
                        }
                
                        string DisplayText
                        {
                            get
                            {
                                return textBox1.Text;
                            }
                            set
                            {
                                textBox2.Text = textBox1.Text = value;
                            }
                        }
                
                        private void btn1_Click(object sender, EventArgs e)
                        {
                            DisplayText += "1";
                        }
                
                        private void btn2_Click(object sender, EventArgs e)
                        {
                            DisplayText += "2";
                        }
                    }
                }
                Now you have a single central variable (DisplayText) that automagically changes all the textboxes it is meant to have control over.

                If you recalculate sales tax for example, then changing the one string changes all of its boxes, and so on.

                Comment

                • fishercraigj
                  New Member
                  • Jan 2010
                  • 16

                  #9
                  The keypad i'm refering to are buttons that I created on the form. :)

                  Comment

                  • Curtis Rutland
                    Recognized Expert Specialist
                    • Apr 2008
                    • 3264

                    #10
                    Yeah, tlhintoq, that's the direction I would have worked towards, but I wasn't sure where we were starting at.

                    Actually, I'd simplify it a bit further. All of the number button's OnClick event's would be bound to a single event handler, then I would use (sender as Button).Text to get which button was pressed.

                    Comment

                    • fishercraigj
                      New Member
                      • Jan 2010
                      • 16

                      #11
                      I think that I'm getting ahead of where my skill level is. I can't help it, I love c sharp!

                      Comment

                      • Curtis Rutland
                        Recognized Expert Specialist
                        • Apr 2008
                        • 3264

                        #12
                        That's part of the problem here. I would suggest you find a good book or online tutorial dealing with Windows Forms applications, and all of these questions will disappear. Work through it and learn each concept on the way, and don't try to get too far ahead of yourself.

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          Craig you have inspired me.
                          I'm going to use this idea (a basic cash register) to build an "insights" article. How to build a little program from the ground up, making use of properties, form to form talking, custom events etc. I'll let you know when it is posted

                          Comment

                          • fishercraigj
                            New Member
                            • Jan 2010
                            • 16

                            #14
                            I would absolutely love to see something like that!!! I make a lot of tutorials for beginners using the Adobe CS4 Suite and have got a lot of good feedback. I've been teaching a few of the classes that I've been a student in and the professor asked me to make up a bunch of these tutorials. You can see an example at www.craigfisherlive.com/tutorials.html. The site is still being built so there isn't much up there and probably a few broken links.

                            Are you going to do it through VS2008? Reason I ask is, a lot of the tutorials put out there (even the beginning ones) just dump a ton of code onto the screen and say do this. As a beginner, we don't really understand all the extra code that we are seeing or how to "plug" everything in. It's escpecially difficult to figure out since VS2008 does a lot of the code work for you, IE: the designer. That being said, I still try to figure it out.

                            Basically what I'm saying is...if you had a step-by-step tutorial geared towards my level of c sharp, it would inspire a lot of beginning programers like myself and give them "ideas" of how they could construct/modify the program to their specific needs.

                            -Craig

                            Comment

                            • tlhintoq
                              Recognized Expert Specialist
                              • Mar 2008
                              • 3532

                              #15
                              Here is the start on it


                              Yes it is going to be based on VS08 and filled with screenshots.

                              Comment

                              Working...