2

I am new to C# and I am planing to design my own keypad but I don't know how/where to start. as shown in photo, 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.

Photo

naouf
  • 627
  • 2
  • 18
  • 28
  • 1
    But once he clicks the button the focus will no longer be on the textbox, no? – ragerory Nov 12 '15 at 20:37
  • Create a variable such as `int lastSelectedTextBox = 0;`, when a user clicks/focuses/selects a text box change the value accordingly. Now when a user presses a button in the dial pad `switch` on the variable and alter the text of the corresponding text box. – KDecker Nov 12 '15 at 20:38
  • You really should not reimplement such basic functionality as keyboard. Try invoking a built-in virtual keyboard. Like the one here: http://stackoverflow.com/questions/8800212/calling-the-on-screen-keyboard-using-a-button-in-c-sharp – Agent_L Nov 12 '15 at 21:03
  • @Agent_L OP said he was new, so he's testing the waters. – ragerory Nov 12 '15 at 21:05
  • If you really want to build the keyboard yourself, then `Button` is wrong choice, as it will take focus away from the textbox. You should look for some low level controls that won't take focus at all. – Agent_L Nov 12 '15 at 21:11
  • @ragerory Exactly, and crashing into the first big rock on the reef is not a nice start. – Agent_L Nov 12 '15 at 21:13
  • @Agent_L - I think we all thought he meant focus, but look at the rest of the comments and answers. Essentially he wants the last clicked textbox. There's nothing wrong with a simple form with buttons and textboxes to see how you can interact with them. Don't focus so much on the fact that it's a keyboard. – ragerory Nov 12 '15 at 21:14
  • Agent_L . I know how to use built-in virtual keyboard and it is not me who uses ready stuff and as i said I want to build my own keypad, yes it looks very basic but i am doing it for a reason... I will design a full complete keyboard and this simple keypad is the way to start. – naouf Nov 12 '15 at 21:51

5 Answers5

3

Firstly, have a textbox that represents the one that is selected (outside of subroutines but inside the class):

TextBox SelectedTextBox = null;

And then make the "Click" event of each TextBox look like this:

private void textBoxNUM_Click(object sender, EventArgs e)
{
    SelectedTextBox = sender as TextBox;
}

And then make the "Click" event of each Button look like this:

private void buttonNUM_Click(object sender, EventArgs e)
{
    if (SelectedTextBox != null)
    {
        SelectedTextBox.Text = buttonNUM.Text;//Or set it to the actual value, whatever.
    }
}

Or if that one doesn't work, this should.

private void buttonNUM_Click(object sender, EventArgs e)
{
    if (SelectedTextBox != null)
    {
        (SelectedTextBox as TextBox).Text = buttonNUM.Text;//Or set it to the actual value, whatever.
    }
}
Sophie Coyne
  • 1,018
  • 7
  • 16
  • 1
    You should add NULL checks in these calls. If the sender coming in is not a TextBox, it will throw NullReferenceExceptions. – JimmyV Nov 12 '15 at 20:53
  • This is the correct answer. Also, make sure you add the handler to the `InitializeComponent` method in the designer as well. Example: `this.textBox2.Click += new System.EventHandler(this.textBox2_Click);` – ragerory Nov 12 '15 at 21:00
  • Thank you for your answer. do you know how to set focus the cursor on textBox1 whenever the form is loaded? thank you – naouf Nov 12 '15 at 22:13
  • In the form's "load" event (or the constructor) put "textBox1.Select();" – Sophie Coyne Nov 12 '15 at 22:16
  • 1
    most of the internet says use "textbox.focus();" i tried it but it didnt work so yours worked well. thank you alot – naouf Nov 12 '15 at 22:28
  • ao now I have new error when I added the textBox1.Select(). The form loads fine and when I click on button1 (to print "1" on textbox1) it shows error. however when i click on textbox1 it works well with no error. i think there someting to do with " private void textBox1_Click(object sender, EventArgs e)" because it has "SelectedTextBox = sender as TextBox;" any idea? – naouf Nov 12 '15 at 22:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94985/discussion-between-naouf-and-rariolu). – naouf Nov 12 '15 at 22:45
  • If you want textBox1 to be the default, change "TextBox SelectedTextBox = null;" to "TextBox SelectedTextBox = textBox1 as TextBox;" – Sophie Coyne Nov 12 '15 at 22:46
0

To check if a textbox is focused you can do

if(textbox1.Focused)
{
  //Print the value of the button to textbox 1
}
else if (textbox2.Focused)
{
  //Print the value to textbox 2
}

UPDATE:

Since the textbox will lose focus when you click the button, you should have a temporary textbox (ie lastTextboxThatWasFocused) which is saved to everytime a textbox gains focus. Write an OnFocused Method and do something like

public void Textbox1OnFocused(/*Sender Event Args*/)
{
   lastTextboxThatWasFocused=textbox1;
}

Then on button click you can do

if(lastTextboxThatWasFocused.Equals(textbox1))
{
  //ETC.
}
Seth Kitchen
  • 1,526
  • 19
  • 53
  • The textboxes will lose focus when you click the button, additionally `Focused` is a property and not a method. Furthermore, OP just wants to detect which control the mouse is "in", not which has (or had) focus. – sab669 Nov 12 '15 at 20:37
  • @sab669 I fixed the first two things. How could he click the button and be in the textbox at the same time? – Seth Kitchen Nov 12 '15 at 20:46
0

You can give something along these lines a try. Create a generic click handler for the buttons and then assign the value to a textbox the text from the button, which happens to be the value. You can check which box was the last one focused in the TextBoxes' Click event. Create a global variable to store which one and use it in the below method.

        private TextBox SelectedTextBox { get; set; }

    private void NumericButton_Click(object sender, EventArgs e)
    {
        var clickedBox = sender as Button;

        if (clickedBox != null)
        {
            this.SelectedTextBox.Text += clickedBox.Text;
        }
    }

    private void TextBox_Click(object sender, EventArgs e)
    {
        var thisBox = sender as TextBox;

        if (thisBox == null)
        {
            return;
        }
        this.SelectedTextBox = thisBox;
    }
JimmyV
  • 493
  • 3
  • 12
0

Try this code:

TextBox LastTxtBox;

private void textBox_Enter(object sender, EventArgs e)
    {
        LastTxtBox = sender as TextBox;
    }

private void button_Click(object sender, EventArgs e)
    {
        LastTxtBox.Text = this.ActiveControl.Text;
    }

Add textBox_Enter function to all textboxes enter event.
Add button_Click to all buttons click event.

c4pricorn
  • 3,471
  • 1
  • 11
  • 12
0

Button Enter Event

    Control _activeControl;
    private void NumberPadButton_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        if (_activeControl is TextBox || _activeControl is RichTextBox)
        {
            _activeControl.Text += btn.Text;
            if (!_activeControl.Focused) _activeControl.Focus();
        }
    }

   

TextBox or RihTextBox Enter Event

    private void TextBoxEnter_Click(object sender, EventArgs e)
    {
        _activeControl = (Control)sender;
    }
Adarsh Babu PR
  • 179
  • 1
  • 5