Compare two numbers and display larger of two in label

I am new to this, please be kind as I am sure to sound a bit dim.
I placed a command button and two text boxes on a form. I want to click the command button have the program look at the entries in txtfirst and txtsecond determine the larger of the two numbers typed there and then output that number to a label lblresults

so I coded Dim txtfirst as Integer
Dim txtsecond as Integer
If txtsecond > txtfirst Then
txtresults.text = txtsecond
Else
txtresults.text = txtfirst
End If
of course this puts out a 0 (zero) as the result.
how do I put the numbers out rather than the 0?
thanks

[636 byte] By [chipscap] at [2007-12-17]
# 1

Hi,

Double-Click on the Button in the Design Mode - that should automatically place a EventHandler method for the Click of the button. Within that you can place the following code snippet:


Dim txtfirst As Integer
Dim txtsecond As Integer
Try
txtfirst = Convert.ToInt32(TextBox1.Text.Trim())
txtsecond = Convert.ToInt32(TextBox2.Text.Trim())
If txtsecond > txtfirst Then
lblResults.Text = txtsecond
Else
lblResults.Text = txtfirst
End If
Catch ex As Exception
MessageBox.Show(
"ERROR : " + ex.Message)
End Try

Regards,
Vikram

Vikram at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
The problem is that you're redefining your textboxes within the click event (assuming your code is coming from the click event of the command button).

If you've already named your textboxes (through the designer) as txtfirst and txtsecond, you don't need to dim then. So your code would look like this:



If Convert.ToInt16(txtsecond.Text) > Convert.ToInt16(txtfirst.Text) Then
lblresults.Text = txtsecond.Text
Else
lblresults.Text = txtfirst.Text
End If

You don't need the Text property, I just use it for clarity.
Josh

JoshLindenmuth at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

hi,

this works for me:

Private Sub Command1_Click()

' Is txtsecond larger than txtfirst
If Form1.txtsecond.Text > Form1.txtfirst.Text Then
Form1.txtresults.Text = Form1.txtsecond.Text

' Is txtfirst larger than txtsecond
ElseIf Form1.txtfirst.Text > Form1.txtsecond.Text Then
Form1.txtresults.Text = Form1.txtfirst.Text

' are they equal
ElseIf Form1.txtfirst.Text = Form1.txtsecond.Text Then
Form1.txtresults.Text = 0
End If

End Sub

jackamarra at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
Smile Thank you, this works and I see that there are many ways to do this as I received other posts that, although different, worked the same.

thanks
Gary

chipscap at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...