Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

If Textbox is left Empty Then Set it to Default Value

$
0
0
Hey, I am making a simple calculator in Visual Studio 2012 using Basic... I want there to be a default value to an input into a textbox in case it is left empty (default = 0)... this is my code
Public Class Form1

    'When ADD BUTTON IS CLICKED
    Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
        Dim total As Double = addNumbers(TextBox1.Text, TextBox2.Text)
        Label.Text = total.ToString()
    End Sub
    'ADD
    Private Function addNumbers(ByVal num1 As String, ByVal num2 As String)
        Dim nums1 As Double = System.Text.RegularExpressions.Regex.Replace(num1, "[^\d]", "")
        Dim nums2 As Double = System.Text.RegularExpressions.Regex.Replace(num2, "[^\d]", "")
        Return nums1 + nums2
    End Function


    'When SUBTRACTION BUTTON IS CLICKED
    Private Sub SubButton_Click(sender As Object, e As EventArgs) Handles SubButton.Click
        Dim difference As Double = subNumbers(TextBox1.Text, TextBox2.Text)
        Label.Text = difference.ToString()
    End Sub
    'SUBTRACT
    Private Function subNumbers(ByVal num1 As String, ByVal num2 As String)
        Dim nums1 As Double = System.Text.RegularExpressions.Regex.Replace(num1, "[^\d]", "")
        Dim nums2 As Double = System.Text.RegularExpressions.Regex.Replace(num2, "[^\d]", "")
        Return nums1 - nums2
    End Function


    'When MULTIPLY BUTTON IS CLICKED
    Private Sub MultiButton_Click(sender As Object, e As EventArgs) Handles MultiButton.Click
        Dim product As Double = multiNumbers(TextBox1.Text, TextBox2.Text)
        Label.Text = product.ToString()
    End Sub
    'MULTIPLY
    Private Function multiNumbers(ByVal num1 As String, ByVal num2 As String)
        Dim nums1 As Double = System.Text.RegularExpressions.Regex.Replace(num1, "[^\d]", "")
        Dim nums2 As Double = System.Text.RegularExpressions.Regex.Replace(num2, "[^\d]", "")
        Return nums1 * nums2
    End Function


    'When DIVIDE BUTTON IS CLICKED
    Private Sub DivButton_Click(sender As Object, e As EventArgs) Handles DivButton.Click
        Dim quotient As Double = divNumbers(TextBox1.Text, TextBox2.Text)
        Label.Text = quotient.ToString()
    End Sub
    'DIVIDE
    Private Function divNumbers(ByVal num1 As String, ByVal num2 As String)
        Dim nums1 As Double = System.Text.RegularExpressions.Regex.Replace(num1, "[^\d]", "")
        Dim nums2 As Double = System.Text.RegularExpressions.Regex.Replace(num2, "[^\d]", "")
        Return nums1 / nums2
    End Function

End Class



I have tried making the 'ByVal' into 'Optional ByVal' then setting a default for that. I also tried adding 'if' statements into the 'Private Sub ..._Clicked' and setting the value given off as a string (on the if statement I said 'if stringName.textLength = 0 then valueGivenOff = otherValueEntered' and vise versa for the other textbox being left empty)(I know that is not true in all cases, but you get the point). So, that didnt work. The code above is working code with no errors, so what do I need to add to it?

I feel like I did not word this very well, so if you have any questions, please ask :)

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>