D:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\examsignup\Form7.cs(437): “System.Windows.Forms.KeyEventArgs”并不包含对“KeyChar”的定义
将窗体的KeyPreview设为True if (e.KeyChar==(char)13) { SelectNextControl(ActiveControl,true,true,true,true); }
if (e.KeyChar==(char)13)
{
SelectNextControl(ActiveControl,true,true,true,true);
}
name7.Focus();
之前加一句
e.Handled = true;
如果不这样的话,KeyUp事件可能还是发生在原来的控件上,就又把焦点带回去了。
只是一个猜测,你可以试一下
然后把KeyPress KeyUp KeyDown按所有可能的组合都试一遍,我不信就不行。
{
this.textBox2.Focus();
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
e.Handled = true;
bool myCheck = false;
myCheck = IsNumber(textBox1.Text.ToString());
if (myCheck == true)
{
MessageBox.Show("您输入的国家名为:"+textBox1.Text+"有错误,请检查!");
textBox1.Text = "";
textBox1.Focus();
}
else
button1.Focus();
}
}
我试着这样写:
private void studentno7_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
MessageBox.Show("所有空都必须填写!,出错信息提示:");
if(e.KeyCode.ToString()=="Enter")
{
MessageBox.Show("所有空都必须填写!,出错信息提示:");
SendKeys.Send("{TAB}");
}
还是不行!!连消息框都没有 :(
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data; namespace EnterToTab
{
/// <summary>
/// 这是一个Enter to Tab 的例子。
/// 注意:
/// 1.不要设置From的AcceptButton,否则子控件无法响应回车事件。
/// 2. 用ENTER代替TAB对于TextBox等的AcceptsReturn属性会造成影响。
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public MainForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(144, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Enter To Tab 测试:";
//
// textBox1
//
this.textBox1.AcceptsReturn = true;
this.textBox1.Location = new System.Drawing.Point(8, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 64);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(120, 21);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"1",
"2",
"3",
"4",
"5",
"6"});
this.comboBox1.Location = new System.Drawing.Point(8, 96);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(120, 20);
this.comboBox1.TabIndex = 3;
this.comboBox1.Text = "1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 136);
this.button1.Name = "button1";
this.button1.TabIndex = 4;
this.button1.Text = "关闭";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(152, 165);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.comboBox1,
this.textBox2,
this.textBox1,
this.label1});
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "测试";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
} private void EnterKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
{
System.Windows.Forms.SendKeys.Send("{TAB}");
}
} private void MainForm_Load(object sender, System.EventArgs e)
{
foreach(Control ctrl in this.Controls)
{
if ((ctrl is TextBox) || (ctrl is ComboBox))
{
ctrl.KeyDown += new System.Windows.Forms.KeyEventHandler(this.EnterKeyDown);
}
}
} private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
//这个TextBox挂接了两个EventHandler.当挂接多个EventHandler时注意挂接的顺序。
MessageBox.Show("这里还可以写处理代码");
} private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
有没有人愿意帮我改改?!!
留email我发过去。谢谢!分数上不是问题!!
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyValue == 13)
textBox2.Focus();
}或许我不很明白你的意思
{
this.textBox2.Focus();
}
private void studentno7_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode.ToString()=="Enter")
name7.Focus();
}
完全正常。
你给出的另一套代码,只要去掉第一个消息框也正常:
private void studentno7_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// MessageBox.Show("所有空都必须填写!,出错信息提示:");
if(e.KeyCode.ToString()=="Enter")
{
MessageBox.Show("所有空都必须填写!,出错信息提示:");
SendKeys.Send("{TAB}");
}
}
我知道了,是我的程序里少了这句:
this.studentno7.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.studentno7_KeyPress);
和this.Load += new System.EventHandler(this.Form7_Load);
对吗?
厚脸皮问一句可以交个朋友吗?30647766(我的网名很难听不要笑噢)
真的感谢你,谢谢!
对不起,Jasanye(叶子)结不了贴!!
对不起:(
不知道这里是怎么了。
我再问个问题可以吗?
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode.ToString() == "Enter")
this.textBox2.Focus();
if(e.KeyCode.ToString() == "Space")
this.textBox3.Focus();
}