模拟了一个简单的. 界面有2个文本框 ,1和2. public partial class Form2 : Form { public Form2() { InitializeComponent(); textBox1.Focus(); //初始化,文本框1获取焦点 } private void textBox1_TextChanged(object sender, EventArgs e) { //如果文本框1的长度大于10,则文本框2获取焦点 if (textBox1.TextLength > 10) { textBox2.Focus(); } } }
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 WindowsFormsApplication4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { CenterToScreen(); } private void plus_Click_1(object sender, EventArgs e) {
textBox1.MaxLength = 5;
//如果是纯数字.
//字符长度超过10.
if (textBox1.TextLength > 10)
{
//文本框2获取焦点.
textBox2.Focus();
}
...WinForm or ..?MaxLength的确是限制文本输入长度的属性..尝试直接F4从属性设置.
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.TextLength > 10)
{
textBox2.Focus();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent(); textBox1.Focus(); //初始化,文本框1获取焦点
} private void textBox1_TextChanged(object sender, EventArgs e)
{
//如果文本框1的长度大于10,则文本框2获取焦点
if (textBox1.TextLength > 10)
{
textBox2.Focus();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
CenterToScreen();
}
private void plus_Click_1(object sender, EventArgs e)
{
no1.Focus();
result.Clear();
result.Text = "";
long n1, n2, n3;
n1 = long.Parse(no1.Text);
n2 = long.Parse(no2.Text);
n3 = n1 + n2;
result.Text += ("" + n3 + "");
} private void minus_Click(object sender, EventArgs e)
{
no1.Focus();
result.Clear();
result.Text = "";
long n1, n2, n3;
n1 = long.Parse(no1.Text);
n2 = long.Parse(no2.Text);
n3 = n1 - n2;
result.Text += ("" + n3 + "");
} private void cheng_Click(object sender, EventArgs e)
{
no1.Focus();
result.Clear();
result.Text = "";
long n1, n2, n3;
n1 = long.Parse(no1.Text);
n2 = long.Parse(no2.Text);
n3 = n1 * n2;
result.Text += ("" + n3 + "");
} private void chu_Click(object sender, EventArgs e)
{
no1.Focus();
result.Clear();
result.Text = "";
long n1, n2, n3;
n1 = long.Parse(no1.Text);
n2 = long.Parse(no2.Text);
if (n2 != 0)
{
n3 = n1 / n2;
result.Text += ("" + n3 + "");
}
else
{
MessageBox.Show("错误!");
}
} private void button1_Click(object sender, EventArgs e)
{
this.no1.Text = this.no1.Text + 7;
} private void bt8_Click(object sender, EventArgs e)
{
this.no1.Text = this.no1.Text + 8;
} private void bt9_Click(object sender, EventArgs e)
{
this.no1.Text = this.no1.Text + 9;
} private void bt4_Click(object sender, EventArgs e)
{
this.no1.Text = this.no1.Text + 4;
} private void bt5_Click(object sender, EventArgs e)
{
this.no1.Text = this.no1.Text + 5;
} private void bt6_Click(object sender, EventArgs e)
{
this.no1.Text = this.no1.Text + 6;
} private void bt1_Click(object sender, EventArgs e)
{
this.no1.Text = this.no1.Text + 1;
} private void bt2_Click(object sender, EventArgs e)
{
this.no1.Text = this.no1.Text + 2;
} private void bt3_Click(object sender, EventArgs e)
{
this.no1.Text = this.no1.Text + 3;
} private void no1_TextChanged(object sender, EventArgs e)
{
no1.MaxLength = 10;
if (no1.TextLength >= 10)
{
no2.Focus();
}
} private void button1_Click_1(object sender, EventArgs e)
{
no1.Clear();
no1.Focus();
no2.Clear();
result.Text = "";
} private void no2_TextChanged(object sender, EventArgs e)
{
no2.MaxLength = 10;
}
}
}
e.Handle那个我这里实现不了
public bool isInt(string str)
{
//^([+-]?)表示加减号只能出现在字符串开头且只有一位
///d*表示后面可以有多个或一个十进制数
//$表示字符串结尾
return Regex.IsMatch(str, @"^([+-]?)/d*$");//返回只能以正负号开头的整数
}再TextChanged事件,写你的处理办法
打上断点, 看看为什么textbox1 一直处于Focus 状态. //button1_click 是什么事件?
private void button1_Click_1(object sender, EventArgs e)
{
no1.Clear();
no1.Focus(); //no1获取焦点
no2.Clear();
result.Text = "";
}
但是按数字按钮还是往1输入内容,用键盘的话才向2输入
button1是清空按钮
命名我看起来方便就这样来了
private void button1_Click(object sender, EventArgs e)
{
this.no1.Text = this.no1.Text + 7; //只是给no1 这个文本框累加值.
}建议你定义一个全局变量, 用来记载textbox 1 和 textbox 2 的焦点状况.
//仅供参考.
string focus = string.Empty; //记录了当前该由哪个文本框获取内容.
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.TextLength > 10)
{
textBox2.Focus();
focus = "textBox2";
}
} //数字1 的按钮事件.
private void button1_Click(object sender, EventArgs e)
{
if (focus.Equals("textBox2"))
{
textBox2.Text += "1";
}
else
{
textBox1.Text += "1";
}
}