5 Replies - 11593 Views - Last Post: 04 January 2013 - 03:10 PM Rate Topic: -----

#1 mgaines2  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 06-December 12

If Textbox is left Empty Then Set it to Default Value

Posted 04 January 2013 - 02:26 PM

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 :)

Is This A Good Question/Topic? 0
  • +

Replies To: If Textbox is left Empty Then Set it to Default Value

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,090
  • Joined: 02-July 08

Re: If Textbox is left Empty Then Set it to Default Value

Posted 04 January 2013 - 02:43 PM

How about the textChanged event for the Textbox and {psuedo}if it's length = 0 then .text = "0"
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: If Textbox is left Empty Then Set it to Default Value

Posted 04 January 2013 - 02:47 PM

Did you try the docs or Google..?

Private Function addNumbers(ByVal num1 As String, Optional ByVal num2 As String = "")

End Function


But I assume you might want to pass your function a default value..?

Dim total As Double = addNumbers(TextBox1.Text, TextBox2.Text)


so you need to split this up:

Dim total As Double
dim txt1Value As String 'why String?
dim txt2Value As String
'assign txt1Value as either the textbox-text, or 
'something else. etc.

This post has been edited by andrewsw: 04 January 2013 - 02:51 PM

Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: If Textbox is left Empty Then Set it to Default Value

Posted 04 January 2013 - 02:54 PM

You need this as well:

If String.IsNullOrEmpty(TextBox1.Text) Then

Was This Post Helpful? 0
  • +
  • -

#5 billy_welsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 04-December 12

Re: If Textbox is left Empty Then Set it to Default Value

Posted 04 January 2013 - 02:55 PM

Or just add to the first part of the click event.
        If TextBox1.Text = "" Then
            TextBox1.Text = 0
        End If
        If TextBox2.Text = "" Then
            TextBox2.Text = 0
        End If


Was This Post Helpful? 0
  • +
  • -

#6 mgaines2  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 06-December 12

Re: If Textbox is left Empty Then Set it to Default Value

Posted 04 January 2013 - 03:10 PM

View Postandrewsw, on 04 January 2013 - 02:47 PM, said:

Did you try the docs or Google..?

Private Function addNumbers(ByVal num1 As String, Optional ByVal num2 As String = "")

End Function


But I assume you might want to pass your function a default value..?

Dim total As Double = addNumbers(TextBox1.Text, TextBox2.Text)


so you need to split this up:

Dim total As Double
dim txt1Value As String 'why String?
dim txt2Value As String
'assign txt1Value as either the textbox-text, or 
'something else. etc.

I made it a string because I am simultaneously removing all non-numeric characters from it... Also, your stuff was correct, thanks man :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1