111,120
社区成员
发帖
与我相关
我的任务
分享
public partial class Form1 : Form
{
int index = 0;
TextBox box = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (box != null)
{
box.Select();//激活控件
box.Select(index, 0);//定位光标
SendKeys.Send("A");//发送字符
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
}
void textBox1_LostFocus(object sender, EventArgs e)
{
index = textBox1.SelectionStart;//保存当前光标位置
box = sender as TextBox;//保存要操作的文本框
}
}
TextBox tb,,,,
tb.text+="a";
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication243
{
public partial class Form1 : Form
{
Control OldActiveControl = null;
public Form1()
{
InitializeComponent();
Button B = new Button();
B.Parent = this;
B.Text = "插入";
B.MouseEnter += new EventHandler(B_MouseEnter);
B.Click += new EventHandler(B_Click);
for (int i = 0; i < 5; i++)
{
TextBox TB = new TextBox();
TB.Parent = this;
TB.Text = "123";
TB.Location = new Point(0, (i + 1) * 30);
}
}
void B_MouseEnter(object sender, EventArgs e)
{
OldActiveControl = ActiveControl;
}
void B_Click(object sender, EventArgs e)
{
if (OldActiveControl == null || !(OldActiveControl is TextBox))
return;
TextBox TB = (TextBox)OldActiveControl;
TB.Text = TB.Text.Substring(0, TB.SelectionStart) + "a"
+ TB.Text.Substring(TB.SelectionStart, TB.TextLength - TB.SelectionStart);
TB.Focus();
}
}
}
int index = 0;//保存TextBox光标所在的位置
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//按下按钮时,激活Box控件
textBox1.Select();
//恢复光标所在位置
textBox1.Select(index, 0);
//发送字符串
SendKeys.Send("A");
}
private void Form1_Load(object sender, EventArgs e)
{
//注册事件,每当Box失去输入焦点时发生
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
}
void textBox1_LostFocus(object sender, EventArgs e)
{
index = textBox1.SelectionStart;
//将Box的光标位置保存
}