簡體   English   中英

如何在Windows應用程序中制作鍵盤

[英]how to make keyboard in windows application

我需要使用Windows窗體應用程序中的鍵盤功能

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

在此處輸入圖片說明

但是我需要多個textbox例如,如果將焦點放在textbox1 ,它將在textbox1添加text ;如果以TextBox2焦點的鍵盤button將僅在TextBox2起作用。

如.Net 3.5版本中的真實鍵盤功能

在屏幕截圖中看到

首先找到重點突出的文本框,然后在該框中設置文本:

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

腳注:尋找焦點的來源是: 在WinForms應用程序中尋找焦點控件的首選方法是什么?

我想建議您一個選擇。 希望對您有幫助

在頁面中聲明一個全局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;   
}

嘗試這個:

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

嘗試這個:

這將對您有所幫助。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM