简体   繁体   中英

how to make keyboard in windows application

I need keyboard functionality in windows form application I am using

private void btnW_Click(object sender, EventArgs e)
{
    txtCategory.Text += btnW.Text;      
}

在此处输入图片说明

But I need it for multiple textbox like if focused on textbox1 it will add text in textbox1 an if TextBox2 focused keyboard button will effect on TextBox2 only.

like real keyboard functionality in .Net 3.5 version

seen in screen shoots

First find the focussed textbox, then set the text in that box:

private void btnW_Click(object sender, EventArgs e)
{
    Func<Control, TextBox> FindFocusedTextBox(Control control)
    {
        var container = this as IContainerControl;
        while (container != null)
        {
            control = container.ActiveControl;
            container = control as IContainerControl;
        }
        return control as TextBox;
    }

    var focussedTextBox = FindFocusedTextBox(this);
    if(focussedTextBox != null)
        focussedTextBox.Text += btnW.Text;
}

Footnote: Finding the focus was derived from: What is the preferred way to find focused control in WinForms app?

I would like to suggest you an option. Hope that it will help you

Declare a global TextBoxObject in the page and assign the textbox that is currently focused to this object. there will be an event handler let it be btnW_Click for all buttons. then you can add text to the focused textbox. See the code for this:

TextBox TextBoxObject; // this will be global

// Event handler for all button
private void btnW_Click(object sender, EventArgs e)
{
   if(TextBoxObject!=null)
   {
      TextBoxObject.Text += btnW.Text;   // This will add the character at the end of the current text
      // if you want to Add at the current position means use like this
        int currentIndex = TextBoxObject.SelectionStart;
      TextBoxObject.Text = TextBoxObject.Text.Insert(currentIndex, btnW.Text);
   }
}

You have to assign focus to the text box by using following code:

private void textBox2_Click(object sender, EventArgs e)
{
    TextBoxObject = textBox1;   
}

Try this:

object obj;
private void btnW_Click(object sender, EventArgs e)
{
    if (obj != null)
    {
        (obj as TextBox).Text += btnW.Text;
    }
}
private void txtCategory_Click(object sender, EventArgs e)
{
    obj = txtCategory;
}
private void textBox1_Click(object sender, EventArgs e)
{
    obj = textBox1;
}

Try this:

It will helpful for you.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StackOverflow
{    
public partial class Form2 : Form
{
    TextBox txtName; 
    public Form2()
    {
        InitializeComponent();
    }

    private void textBox1_Click(object sender, EventArgs e)
    {
        txtName = textBox1;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (txtName != null)
        {
            txtName.Text += button1.Text;                                
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        if (txtName != null)
        {
            txtName.Text += button2.Text;             
        }
    }
    private void textBox2_Click(object sender, EventArgs e)
    {
        txtName = textBox2;
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM