Results 1 to 8 of 8

Thread: how do i validate textbox input against ddl value range

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    how do i validate textbox input against ddl value range

    i have a drop down list with weight range values i.e 1-50 or 51-70 and so forth and i have a textbox that the user enters a value for the weight according to the weight range. i want the user to only enter a value within this range and how do i validate for this

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how do i validate textbox input against ddl value range

    So, this is a web application and you have a DropDownList control or is it a Windows app and it's actually a ComboBox? If you use the actual names for things then it avoids confusion. Also, questions on web applications are generally better posted in the ASP.NET forum.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    Re: how do i validate textbox input against ddl value range

    hie thanks for the response. Its a windows form app and its a normal combobox

    Quote Originally Posted by jmcilhinney View Post
    So, this is a web application and you have a DropDownList control or is it a Windows app and it's actually a ComboBox? If you use the actual names for things then it avoids confusion. Also, questions on web applications are generally better posted in the ASP.NET forum.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how do i validate textbox input against ddl value range

    There's no such thing as a normal ddl in Windows Forms. The control is called ComboBox so please use the name ComboBox when you refer to it so that we don't have to waste our time trying to clarify. As I said, if you use the proper name for everything then there's no confusion and we can provide you with the help you want without wasting our time or yours.

    Anyway, there are various ways that this could be done. The easiest in your case might be to use a couple of arrays:
    Code:
    Private minima As Integer() = {1, 51}
    Private maxima As Integer() = {50, 70}
    You can then use the SelectedIndex of the ComboBox as an index into both the arrays to get the minimum and maximum values allowed, e.g.
    Code:
    Dim index As Integer = ComboBox1.SelectedIndex
    Dim minimum As Integer = minima(index)
    Dim maximum As Integer = maxima(index)
    
    If theNumber < minimum OrElse theNumber > maximum Then
        MessageBox.Show(String.Format("Please enter a value in the range {0} to {1} inclusive.", minimum, maximum))
    End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    Re: how do i validate textbox input against ddl value range

    thanks for the idea...but in my case i dont know what weight ranges the user will input so i cannot use the arrays. is there any other way

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how do i validate textbox input against ddl value range

    I must have missed the part where you told us that the user was entering those ranges. You can still use the same principle though. You can use List(Of Integer) instead of Integer() and then you can add new ranges at run time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    Re: how do i validate textbox input against ddl value range

    it has given me an error object reference not set to an instance of an object on line
    dim minimum as integer = minima(index)

    i have used the textbox validating control

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how do i validate textbox input against ddl value range

    There is no TextBox validating control. Do you mean the Validating event of the TextBox control?

    What was the value of 'index" when the error occurred? If there was not item selected then it would have been -1, so you obviously need to code for that special case specifically. If it was any other value then the error occurred because you did it wrong, but we can't tell you what you did wrong because we can't see what you did. If the number of items in the arrays/collections is the same as in the ComboBox then it will work, except for the special case of no item selected, which would need to be coded for specifically.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width