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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Num As Integer ' Declars Num as a Number
If CheckBox1.Checked = True Then ' Checks if the checkbox is checked
If TextBox1.TextLength > 0 Then ' Checks if Textbox is checked
If IsNumeric(TextBox1.Text) = True Then ' Checks if text is a number
Num = Num + TextBox1.Text ' Number + what ever is inside textbox1
Else
TextBox1.Text = 0
End If
End If
End If
If CheckBox2.Checked = True Then ' Checks if the checkbox is checked
If TextBox2.TextLength > 0 Then ' Checks if Textbox is checked
If IsNumeric(TextBox2.Text) = True Then ' Checks if text is a number
Num = Num + TextBox2.Text ' Number + what ever is inside textbox2
Else
TextBox2.Text = 0
End If
End If
End If
If CheckBox3.Checked = True Then ' Checks if the checkbox is checked
If TextBox3.TextLength > 0 Then ' Checks if Textbox is checked
If IsNumeric(TextBox3.Text) = True Then ' Checks if text is a number
Num = Num + TextBox3.Text ' Number + what ever is inside textbox3
Else
TextBox3.Text = 0
End If
End If
End If
RichTextBox1.Text = Num ' Displays total of Number
Num = 0 ' Resets number for next time button is pressed
End Sub
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. :lol:
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 ;)
Re: VB.Net 2008 - 3 Check Boxes, 3 Text Boxes - Adding if Check box is checked.
what about this :D
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
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.
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
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