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]
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
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

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