仅在文本框中指定字符输入 +能够使用快捷方式

发布于 2024-12-15 19:10:32 字数 481 浏览 2 评论 0原文

我想要一个 C# 中的文本框,它只接受数字。我的方法是:

    private void textBoxStart_KeyPress(object sender, KeyPressEventArgs e)
    {
        char i = e.KeyChar;
       
        if (!((i >= '0' && i <= '9') || i == 8))
            e.Handled = true;
    }

效果很好。但我希望能够使用快捷键(如 ctrl + v)。我该怎么做呢?

编辑

我想简化我的问题。但是像“use numericUpDown”这样的回答迫使我显示所有问题。确切的问题是能够接受所有数字、大写字母和小写字母。 NumericUpDown 仅接受数字;p

edit 2

我在 winForms 应用程序中执行此操作。

I want to have a textbox in C# which accepts only digits. My way to do this:

    private void textBoxStart_KeyPress(object sender, KeyPressEventArgs e)
    {
        char i = e.KeyChar;
       
        if (!((i >= '0' && i <= '9') || i == 8))
            e.Handled = true;
    }

It works fine. But I want to be able use shortcuts (like ctrl + v). How can I do it?

edit

I wanted to simplify my problem. But answer like "use numericUpDown" oblige me to show all problem. Exactly problem is to be able accept all digit, capital and small letter. NumericUpDown accept only number;p

edit 2

I do it in winForms app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

神魇的王 2024-12-22 19:10:32

请改用 NumericUpDown 框。

Use a NumericUpDown box instead.

转瞬即逝 2024-12-22 19:10:32

查看 silverlight 特定的数字文本框。尽管它是特定于 Silverlight 的,但这应该适用于所有 C# 代码隐藏。

如何在 Silverlight 中创建数字文本框?

我最终使用 Sameh Serag 的实现并对其进行一些修改以设置最大/最小值。效果很好。

Check out the silverlight-specific numeric textbox. Although it's Silverlight-Specific, this should work for all C# code-behind.

How to create a numeric textbox in Silverlight?

I ended up using Sameh Serag's implementation and modifying it a bit to set a max/min. It's worked quite well.

北恋 2024-12-22 19:10:32

为什么要重新造轮子?您应该查看 WPF 工具包中的屏蔽文本框

你应该看看这个问题。看起来和你的问题很相似。

why recreate the wheel? you should check out the masked textbox in the WPF toolkit.

you should look at this SO question. seems similar to your problem.

标点 2024-12-22 19:10:32

你可以使用这个,尽管它可能有点过分了......

if (Regex.IsMatch(subjectString, @"[a-z\d]", RegexOptions.IgnoreCase)) {
        // Successful match
}

You could use this, although it's probably an overkill...

if (Regex.IsMatch(subjectString, @"[a-z\d]", RegexOptions.IgnoreCase)) {
        // Successful match
}
七禾 2024-12-22 19:10:32

摘自 Simple Numeric TextBox,这是基本例程:

private void textBox1_KeyDown(object sender, KeyEventArgs e) {
  bool numOK = ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ||
                 e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) &&
                 e.Modifiers != Keys.Shift);

  bool editOK = ((e.Control && e.KeyCode == Keys.V) ||
                 (e.Control && e.KeyCode == Keys.C) ||
                 (e.Control && e.KeyCode == Keys.X) ||
                 (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back));

  bool moveOK = (e.KeyCode == Keys.Up ||
                 e.KeyCode == Keys.Right ||
                 e.KeyCode == Keys.Down ||
                 e.KeyCode == Keys.Left ||
                 e.KeyCode == Keys.Home ||
                 e.KeyCode == Keys.End);

  if (!(numOK || editOK || moveOK)) {
    e.SuppressKeyPress = true;
    e.Handled = true;
  }
}

当然,通过限制键盘输入只是数字,但允许控制粘贴,您在控制最终用户粘贴的内容(可能是字符串)时遇到问题。如果您需要,请仔细查看链接的项目 - 您必须覆盖 WndProc 并拦截粘贴命令,然后检查剪贴板的内容。这将需要您在从 TextBox 继承的位置创建自己的控件。

Lifted from Simple Numeric TextBox, here is the basic routine:

private void textBox1_KeyDown(object sender, KeyEventArgs e) {
  bool numOK = ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ||
                 e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) &&
                 e.Modifiers != Keys.Shift);

  bool editOK = ((e.Control && e.KeyCode == Keys.V) ||
                 (e.Control && e.KeyCode == Keys.C) ||
                 (e.Control && e.KeyCode == Keys.X) ||
                 (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back));

  bool moveOK = (e.KeyCode == Keys.Up ||
                 e.KeyCode == Keys.Right ||
                 e.KeyCode == Keys.Down ||
                 e.KeyCode == Keys.Left ||
                 e.KeyCode == Keys.Home ||
                 e.KeyCode == Keys.End);

  if (!(numOK || editOK || moveOK)) {
    e.SuppressKeyPress = true;
    e.Handled = true;
  }
}

Of course, by restricting keyboard input to just numbers but allowing for Control-Paste, you have a problem with controlling what the end user is pasting, which could be strings. If you need that, look closer at the linked project-- you have to override WndProc and intercept the paste command and then inspect the contents of the clipboard. That would require you to make your own control where you inherit from TextBox.

一笑百媚生 2024-12-22 19:10:32

请参阅:http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b95b5197-e695-4e46-ae0c-9575c7c80c5b/

您可以使用 Validating 或 TextChanged 事件处理程序在键入后验证输入。对于这项任务,正则表达式应该很好。但这并不完美,因为它会以某种方式中断用户体验。

我认为没有易于使用的可能性来立即拒绝无效字符。您应该等到控件失去焦点或类似的情况。

然后您可以尝试使用 MaskedTextBox,但 Mask 不能接受任意数量的字符。

See this: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b95b5197-e695-4e46-ae0c-9575c7c80c5b/

You could use a Validating or TextChanged event handler to verify the input after typing. For that task regular expressions should be good. This isn't perfect though as interrupts the user experience in some way.

I can think of no easy to use possibility to reject invalid characters right away. You should wait till the control loses focus or something like that.

Then you could try a MaskedTextBox but the Mask cannot take an arbitrary number of characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文