Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am making a touch screen keyboard i have created all the letters and stuff however the issue is I have multiple text boxes I wish to be able to type in.

Below is a segment of the code:

C#
private void btnS_Click(object sender, EventArgs e)
{
    if (capsLock == 0)
    {
        textBox1.Text += ("S");
    }
    else if (capsLock == 1)
    {
        textBox1.Text += ("s");
    }
}


Basically its only targeted at textBox1 what command would make it enter the letter in the Target textbox? (where the cursor is).
Thanks

Edit
Anyone got any help for setting a button to Focusable = false?


[Edit]unchecked "Ignore HTML in text"[/Edit]
Posted
Updated 12-Jan-11 5:12am
v3
Comments
Nuri Ismail 12-Jan-11 12:35pm    
Please have a look at my updated answer, that contains a working example for you. ;)

You can get the focused TextBox using Control.Focused[^] property. See this[^] example.

[UPDATE]
My guess is that you want to append text to the TextBox that is focused. You could write your own helper method say void AppendText(string strToAppend) that checks for the active TextBox and append the given string to it.

For example if you have 2 TextBoxes (textBox1 and textBox2) than you could write get focused TexBox method like this:
C#
private TextBox GetFocusedTextBox()
{
    if (textBox1.Focused == true)
    {
        return textBox1;
    }
    if (textBox2.Focused == true)
    {
        return textBox2;
    }

    // Here you can add more...

    // There is no focused textbox
    return null;
}


If you have lots of textboxes then you can change the GetFocusedTextBox() method to iterate through the controls on the form using the first example from the link that I gave you.
Your append text method could be something like:
C#
private void AppendText(string strToAppend)
{
    TextBox tbFocused = GetFocusedTextBox();
    if(tbFocused != null)
    {
       tbFocused.Text += strToAppend;
    }
    else
    {
       // There is no focused textbox, you should decide what to do
    }
}


This is an easy solution and you can call the AppendText method where you want to append text to the focused TextBox. I hope this helps. Feel free to post a comment in case this not enough or not clear. :)
[/UPDATE]

[UPDATE 2.1]
Sorry for late answer, my internet connection was down but I'm here with a working example for you. :)
First I will paste the code and after that will try to explain:
C#
public partial class Form1 : Form
{
    // These are members that we will use to restore
    // the selection of last selected textbox,
    // restore the caret position and selection length
    private TextBox tbSelected; // Last focused TextBox
    private int posCaret;       // Caret position
    private int selLength;      // Selection length
    public Form1()
    {
        InitializeComponent();
        // We will use leave event for textboxes
        textBox1.Leave += new System.EventHandler(textBox_Leave);
        textBox2.Leave += new System.EventHandler(textBox_Leave);
        // Set initial selection to the first textbox
        textBox1.Select();
        tbSelected = textBox1;
        posCaret = 0;
        selLength = 0;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        // Call this to restore the last selection
        RestoreLastSelection();
        // Set the selected text
        tbSelected.SelectedText = "S";
    }
    private void button2_Click(object sender, EventArgs e)
    {
        // Call this to restore the last selection
        RestoreLastSelection();
        // Set the selected text
        tbSelected.SelectedText = "FOO";
    }
    // Leave event handler
    private void textBox_Leave(object sender, EventArgs e)
    {
        // Remember the last focused thextbox,
        // the caret position in it and the selection length
        tbSelected = (TextBox)sender;
        posCaret = tbSelected.SelectionStart;
        selLength = tbSelected.SelectionLength;
    }
    // Helper method to restore selection
    private void RestoreLastSelection()
    {
        tbSelected.Select();
        posCaret = tbSelected.SelectionStart;
        selLength = tbSelected.SelectionLength;
    }
}

I've created a new project (Windows forms application) added 2 buttons and 2 textboxes. My approach is very simple. Have a look at the Control.Leave[^] event. I use this event to register the last focused textbox, caret position in it and the length of the selected text. After a button click occurs I restore the selection in the textbox and then replace the selected text. This way you can add text to cursor position and/or replace selected text. Examine the source above. I believe that this approach is easy to understand and use. Give it a try and let me know if there are some problems. :)
[/UPDATE 2.1]
 
Share this answer
 
v6
Comments
Espen Harlinn 12-Jan-11 9:50am    
5+ It's a good guess at what the OP needed :)
WurmInfinity 12-Jan-11 9:55am    
Ok thank you for that, thats given me some direction however, i've tried if(textBox1.Focused == true) and also if(textBox1.Focused) and neither work, am I missing something? (sorry im new thanks for the help :))
Nuri Ismail 12-Jan-11 10:11am    
@WurmInfinity: See my update. :)
Nuri Ismail 12-Jan-11 10:11am    
@Espen: Thank you! :)
Estys 12-Jan-11 10:16am    
Wouldn't the textbox lose focus if someone clicks on a screen keyboard button?
You can have something like this in the Load event of the form or in the constructor of the form:
class Form1{
TextBox _activeTextBox = null;

        public Form1()
        {
            InitializeComponent();
            BindGotFocusHandler();
        }

        private void BindGotFocusHandler()
        {
            foreach (Control control in this.Controls)
            {
                if (control.Controls != null && control.Controls.Count > 0)
                {
                    BindGotFocusHandler();
                }

                control.GotFocus += new EventHandler(ControlFocussed);
            }
        }

        private void ControlFocussed(object sender, EventArgs e)
        {
            TextBox textBox;
            if ((textBox = sender as TextBox) != null)
            {
                _activeTextBox = textBox;
            }
            else
            {
                _activeTextBox = null;
            }
        }

        private void KeyBoardButtonClick(object sender, EventArgs e)
        {
            if (_activeTextBox != null)
            {
                _activeTextBox.AppendText("button character");
            }
        }
}
 
Share this answer
 
Comments
WurmInfinity 12-Jan-11 11:11am    
mmm since im new a lot of thats a bit fuzzy, however i'm pretty certain if i can work out how to set a buttons focus to false I can code it to work. Thanks for the reply tho.
I would recommend you to get the focussed control (not only textboxs) using the Enter event and then pass the focussed control to the touchscreen keyboard control. When a button in the touchscreen control is clicked you don't need to append a text to the content of the textbox, there's a better aproach. You can use SendKeys to send the appropiate key. It's like if someone were pressing the key.
Another idea: don't make a custom event handler for every button, just create a user control that inherits from Button to represent a letter (let's say TouchscreenKeyButton) and add a "KeyValue" property. Use that property to determine the key pressed. For example: TouchscreenKeyButton representing the A letter should have a 'a' value in the KeyValue property. When click event occurs, just use SendKeys to send this letter. Be careful with Capslock and shift. But in my opinion that has to be managed by the keyboard, not the key.
I hope to hear from you soon.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900