一些初学者的问题 [英] a few beginner questions

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

问题描述

我正在学习编程,并希望有人可以用以下内容帮助我/ b
$ b一个简单的计算器,输入一个字符串一个文本

框...如何阻止用户输入文本

而不是数字,或者给出错误信息?

另外,如何让程序验证txtBox1和txtBox2中有两个有效的条目,然后启用

按钮运算符(即+, - ,/,*)。

感谢您的帮助。

解决方案

1)Private Sub TextBox1_KeyPress( ByVal sender As Object,_

ByVal e As System.Windows.Forms.KeyPressEventArgs)_

处理TextBox1.KeyPress


如果e.KeyChar.IsNumber(e.KeyChar)那么

e.Handled = False

否则

e.Handled = True
结束如果


结束子

2)


在一个文本的TextChanged事件上盒子,打电话给一些

函数.... btnCalculate.Enabled = IsValid(textbox1.text)和

IsValid(textbox2.text)其中isValid是你的验证程序。


在textbox1_TextChanged上添加第二个句柄子句....句柄

textbox1.TextChanged,处理textBox2.TextChanged。


这将为你做。


干杯,


比尔



" MHoffman" < MH ******** @ hotmail.com>在消息中写道

news:37 **************************** @ phx.gbl ... < blockquote class =post_quotes>我正在学习编程,并希望有人可以帮助我使用以下内容:

对于一个简单的计算器,一个字符串输入到文本中/>框...如何阻止用户输入文本而不是数字,或者给出错误信息?

另外,如何让程序验证是否有在txtBox1和txtBox2中有两个有效的条目然后启用
按钮操作符(即+, - ,/,*)。

感谢您的帮助。



嘿,谢谢你的帮助...你认为你可以吗?b $ b虽然很快就向我解释了吗?我很感激

知道怎么做,但是我想学习所以有一天我可以比票据更丰富,(只是开玩笑,但你知道

我的意思是。


另外......退出确认箱怎么样?


如果更容易,e给我发电子邮件@ mh********@hotmail.com

谢谢,

meredith

-----原始消息-----
1)Private Sub TextBox1_KeyPress(ByVal sender As Object ,_
ByVal e As System.Windows.Forms.KeyPressEventArgs)_
处理TextBox1.KeyPress

如果e.KeyChar.IsNumber(e.KeyChar)那么
e.Handled = False
否则
e.Handled = True
结束如果

结束子
2)

关于一个文本框的TextChanged事件,调用一些
函数.... btnCalculate.Enabled = IsValid(textbox1.text)
andIsValid(textbox2.text)where isa lid是你的验证
例程。
在textbox1_TextChanged上添加第二个句柄
子句.... Handlestextbox1.TextChanged,Handles textBox2.TextChanged。

这样做它适合你。

干杯,

比尔

MHoffman < MH ******** @ hotmail.com>在消息中写道
新闻:37 **************************** @ phx.gbl ...

我只是学习编程,并希望有人能用
帮我解决以下问题:

对于一个简单的计算器,一个字符串输入一个文本
框。 ..如何阻止用户输入文本而不是数字,或者给出错误信息?

另外,如何让程序验证有两个 txtBox1和txtBox2中的有效条目然后启用
按钮操作符(即+, - ,/,*)。

感谢您的帮助。








大声笑。我认为需要解释。这不是明显的事情。我会

解释一切 - 甚至是你知道的东西。


1私人Sub TextBox1_KeyPress _

2( ByVal sender As Object,_

3 ByVal e As System.Windows.Forms.KeyPressEventArgs)_

4句柄TextBox1.KeyPress

5如果e.KeyChar.IsNumber(e.KeyChar)然后

6 e.Handled = False

7否则

8 e.Handled = True

9结束如果

10结束子


1 TextBox1按键的事件处理程序。

2发送者将是TextBox1,但为了简单起见,它作为对象传递

(编译器不是我们的)。

3关于哪个键和哪个键的信息其他的东西在

特殊结构中传递。

4这个例程将这个例程挂钩到TextBox1以获取KeyPress事件。

5 e.KeyChar是被按下的钥匙。

我sNumber测试数字键。

6和8

当键盘事件处理程序返回时,调用者可以说

哦,你已经处理了密钥,我不会再接受了。或者它可以说

啊,你只是看着它,但这取决于我处理它。在第一个

的情况下,密钥被有效地丢弃了。在最后一种情况下,密钥被传递给

TextBox。


e.Handled告诉电话;呃是否处理了密钥。所以False意味着它

