Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to design a keypad in C#?

How to design a keypad in C#?

Scheduled Pinned Locked Moved C#
tutorialquestioncsharpdesignhelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    naouf10
    wrote on last edited by
    #1

    I am new to C# and I am planing to design my own keypad but I don't know how/where to start, I have 4 textBoxes the keypad buttons. The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?). So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing. Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in. Thank you professionals.

    B L G 3 Replies Last reply
    0
    • N naouf10

      I am new to C# and I am planing to design my own keypad but I don't know how/where to start, I have 4 textBoxes the keypad buttons. The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?). So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing. Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in. Thank you professionals.

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      edit #1 All you need to do to keep the Focus on the TextBox Control when a Button is clicked is to set the TextBox 'Capture property to 'true in the Click EventHandler. Example:

      private void button1_Click(object sender, EventArgs e)
      {
      CurrentTextBox.Text += "A";
      CurrentTextBox.Capture = true;
      }

      See below for how to keep the 'CurrentTextBox field up to date as the user changes which TextBox they use. end edit #1 The word "keypad" suggests to me a bunch of Buttons that you click on, and things happen. I am not clear what you are doing with the TextBoxes here; please clarify. In WinForms the 'ActiveControl property of the ContainerControl Class will always return which Control has Focus in the ContainerControl (which could be Form, a Panel, etc.) [^]. Control activeFormControl = this.ActiveControl; // of the Form Control activePanelControl = panel1.ActiveControl; // of a Panel A way I often use to keep track of which of several TextBoxes has Focus currently, or has had Focus most recently is: wire all of them up to the same 'Enter EventHandler, and:

      TextBox CurrentTextBox;

      private void TextBoxes1To4_Enter(object sender, EventArgs e)
      {
      CurrentTextBox = sender as TextBox;

      // remove when testing is complete
      if(CurrentTextBox == null) 
      {
          throw new ArgumentNullException("Illegal use TextBox Enter");
      }
      
      // example of code that will do something when TextBox is entered
      switch (CurrentTextBox.Name)
      {
          case "textBox1":
              // do something specific to textBox1 getting Focus, being Acive
              // example:
              textBox1.Text = "Entered";
              break;
          case "textBox2":
              break;
          case "textBox3":
              break;
          case "textBox4":
              break;
      }
      

      }

      «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

      1 Reply Last reply
      0
      • N naouf10

        I am new to C# and I am planing to design my own keypad but I don't know how/where to start, I have 4 textBoxes the keypad buttons. The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?). So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing. Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in. Thank you professionals.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Clicking the button will cause the cursor to leave any textbox it is in. You can determine which textbox the cursor is in (or "was" in) by hooking up the "Leave" event for each textbox and storing the "sender" (i.e. textbox) for each event in a common field / property. (Note that all the textboxes can use the same event handler). e.g.

        private TextBox _tb = null;

        this.textBox1.Leave += new System.EventHandler(this.textBox_Leave);
        this.textBox2.Leave += new System.EventHandler(this.textBox_Leave);
        this.textBox3.Leave += new System.EventHandler(this.textBox_Leave);

        private void textBox_Leave( object sender, EventArgs e ) {
        _tb = sender as TextBox;
        }

        private void button1_Click( object sender, EventArgs e ) {
        if ( _tb != null ) {
        MessageBox.Show( "The last TextBox was: " + _tb.Name );
        }
        }

        (The "Enter" event can be used to record where the cursor currently is as long as there is no other UI activity that will move the cursor; i.e. a button click).

        1 Reply Last reply
        0
        • N naouf10

          I am new to C# and I am planing to design my own keypad but I don't know how/where to start, I have 4 textBoxes the keypad buttons. The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?). So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing. Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in. Thank you professionals.

          G Offline
          G Offline
          GrooverFromHolland
          wrote on last edited by
          #4

          Hi If You are new to C# it wont be easy. First of all You can not use buttons, because when You click a button that button will steal focus, so your key stroke will have no notion where to go. The only (winforms)control that not steals focus is a label, but a label has not got the right properties to act as a key. The solution is to create a custom control derived from label control. to do so You must add this class to your project:

          namespace YOURPROJECTNAMESPACE
          {
          public sealed class MyControl : Label
          {
          public MyControl()
          {
          // Allow transparent background
          SetStyle(ControlStyles.SupportsTransparentBackColor, true);
          // Default to transparent background
          BackColor = Color.Transparent;
          }

              public Image ControlImage { get; set; }
          
              protected override void OnPaint(PaintEventArgs e)
              {
                  // Draw image, sized to control
                  if (ControlImage != null)
                  {
                      e.Graphics.DrawImage(ControlImage, 4, 4, Width - 8, Height - 8);
                  }
                  // Draw text, centered vertically and horizontally
                  var rectangle = new RectangleF(2, 0, Width, Height + 5);
                  var format = new StringFormat
                  {
                      LineAlignment = StringAlignment.Center,
                      Alignment = StringAlignment.Center
                  };
                  e.Graphics.DrawString(Text, Font, Brushes.Black, rectangle, format);
              }
          }
          

          }

          After you have done this You will find in the designer toolbox MyControl. Then drag a flowLayouPanel from the toolbox. Populate this panel by dragging as many MyControls as you need. in properties give them a nice picture of a key and a character as text. One more thing to do, In the designer doubleclick each key,and in the click event this:

          private void myControl1_Click(object sender, EventArgs e)
          {
          SendKeys.Send("{1}");
          }

          private void myControlEnter_Click(object sender, EventArgs e)
          {
          SendKeys.Send("{ENTER}");
          }

          Etc. If You have any more questions just ask, Groover

          0200 A9 23 0202 8D 01 80 0205 00

          B 1 Reply Last reply
          0
          • G GrooverFromHolland

            Hi If You are new to C# it wont be easy. First of all You can not use buttons, because when You click a button that button will steal focus, so your key stroke will have no notion where to go. The only (winforms)control that not steals focus is a label, but a label has not got the right properties to act as a key. The solution is to create a custom control derived from label control. to do so You must add this class to your project:

            namespace YOURPROJECTNAMESPACE
            {
            public sealed class MyControl : Label
            {
            public MyControl()
            {
            // Allow transparent background
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            // Default to transparent background
            BackColor = Color.Transparent;
            }

                public Image ControlImage { get; set; }
            
                protected override void OnPaint(PaintEventArgs e)
                {
                    // Draw image, sized to control
                    if (ControlImage != null)
                    {
                        e.Graphics.DrawImage(ControlImage, 4, 4, Width - 8, Height - 8);
                    }
                    // Draw text, centered vertically and horizontally
                    var rectangle = new RectangleF(2, 0, Width, Height + 5);
                    var format = new StringFormat
                    {
                        LineAlignment = StringAlignment.Center,
                        Alignment = StringAlignment.Center
                    };
                    e.Graphics.DrawString(Text, Font, Brushes.Black, rectangle, format);
                }
            }
            

            }

            After you have done this You will find in the designer toolbox MyControl. Then drag a flowLayouPanel from the toolbox. Populate this panel by dragging as many MyControls as you need. in properties give them a nice picture of a key and a character as text. One more thing to do, In the designer doubleclick each key,and in the click event this:

            private void myControl1_Click(object sender, EventArgs e)
            {
            SendKeys.Send("{1}");
            }

            private void myControlEnter_Click(object sender, EventArgs e)
            {
            SendKeys.Send("{ENTER}");
            }

            Etc. If You have any more questions just ask, Groover

            0200 A9 23 0202 8D 01 80 0205 00

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            Well, it can be easy: all you need to do to keep the Focus on the TextBox Control when a Button is clicked is to set the TextBox 'Capture to 'true in the Click EventHandler.

            «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups