数字文本框代码 [英] Numeric Textbox Code

查看:51
本文介绍了数字文本框代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为那些认为我在同一主题上发帖的人道歉。并不是我不理解你的所有评论 - 而且我肯定是在阅读它们 - 但我认为我对如何处理应用程序中的''用户体验'有不同的看法我'创造。虽然我知道我可以允许用户输入数字和字母文本 - 在文本框中 - 然后告诉他们何时执行命令这不是数字数据,我宁愿不允许他们输入字母数据 - 开始。


我已经从某人建议的页面发布了以下代码。它来自William Ryan。我用它 - 它确实有效 - 但我遇到了两个问题。一,用户不能使用退格键和两个,它允许某人输入另一个。。 - 小数。代码作者意识到这一点,并说:上面的使用将允许所有有效数字和小数。对此进行扩展以确认您在数字中没有两位小数,允许使用货币字符等并不是很困难。但是 - 说实话 - 我不遵循代码甚至做的事情。有人可以澄清它在做什么以及如何修改它以仅允许一个小数? (Bernie Yaeger我也读了你的帖子 - 如果你能告诉我如何下载你的代码/控件 - 我很乐意看到它)请参阅下面的William'代码(链接: http://www.knowdotnet.com/articles/n...textboxes.html <私有超载Sub TextBox1_TextChanged(ByVal sender As System.Object,ByVal e As System.Windows.Forms.KeyPressEventArgs)处理TextBox1.KeyPres


Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar

Dim isDecimal As Boolean = e.KeyChar.ToString ="。

如果不是isKey而不是isDecimal

e.Handled = Tru

结束我

结束子

I apologize for those of you who think I''m posting on the same topic. It is not that I don''t appreciate all of your comments - and I''m definitely reading them all - but I think I have a differing opinion of how I want to handle the ''user experience'' in the application I''m creating. While I know I could allow the user to enter in number and alpha text - in a text box - and then tell them when the execuate a command "This is not numeric data", I would rather not allow them to enter alpha data - to begin with.

I''ve posted the below code from a page someone suggested. It is from a William Ryan. I have used it - it does work - but I run into two problems. One, the user cannot use the backspace key and two, it allows someone to enter another "." - decimal. The code author realizes this and says, "The use above will allow all valid numbers as well as decimals. It wouldn''t be very difficult to expand upon this to verify that you don''t have two decimal in the number, allow for currency characters etc." However - to be quite honest - I don''t follow what the code is even doing. Can someone clarify what it is doing and how one would modify it to allow for only one decimal? (Bernie Yaeger I did read your post as well - if you could let me know how I could download your code/control - I would love to see it) Please see William''s code below (link: http://www.knowdotnet.com/articles/n...textboxes.html

Private Overloads Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPres

Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar
Dim isDecimal As Boolean = e.KeyChar.ToString = ".
If Not isKey And Not isDecimal The
e.Handled = Tru
End I
End Sub

推荐答案

嗨Keith,


我已经看过你的帖子。我现在有点忙,但我要么。把代码压缩到

你或者联系你并以另一种方式告诉你。我会得到的这个

希望在本周末的某个时间完成,Mon是最新的。我将尝试包括

一个解释任何可能需要解释的文档。我实际上有2个

控件,我可能告诉过你 - 一个用于整数,一个用于货币。


但是 - 我需要一个电子邮件地址或者将ftp位置发送到 -

请在最方便的时候回复我。您可以直接发送电子邮件至 be*****@cherwellinc.com ,直接发送电子邮件至



伯尼


" Keith" <一个******* @ discussions.microsoft.com>在消息中写道

新闻:BA ********************************** @ microsof t.com ...
Hi Keith,

I''ve read your post. I''m a bit busy now but I will either .zip the code to
you or contact you and get it to you in another manner. I will get this
done hopefully sometime this weekend, Mon the latest. I will try to include
a doc explaining whatever might need explanation. I actually have 2
controls, as I may have told you - one for integers and one for currency.

But - I need to have an email address or an ftp location to send it to -
please get back to me with this at your soonest convenience. You can email
me directly at be*****@cherwellinc.com.

Bernie

"Keith" <an*******@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
我为那些认为我在同一主题上发帖的人道歉。它
并不是我不理解你的所有评论 - 而且我绝对是

阅读它们 - 但我想我对我的想法有不同的看法

处理我正在创建的应用程序中的用户体验。虽然我知道

我可以允许用户在文本框中输入数字和字母文本 -

然后在执行命令时告诉他们这是不是数字数据,我不想让他们输入alpha数据,而是开始使用。
我已经从有人建议的页面发布了以下代码。这是来自
William Ryan。我用过它 - 它确实有用 - 但是我遇到了两个问题。

一,用户不能使用退格键和两个,它允许某人

输入另一个"。" - 小数。代码作者意识到这一点并说,上面使用的

将允许所有有效数字和小数。它不会很难扩展到这个,以验证你没有数字中的两位小数

,允许使用货币字符等。 ;但是 - 要相当实实在在的宝贝 - 我不会遵循代码甚至做的事情。有人可以澄清

它正在做什么以及如何修改它只允许一个小数?

(Bernie Yaeger我也读了你的帖子 - 如果你能让我知道我怎么下载你的代码/控件我可以下载你的代码/控件 - 我很乐意看到它。请参阅

William'的代码如下(链接:
http://www.knowdotnet.com/articles/n...textboxes。 HTML
私有重载Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.KeyPressEventArgs)Handles TextBox1.KeyPress
Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)
Dim isDecimal As Boolean = e.KeyChar.ToString ="。"
如果Not isKey且Not isDecimal则
e.Handled = True
结束如果
结束子
I apologize for those of you who think I''m posting on the same topic. It is not that I don''t appreciate all of your comments - and I''m definitely
reading them all - but I think I have a differing opinion of how I want to
handle the ''user experience'' in the application I''m creating. While I know
I could allow the user to enter in number and alpha text - in a text box -
and then tell them when the execuate a command "This is not numeric data", I
would rather not allow them to enter alpha data - to begin with.
I''ve posted the below code from a page someone suggested. It is from a William Ryan. I have used it - it does work - but I run into two problems.
One, the user cannot use the backspace key and two, it allows someone to
enter another "." - decimal. The code author realizes this and says, "The
use above will allow all valid numbers as well as decimals. It wouldn''t be
very difficult to expand upon this to verify that you don''t have two decimal
in the number, allow for currency characters etc." However - to be quite
honest - I don''t follow what the code is even doing. Can someone clarify
what it is doing and how one would modify it to allow for only one decimal?
(Bernie Yaeger I did read your post as well - if you could let me know how I
could download your code/control - I would love to see it) Please see
William''s code below (link:
http://www.knowdotnet.com/articles/n...textboxes.html)
Private Overloads Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)
Dim isDecimal As Boolean = e.KeyChar.ToString = "."
If Not isKey And Not isDecimal Then
e.Handled = True
End If
End Sub



