Results 1 to 7 of 7

Thread: VB.Net 2008 - 3 Check Boxes, 3 Text Boxes - Adding if Check box is checked.

  1. #1

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    VB.Net 2008 - 3 Check Boxes, 3 Text Boxes - Adding if Check box is checked.

    Hey guys, I think I am posting this in the right place.

    I am rather happy and quite impressed with this. Be it quite simple, I am still impressed .

    But I am sure there is room for improvment.

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim Num As Integer ' Declars Num as a Number
    3.  
    4.         If CheckBox1.Checked = True Then ' Checks if the checkbox is checked
    5.             If TextBox1.TextLength > 0 Then ' Checks if Textbox is checked
    6.                 If IsNumeric(TextBox1.Text) = True Then ' Checks if text is a number
    7.                     Num = Num + TextBox1.Text ' Number + what ever is inside textbox1
    8.                 Else
    9.                     TextBox1.Text = 0
    10.                 End If
    11.             End If
    12.         End If
    13.  
    14.         If CheckBox2.Checked = True Then ' Checks if the checkbox is checked
    15.             If TextBox2.TextLength > 0 Then ' Checks if Textbox is checked
    16.                 If IsNumeric(TextBox2.Text) = True Then ' Checks if text is a number
    17.                     Num = Num + TextBox2.Text ' Number + what ever is inside textbox2
    18.                 Else
    19.                     TextBox2.Text = 0
    20.                 End If
    21.             End If
    22.         End If
    23.  
    24.         If CheckBox3.Checked = True Then ' Checks if the checkbox is checked
    25.             If TextBox3.TextLength > 0 Then ' Checks if Textbox is checked
    26.                 If IsNumeric(TextBox3.Text) = True Then ' Checks if text is a number
    27.                     Num = Num + TextBox3.Text ' Number + what ever is inside textbox3
    28.                 Else
    29.                     TextBox3.Text = 0
    30.                 End If
    31.             End If
    32.         End If
    33.  
    34.         RichTextBox1.Text = Num ' Displays total of Number
    35.  
    36.         Num = 0 ' Resets number for next time button is pressed
    37.     End Sub
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  2. #2
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: VB.Net 2008 - 3 Check Boxes, 3 Text Boxes - Adding if Check box is checked.

    Something I am always told about VB.NET (looks like you moved from VB6 like me) is to try to use int64.tryparse (or similar) instead of IsNumeric. Thats one suggestion. Another is that you are better to make the first line:

    Dim Num As Integer = 0

    The last line is not needed (whether you do the above or not as it is always re-declared. e.g. it is not static).

    Thats it for just now. Can't see anything else. There are others who will come in with byte arrays or something and speed it up by a factor of a billion though.

  3. #3

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Re: VB.Net 2008 - 3 Check Boxes, 3 Text Boxes - Adding if Check box is checked.

    lol, thanks, I will go through and check through all of that in a bit
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  4. #4
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: VB.Net 2008 - 3 Check Boxes, 3 Text Boxes - Adding if Check box is checked.

    what about this

    Code:
     Public Function GetNum(ByVal intNum As Double, ByVal CheckBox As CheckBox, ByVal TextBox As TextBox) As Double
            Try
                If CheckBox.Checked = True Then ' Checks if the checkbox is checked
                    If IsNumeric(TextBox1.Text) Then  ' Checks if Textbox has numeric value
                        intNum = intNum + CDbl(TextBox.Text) ' Number + what ever is inside textbox
                    Else
                        TextBox.Text = 0
                    End If
                End If
                Return intNum
            Catch exc As Exception
                Throw exc
            End Try
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Num As Double = 0    
            Try
                Num = GetNum(Num, CheckBox1, TextBox1)
                Num = GetNum(Num, CheckBox2, TextBox2)
                Num = GetNum(Num, CheckBox3, TextBox3)
                RichTextBox1.Text =cstr(Num) ' Displays total of Number
            Catch exc As Exception
                MessageBox.Show(exc.Message)
            End Try
        End Sub
    Last edited by riteshjain1982; Jun 12th, 2008 at 04:08 PM.
    __________________
    Rate the posts that helped you

  5. #5

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Re: VB.Net 2008 - 3 Check Boxes, 3 Text Boxes - Adding if Check box is checked.

    Thats excallent!

    Is there a way to test the speed of code? I have seen people do this but I do not know myself.
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: VB.Net 2008 - 3 Check Boxes, 3 Text Boxes - Adding if Check box is checked.

    The above suggestion from riteshjain is spot on - you want to use this and pass in your 2 controls into a separate method.

    I've just included this to show you another alternative way you could perform the same thing, accessing the form controls collection, but the above already posted sample will perform more efficiently and is the more professional way of coding this.

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim num As Integer
            AddTextboxValToNumber(num, Me, 1)
            AddTextboxValToNumber(num, Me, 2)
            AddTextboxValToNumber(num, Me, 3)
        End Sub
    
        Private Overloads Function AddTextboxValToNumber(ByVal numberToAlter As Integer, _
        ByVal formWithControls As Form, ByVal controlSeriesID As Integer)
            With formWithControls
                If CType(.Controls("CheckBox" & controlSeriesID.ToString), CheckBox).Checked = True Then
                    Dim textboxControl As TextBox = CType(.Controls("TextBox" & controlSeriesID.ToString), TextBox)
                    If (textboxControl.TextLength > 0) Then
                        Dim isTextboxValueNumeric As Boolean
                        Integer.TryParse(textboxControl.Text, isTextboxValueNumeric)
                        If (isTextboxValueNumeric) Then
                            numberToAlter += CType(TextBox1.Text, Integer)
                        Else
                            TextBox1.Text = (0).ToString
                        End If
                    End If
                End If
            End With
            Return numberToAlter
        End Function
    
    End Class

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: VB.Net 2008 - 3 Check Boxes, 3 Text Boxes - Adding if Check box is checked.

    To test the speed get a very accurate timer, and run the code a few thousand times. Simple as that. A search brings up a few examples.

    Edit: http://www.vbforums.com/showthread.php?t=231183

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