![](/img/trans.png)
[英]How to automatically resize a Windows Mobile application when the keyboard appears?
[英]how to make keyboard in windows application
首先找到重点突出的文本框,然后在该框中设置文本:
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.