Click here to Skip to main content
15,918,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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

VB
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



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.
Posted
Updated 1-Aug-12 21:31pm
v2

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.
 
Share this answer
 
Comments
bnvelarde 2-Aug-12 3:44am    
If I KeyIn 300 the sum that will show in the other textbox is 22 instead of 225.
Christian Graus 2-Aug-12 3:48am    
And if you step through in the debugger, what do you see ?
bnvelarde 2-Aug-12 3:56am    
There is no error?
Christian Graus 2-Aug-12 4:07am    
OK, so you see the correct values being pulled as you step through the code, everything does what you expect, but the end result is not what you expect ? How is that possible ?
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.
 
Share this answer
 
v2
Comments
bnvelarde 2-Aug-12 5:25am    
At the first I used the TextChanged and it got problem that is why I tried the KeyPress and got a problem too. I return again to TextChanged and so the problem in the LoadImage.
Joan M 2-Aug-12 5:29am    
A good test to do would be to show into the notification area what is happening (update a label if you can't do that in VB.NET).
Come with the results of those tests and update your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900