!!!新手求教,超级简单问题!!!

Comer 2005-11-26 10:33:10
请各位帮个小忙,把下面的delphi代码翻译成c#的:
procedure TPhysicInfoForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then {判断是按执行键}
if not (ActiveControl is TDbgrid) Then { 不是在TDbgrid控件内}
Begin
key := #0;
perform(WM_NEXTDLGCTL,0,0);{移动到下一个控件}
end;
end;

或者给段在c#里实现相同功能的代码也可以!!!
...全文
173 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Knight94 2005-11-28
  • 打赏
  • 举报
回复
我刚才试了一下,在窗体上放入一个textbox,只有当textbox在enable=false或visible=false情况下,form_keypress事件才能捕获,否则,所有的keypress事件被textbox捕获。
Comer 2005-11-28
  • 打赏
  • 举报
回复
没有被别的控件捕获也不行阿,我试过了
Knight94 2005-11-28
  • 打赏
  • 举报
回复
并不是chenyuming2004(这辈子我算是废了) 方法不对,而是你要判断一下keypress是否被别的控减捕获了,如果被其他控件捕获了,那么form_keypress就不能获得了。
Comer 2005-11-28
  • 打赏
  • 举报
回复
自己顶
Comer 2005-11-28
  • 打赏
  • 举报
回复
楼上的也不好用阿

难道在delphi版很简单的事,拿到c#来就变得很难了嘛?
Knight94 2005-11-28
  • 打赏
  • 举报
回复
晕~
SendKeys.Send("{TAB}");//这只是把Enter键换成Tab键而已

实际上,如果你不把FormKeyPreview属性设为true,并在Form中KeyPress事件中进行处理的话,一样也是没效果。
而且像判断是否为Enter键这些代码也是不能少的。
Comer 2005-11-28
  • 打赏
  • 举报
回复
多谢楼上的

在下出学c#,就遇到这么个简单问题,前俩位星星,差点把我学c#的信心给抹煞了
nate_liu 2005-11-28
  • 打赏
  • 举报
回复
直接使用 SendKeys.Send("{TAB}");
Knight94 2005-11-28
  • 打赏
  • 举报
回复
不过Form中KeyPress事件也要写。
Knight94 2005-11-28
  • 打赏
  • 举报
回复
如果是这样的话,是可以解决的,不过你需要设置Form的KeyPreview属性为true就行了。
Comer 2005-11-28
  • 打赏
  • 举报
回复
我的目的是想实现回车和按Tab键一样的效果,如果textbox的enable=false或visible=false,那不是没有意义了嘛

在delphi很容易实现的东西,为什么拿到c#里搞的这么复杂呢?

我看了下同事写的代码,是这样实现的:
#region 文本框的回车换行
private void editUsername_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
editUserDesc.Focus();
}
}
private void editUserDesc_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
editPassword.Focus();
}
}
private void editPassword_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
EditUnitcode.Focus();
}
}
private void EditUnitcode_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
editemail.Focus();
}
}
private void editemail_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
editdepartment.Focus();
}
}
private void editdepartment_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
textBoxBase2.Focus();
}
}
private void textBoxBase2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
checkBoxBase2.Focus();
}
}
private void checkBoxBase2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
textBoxBase1.Focus();
}
}
private void textBoxBase1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
textBoxBase3.Focus();
}
}
private void textBoxBase3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
editUsername.Focus();
}
}

#endregion

还好窗体里的textbox不算多,要不然。。。

个人觉得这样写程序,简直是三岁小孩玩堆积木

chenyuming2004 2005-11-27
  • 打赏
  • 举报
回复
private void Form9_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==(char)13)
{
if(false)
{
e.Handled=true;
this.SelectNextControl();
}
}
}

大概是这样吧。
Comer 2005-11-26
  • 打赏
  • 举报
回复
没人来

自己顶
Comer 2005-11-26
  • 打赏
  • 举报
回复
难道这么简单的问题都没人来拿分嘛?

MyLf 2005-11-26
  • 打赏
  • 举报
回复
你还是先好好学好C#。。。别想什么别的语言

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