按C#中的输入密钥代码? [英] Press Enter Key Code in C#?

查看:81
本文介绍了按C#中的输入密钥代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有文本框的对话框,只收到

整数我已完成所有验证,但以下...

当用户按Enter键时应该检查文本框是否为空...

如果没有那么做其他事情否则this.close();

当用户按下一个键时,我的下面代码不起作用对话框关闭()自我

<前一个=c#> 如果(Convert.ToInt32(e.KeyChar)== 13 && txtsearhkey.Text.Length> 0)
{
frmviewpemaish vp = new frmviewpemaish();
vp.psearchkey = psearch;
vp.MdiParent = .ParentForm;

vp.Show();
vp.loadclient(txtsearhkey.Text);
this .Close();
}
其他
{
。关闭();
}
}

解决方案

输入不是字符;而且没有这样的人物。将字符转换为整数总是一个坏主意;记住,这是Unicode。并且键通常不是字符,它们在字符创建之前处理。要处理关键命中,您需要处理 KeyDown KeyUp 等事件。例如:

  this  .KeyDown + =(sender,eventArgs)= >  {
if (eventArgs.KeyCode == Keys.Enter)
DoSomething();
// ...
}

< br $> b $ b

另一个坏主意是使用MDI。为什么要折磨自己并吓跑你的用户?

帮自己一个大忙:根本不要使用MDI。没有它,您可以更轻松地实现设计,质量更好。 MDI甚至被微软高度劝阻,事实上,微软将其从WPF中删除并且很难支持它。更重要的是,如果您使用MDI,您将吓跑所有用户。只是不要。请参阅:

http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [ ^ ],

如何在WPF中创建MDI父窗口? [ ^ ]。



我可以解释做什么。请看我过去的答案:

如何在WPF中创建MDI父窗口? [解决方案2 ],

关于在WPF中使用MDI窗口的问题 [ ^ ],

麦当劳给出错误 [ ^ ],

如何设置子窗体最大化,最后一个子窗体最小化 [ ^ ]。



-SA


我解决了我的自我感谢重播

  if (Convert.ToInt32(e.KeyChar)==  13 
{
if (txtsearhkey.Text.Length > 0
{
frmviewpemaish vp = new frmviewpemaish();
vp.psearchkey = psearch;
vp.MdiParent = .ParentForm;

vp.Show();
vp.loadclient(txtsearhkey.Text);
this .Close();
}
其他
{
。关闭();
}


承诺给OP:一些示例代码(改编自实际使用的代码)来创建一个带有TextBox的UserControl,它只接受整数,减号作为第一个字符,并在按下'Enter键时自动隐藏:

 使用系统; 
使用 System.Windows.Forms;

命名空间 YourNameSpace
{
public partial class ucIntegerOnly:UserControl
{
public ucIntegersOnly()
{
InitializeComponent();
}

// 只有这个可以为空的Int64属性公开
public Int64 ? EnteredValue { private set ; get ; }

private Int32 maxInt64Length = 的Int64 .MaxValue.ToString()长度。

private void UserControl1_Load(对象发​​件人,EventArgs e)
{
EnteredValue = null ;
tbxIntegerEntry.Clear();
}

private void tbxIntegerEntry_KeyDown( object sender,KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string mTxt = tbxIntegerEntry.Text;

EnteredValue = null ;

// 如果Text为空或只有减号,则不评估
if (mTxt!= String .Empty&& mTxt!= -
{
EnteredValue = Convert.ToInt64(mTxt);
this .Hide();
}
}

e.SuppressKeyPress =
!(
// 退格允许
e.KeyCode == Keys.Back
||
// 不得超过长度限制
tbxIntegerEntry.Text.Length < = maxInt64Length
&&
// 允许的数字
char .IsDigit(Convert.ToChar(e.KeyCode))
||
// < span class =code-comment>在#0位置允许的减号

(tbxIntegerEntry.Text.Length == 0 )& &(e.KeyCo de == Keys.OemMinus)
);
}
}
}


i have a dialogbox that has only text box and receive only
integer i have done all validation except the Following ...
when user press Enter it should Check whether Textbox is empty or not...
if not then do something otherwise this.close();
my below code doesn't work when user Press a Key the dialogueBOx Close() it self

if (Convert.ToInt32(e.KeyChar) == 13 && txtsearhkey.Text.Length>0)
            {
                frmviewpemaish vp = new frmviewpemaish();
                vp.psearchkey = psearch;
                vp.MdiParent = this.ParentForm;
                
                vp.Show();
                vp.loadclient(txtsearhkey.Text);
                this.Close();
               }
                else
                 { 
                  this.Close(); 
                 }
               }

解决方案

Enter is not character; and there is no such characters. And this is always a bad idea to convert characters to integer; remember, this is Unicode. And keys are generally not character, they are handled before characters are created. To handle key hits, you need to handle events like KeyDown or KeyUp. For example:

this.KeyDown += (sender, eventArgs) => {
    if (eventArgs.KeyCode == Keys.Enter)
        DoSomething();
    //...
}



Another bad idea is using MDI, ever. Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA


i solved my self Thanks for Replay

if (Convert.ToInt32(e.KeyChar) == 13)
            {
                if (txtsearhkey.Text.Length > 0)
                {
                    frmviewpemaish vp = new frmviewpemaish();
                    vp.psearchkey = psearch;
                    vp.MdiParent = this.ParentForm;

                    vp.Show();
                    vp.loadclient(txtsearhkey.Text);
                    this.Close();
                }
                else
                {
                    this.Close();
                }


As promised to OP: some sample code (adapted from code in actual use) to create a UserControl with a TextBox that accepts only integers, a minus-sign as the first character, and that auto-hides when the 'Enter Key is pressed:

using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class ucIntegerOnly : UserControl
    {
        public ucIntegersOnly()
        {
            InitializeComponent();
        }

        // only this nullable Int64 property is exposed
        public Int64? EnteredValue { private set; get; }

        private Int32 maxInt64Length = Int64.MaxValue.ToString().Length;

        private void UserControl1_Load(object sender, EventArgs e)
        {
            EnteredValue = null;
            tbxIntegerEntry.Clear();
        }

        private void tbxIntegerEntry_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string mTxt = tbxIntegerEntry.Text;

                EnteredValue = null;

                // don't evaluate if Text is empty or only minus sign
                if (mTxt != String.Empty && mTxt != "-")
                {
                    EnteredValue = Convert.ToInt64(mTxt);
                    this.Hide();
                }
            }

            e.SuppressKeyPress =
            !(
                // backspace allowed
                e.KeyCode == Keys.Back
                ||
                // must not exceed length limit
                tbxIntegerEntry.Text.Length <= maxInt64Length
                &&
                // digits allowed
                char.IsDigit(Convert.ToChar(e.KeyCode))
                ||
                // minus sign allowed at position #0
                (tbxIntegerEntry.Text.Length == 0) && (e.KeyCode == Keys.OemMinus)
            );
        }
    }
}


这篇关于按C#中的输入密钥代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