2 Replies - 1131 Views - Last Post: 10 November 2012 - 12:01 PM Rate Topic: -----

#1 mitchstokes225  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 05-November 09

How to Require selection of value in combobox

Posted 10 November 2012 - 11:35 AM

I am working on my second assignment in VB, where I have a combo box. The default is a blank, but I want to make sure that a value is selected before the calculator will run, so it doesn't kill my program. I have my if statements for my text boxes
 Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

        If txtLoanAmount <= 100000000 Then txtLoanAmount.Text
        Else
        MessageBox.Show("Missing Amount", "Please Enter Amount of Loan", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information)
        End if

        If txtInterestRate <= 100 Then txtInterestRate.Text
        Else
        MessageBox.Show("Missing Interest", "Please Enter Fixed Interest Rate", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information)
        End If



Will I need an if statement for my cboYears drop down?

extra info, I'm trying to validate this combo box has a selection on a button click.

Thanks

This post has been edited by mitchstokes225: 10 November 2012 - 11:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to Require selection of value in combobox

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: How to Require selection of value in combobox

Posted 10 November 2012 - 11:57 AM

You can check if the combobox has a selectedIndex other than -1.

If cboYears.SelectedIndex > -1 then
    ' Do Stuff
End if



Also some other tips for you. Things like If txtLoanAmount <= 100000000 Then txtLoanAmount.Text
are useless. What are you doing with txtLoanAmount? Nothing. On top of that you are not testing for existence, you are testing some crazy range. Try something like...

If String.IsNullOrEmpty(txtLoanAmount.Text) Then
  MessageBox.Show("Please enter an amount in the loan textbox")
End if



Then of course you have to then validate if they entered a number or not. For that you can use Decimal.TryParse.

Lastly test if it is in an acceptable range (probably greater than zero).

Good luck. :)
Was This Post Helpful? 1
  • +
  • -

#3 mitchstokes225  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 05-November 09

Re: How to Require selection of value in combobox

Posted 10 November 2012 - 12:01 PM

View PostMartyr2, on 10 November 2012 - 11:57 AM, said:

You can check if the combobox has a selectedIndex other than -1.

If cboYears.SelectedIndex > -1 then
    ' Do Stuff
End if



Also some other tips for you. Things like If txtLoanAmount <= 100000000 Then txtLoanAmount.Text
are useless. What are you doing with txtLoanAmount? Nothing. On top of that you are not testing for existence, you are testing some crazy range. Try something like...

If String.IsNullOrEmpty(txtLoanAmount.Text) Then
  MessageBox.Show("Please enter an amount in the loan textbox")
End if



Then of course you have to then validate if they entered a number or not. For that you can use Decimal.TryParse.

Lastly test if it is in an acceptable range (probably greater than zero).

Good luck. :)/>



THANK YOU!! I knew my validation code on that was wrong, but my brain is fried and I couldn't think of a better way to do it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1