Results 1 to 7 of 7

Thread: If-Then statements and validating events in VB.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4

    If-Then statements and validating events in VB.net

    This is my first post.....


    I have created a solution that is one form. It is a project designed to calculate the area of a cube. I have the calculations correct but need to validate the entries that are in my text boxes (width, height, and length). Only entries that have positive numbers (numbers > 0) will be allowed. If the txt entry is incorrect when the user leaves that particular text box, the text entry should turn red. If the user attempts to calculate the cube area, then an error window should pop up when the calculate_click event occurs. I know I am supposed to use If then statements in the validate event but am unsure of how to do it. Can anyone help????

    I know it will be good to include the code and I have done so in a text file. If I can post the whole project folder (.sln, .vb, .vbproj files, etc) and that is better then I will. Hopefully this text file will suffice.

    Thanks ahead of time and I look foward to suggestions
    Attached Files Attached Files

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4

    not sure if attachment worked... here is a link


  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    My brain does not operate along the lines of the question setter and I would approach the solution much more simply but if you HAVE to stick to the framework of functions provided I will not be of much help to you.

    I would do something like:

    VB Code:
    1. Private Sub Test_Text_Validity(objTXT as TextBox)
    2.    if val(objTXT.Text)<=0 then objTXT.Forecolor=Color.Red
    3. end sub
    4.  
    5. Private Sub txtWidth_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt1.LostFocus
    6.     Test_Text_Validity(txtWidth)
    7.  End Sub

    Repeat the calling routine for each textbox and use the same approach,for checking the range of measurements, using multiple parameters for the appropriate ranges, using Select Case for the comparison of values.
    Last edited by taxes; Apr 29th, 2004 at 06:31 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4

    That helped!

    The first sub function you posted turns my txt box entries red if the entry is less then zero.

    What I also need is a validating event for a button. The button performs the muliplication of the 3 text box entries to get the answer. If any of these three entries are less then zero, I need to return an pop up error window..... any ideas?

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim strMessage as String="Invalid Value in "
    3.         select case true
    4.             case val(txtHeight.text)<=0
    5.                 strMessage=strMessage & "txtHeight"
    6.             case val(txtWidth.text)<=0
    7.                 strMessage=strMessage & "txtWidth"
    8.             case val(txtDepth.text)<=0
    9.                 strMessage=strMessage & "txtDepth"
    10.             case else
    11.                 (Here call the sub which performs the calculation etc)
    12.                 exit sub
    13.          end select
    14.          Messagebox.show(strMessage)
    15.         End Sub
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4

    That was the trick

    Worked like a charm..... plugged it right in and changed the variables to suit what I needed and it worked right off the bat.

    Thanks so much!!

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    You're welcome

    Don't forget to mark this thread "Resolved"
    Last edited by taxes; Apr 30th, 2004 at 05:08 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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