*" =?Utf-8?B?S2VpdGg =?=" <一个******* @ discussions.microsoft.com> scripsit:
* "=?Utf-8?B?S2VpdGg=?=" <an*******@discussions.microsoft.com> scripsit:
我已经从有人建议的页面发布了以下代码。它来自William Ryan。我用它 - 它确实有用 - 但我遇到了两个问题。一,用户不能使用退格键和两个,它允许某人输入另一个。。 - 十进制。
I''ve posted the below code from a page someone suggested. It is from
a William Ryan. I have used it - it does work - but I run into two
problems. One, the user cannot use the backspace key and two, it allows
someone to enter another "." - decimal.




有一个第三个问题:它不允许我输入德语数字

数字格式当我在德语系统上运行应用程序(对于

示例,12,3。它阻止我输入十六进制数字或

八进制格式。


如果用户已经输入了。并且现在想要将其移动到

如果他在文本框中输入的数字中的另一个位置怎么办?通过

禁止用户输入第二个。,编辑将非常困难。

用户必须删除。,然后转移到另一个位置并且

在那里输入。。


只需2欧分......


-

Herfried K. Wagner [MVP]

< URL:http://dotnet.mvps.org/>



There is a 3rd problem: It doesn''t allow me to enter a number in German
number format when I run the app on a German language system (for
example, "12,3". And it prevents me from entering a number in hex or
octal format.

What if the user already entered a "." and now wants to move it to
another position inside the number he has typed into the textbox? By
forbidding the user to enter a 2nd ".", editing will be very hard. The
user will have to delete the ".", then move to another position and
enter the "." there.

Just my 2 Euro cents...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


嗨Keith,


当你留在原始帖子中并告诉那里有什么

问题,比我给你的样本我已经做了几天

前。它包括Herfried想要的方法,我认为这一个

是客户最好的方法之一。


我希望这有帮助吗?


Cor


\\\

Private Sub Form1_Load(ByVal sender As System.Object,_

ByVal e As System.EventArgs)处理MyBase.Load

Me.TextBox1.MaxLength = 10''或任何你想要的

End Sub

Private Sub textbox1_KeyUp(ByVal sender As Object,_

ByVal e As System.Windows.Forms.KeyEventArgs)处理textbox1.KeyUp

如果e .KeyValue<> 8然后

如果不是IsNumeric(TextBox1.Text)那么

MessageBox.Show(只允许数字)

TextBox1.SelectionStart = TextBox1.Text.Length - 1

TextBox1.SelectionLength = 1

结束如果

结束如果

结束Sub

Private Sub TextBox1_Validating(ByVal sender _

As Object,ByVal e As System.ComponentModel.CancelEventArgs)_

处理TextBox1.Validating

如果不是IsNumeric(TextBox1.Text)那么

MessageBox.Show(粘贴错误)

TextBox1.Focus()

结束如果

结束子

///

Hi Keith,

When you was stayed in the original thread and told there what where the
problems, than I could have given you this sample I made already some days
ago. It includes the methode which Herfried wants as well, I think this one
is one of the nicest for the customer.

I hope this helps?

Cor

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.MaxLength = 10 ''or whatever you want
End Sub
Private Sub textbox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyUp
If e.KeyValue <> 8 Then
If Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("Only numeric is allowed")
TextBox1.SelectionStart = TextBox1.Text.Length - 1
TextBox1.SelectionLength = 1
End If
End If
End Sub
Private Sub TextBox1_Validating(ByVal sender _
As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
If Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("There was an error pasting")
TextBox1.Focus()
End If
End Sub
///


这篇关于数字文本框代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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