< wasn''t>并且应该保留密钥并将其传递给TextBox。并且真实意味着

<被处理>应该被扔掉。


[我更喜欢它是'e.IgnoreKey = True''。这似乎更加明显。]


所以把它们放在一起。如果TextBox收到一个键,它将被忽略,除非它是一个数字。

第2部分即将出现... :-)


问候,

Fergus


I am just learning to program, and hoping someone can help
me with the following:

for a simple calculator, a string is entered into a text
box ... how do I prevent the user from entering a text
instead of a number, or give an error message?

Also, how can I make the program verify there are two
valid entries in txtBox1 and txtBox2 to then ENABLE the
button operators (ie +, -, /, *).

Thanks for the help.

解决方案

1)Private Sub TextBox1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress

If e.KeyChar.IsNumber(e.KeyChar) Then
e.Handled = False
Else
e.Handled = True
End If

End Sub
2)

On the TextChanged Event of one text box, call some
function....btnCalculate.Enabled = IsValid(textbox1.text) and
IsValid(textbox2.text) Where isValid is your validation routine.

On the textbox1_TextChanged add a second handles clause.... Handles
textbox1.TextChanged, Handles textBox2.TextChanged.

This will do it for you.

Cheers,

Bill
,
"MHoffman" <mh********@hotmail.com> wrote in message
news:37****************************@phx.gbl...

I am just learning to program, and hoping someone can help
me with the following:

for a simple calculator, a string is entered into a text
box ... how do I prevent the user from entering a text
instead of a number, or give an error message?

Also, how can I make the program verify there are two
valid entries in txtBox1 and txtBox2 to then ENABLE the
button operators (ie +, -, /, *).

Thanks for the help.



Hey, thanks for the help ... do you think you could
explain that to me though real quick? I appreciate to
know how to do it, but I want to learn to so one day I can
be richer than bill gates, (just joking, but you know that
I mean).

Also ... what about Exit Confirmation Box?

if easier, e-mail me @ mh********@hotmail.com

thanks,
meredith

-----Original Message-----
1)Private Sub TextBox1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress

If e.KeyChar.IsNumber(e.KeyChar) Then
e.Handled = False
Else
e.Handled = True
End If

End Sub
2)

On the TextChanged Event of one text box, call some
function....btnCalculate.Enabled = IsValid(textbox1.text) andIsValid(textbox2.text) Where isValid is your validation routine.
On the textbox1_TextChanged add a second handles clause.... Handlestextbox1.TextChanged, Handles textBox2.TextChanged.

This will do it for you.

Cheers,

Bill
,
"MHoffman" <mh********@hotmail.com> wrote in message
news:37****************************@phx.gbl...

I am just learning to program, and hoping someone can help me with the following:

for a simple calculator, a string is entered into a text
box ... how do I prevent the user from entering a text
instead of a number, or give an error message?

Also, how can I make the program verify there are two
valid entries in txtBox1 and txtBox2 to then ENABLE the
button operators (ie +, -, /, *).

Thanks for the help.


.



Hi

Lol. I thought it would need explaining. It''s not obvious stuff. I''ll
explain it all - even the stuff that you know.

1 Private Sub TextBox1_KeyPress _
2 (ByVal sender As Object, _
3 ByVal e As System.Windows.Forms.KeyPressEventArgs) _
4 Handles TextBox1.KeyPress
5 If e.KeyChar.IsNumber(e.KeyChar) Then
6 e.Handled = False
7 Else
8 e.Handled = True
9 End If
10 End Sub

1 The event handler for TextBox1''s key presses.
2 sender will be TextBox1 but it is passed as an Object for simplicity
(the compiler''s not ours).
3 The information about which key and other stuff is passed in a
special structure.
4 This hooks this routine to TextBox1 for KeyPress events.
5 e.KeyChar is the key that was pressed.
IsNumber tests for a digit key.
6 and 8
When a keyboard event handler returns, the caller can either say
"Oh, you''ve dealt with the key, I won''t take it any further" or it can say
"Ah, you just looked at it but it''s up to me to deal with it". In this first
case the key is effectively thrown away. In last case, the key is passed on to
the TextBox.

e.Handled tells the call;er whether the key was handled. So False means it
<wasn''t> and the key should be kept and passed to the TextBox. And True means
is <was handled> and should be thrown away.

[I''d prefer it to be ''e.IgnoreKey = True''. That seems much more obvious.]

So putting it all together. If the TextBox receives a key, it will be
ignored unless it''s a digit.
Part 2 coming up... :-)

Regards,
Fergus


这篇关于一些初学者的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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