PDA

View Full Version : VB help with button



l33t p1mp
09-07-2008, 01:32 PM
Heres the code I have so far.

Public Class Form1

Private Sub txtSide_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSide.TextChanged
Dim side As Integer
Dim area As Integer
side = Val(Me.txtSide.Text)
area = side * side
Me.lblAnswer.Text = area
End Sub

Private Sub btnAnswer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAnswer.Click

End Sub
End Class
My problem is when I type in something into my textbox, it gives the area of it immediately. What do I put into the button command to make it wait until the button is pressed for the answer to appear?

Marvin_The_Martian
09-07-2008, 01:57 PM
You use button.keypress event, query the keypress if it's an enter/return then run the function :)

edit:

just to make it even clearer, the code you have in textchanged event is in the wrong place ;)

Try something like this? Though I hate giving working examples


Public Class Form1

Private Sub txtside_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtside.KeyPress
If e.KeyChar = chr$(13) Then
'handles if you press enter
btnAnswer_Click(Me, e)
End If
End Sub

Private Sub btnAnswer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAnswer.Click
Dim side As Integer
Dim area As Integer
side = Val(txtside.Text)
area = side * side
lblAnswer.Text = area
End Sub

End Class

You can leave out the entire txt handling though it's only handy when you want to avoid having to click on the button and offer people a chanche to just press enter after their done with their input

l33t p1mp
09-07-2008, 03:56 PM
Edit: Doesnt work... >.>
So much for working example lol.

Marvin_The_Martian
09-07-2008, 04:07 PM
lol that should work if you fix the error yes :rofl: shouldn't be that hard it's just a charcode

You can also delete the txtKeypress sub, if you're not watching for an enter why have code there ;)

And working example's are overrated at your level, you won't learn. Go some steps up and working examples become almost necesarry to see to understand a concept :)

You know what, I'll give you a working example if you click on my signature link ;)

l33t p1mp
09-07-2008, 05:45 PM
Lol, ok, clicked it. Working example please? Also, I seem to learn better with working examples lol, thats how I learned what I know in c++.

Marvin_The_Martian
09-07-2008, 10:52 PM
Public Class Form1

Private Sub btnAnswer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAnswer.Click
Dim side As Integer
Dim area As Integer
side = Val(txtside.Text)
area = side * side
lblAnswer.Text = area
End Sub

End Class

That's the only functionalty you really need, and it should 100% work if you still have the label, textbox and button with the same names in the project. Let me ask you, if you know c++, why don't you stick with c#? Syntax is more easy to learn since it's far more simular.

l33t p1mp
09-08-2008, 03:06 AM
Im taking vB at school. ;)