【问题标题】:how to make keyboard in windows application如何在windows应用程序中制作键盘
【发布时间】:2016-09-09 07:14:54
【问题描述】:

我需要在我正在使用的 Windows 窗体应用程序中使用键盘功能

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

但我需要多个textbox ,例如如果专注于textbox1,它将在textbox1 中添加text,如果TextBox2 专注于键盘button 只会影响TextBox2

像真正的键盘功能 在.Net 3.5 版本中

在屏幕截图中看到

【问题讨论】:

标签: c# winforms keyboard .net-3.5


【解决方案1】:

试试这个:

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;
}

【讨论】:

  • 这很完美,但抱歉这么晚了
【解决方案2】:

首先找到焦点所在的文本框,然后在该框中设置文本:

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;
}

脚注:寻找焦点来源于:What is the preferred way to find focused control in WinForms app?

【讨论】:

    【解决方案3】:

    我想向您推荐一个选项。希望对你有帮助

    在页面中声明一个全局TextBoxObject,并将当前聚焦的文本框分配给这个对象。将有一个事件处理程序,让它成为所有按钮的btnW_Click。然后您可以将文本添加到焦点文本框中。参见代码:

    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);
       }
    }
    

    您必须使用以下代码将焦点分配给文本框:

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

    【讨论】:

      【解决方案4】:

      试试这个:

      对你有帮助。

      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;
          }
      }
      }
      

      【讨论】:

      • @zubair Ahmad 请查看更新后的编码 :))))
      猜你喜欢
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多