按键计算 [英] Calculation with keypress

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

问题描述



我在KeyPress上有问题.我正在尝试创建长宽比计算.

Hi,

i have a problem with the KeyPress. I am trying to create an Aspect Ratio calculation.

Private Sub txtpxWidth_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtpxWidth.KeyPress
        Dim originalWidth As Integer = _storeImage.Width
        Dim originalHeight As Integer = _storeImage.Height

        Dim newWidth As Integer
        Dim newHeight As Integer

        If txtpxWidth.Text = "" Then

        Else
            If CheckBox1.CheckState = CheckState.Checked Then
                newWidth = originalWidth / originalHeight * txtpxHeight.Text
                newHeight = originalHeight / originalWidth * txtpxWidth.Text

                txtpxHeight.Text = newHeight
            Else
                newWidth = txtpxWidth.Text
                newHeight = txtpxHeight.Text
            End If
        End If
    End Sub




当我将代码放在Click事件中但在KeyPress中却不是时,计算是正确的.

请帮助我.

谢谢.




The computation is right when i put the code in a Click event but in the KeyPress it is not.

Please help me with this.

Thank you.

推荐答案

您是否逐步完成了代码?您是否尝试过从文本框中提取文本并确保它是具有期望值的整数?您能否告诉我们出了什么问题,而不仅仅是不是"?

您可以设置断点,然后在调试器中单步执行代码,然后查看出了什么问题.
Have you stepped through the code ? Have you tried grabbing the text out of the textbox and making sure it''s an integer with the value you expect ? Could you tell us what is going wrong, instead of just ''it is not'' ?

You can set breakpoints and then step through the code in the debugger, and then you can see what is going wrong.


可能是因为您在控件中使用按键,并且您想要刚处理完keypress事件后,如何在控件中读取更新的值?

可能是系统在更新控件本身之前正在处理keypress事件,因此该值未更新,并且您获得了错误的值.

我会尝试修改处理程序,并使用 TextChanged one而不是KeyPress.

PS:我从未使用过VB.NET,这只是一种可能.

PS2:绝对您应该控制用户输入哪种类型的值...分号,字母...可能会破坏您的计算...,请避免使用0.

希望这会有所帮助.
Could it be that you are using keypress in a control and that you want to read the updated value inside the control just after processing the keypress event?

Probably the system is handling the keypress event before updating the control itself and therefore the value is not updated and you are getting the wrong values.

I would try to modify the handler and use the TextChanged one and not the KeyPress.

PS: I''ve never used VB.NET, this is simply a possibility.

PS2: definitely you should control which kind of values the user enters... a semicolon, letter... could destroy your calculation... and please avoid a 0.

Hope this helps.


这篇关于按键计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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