'Default number in a text box

Foreword: This is code for a game.

On one of the forms on my VB program I have a large number of text boxes, the issue is regarding the following code:

Total_Gold.Text = (WoodPrice * TxtBoxWoodUnits.Text) + (MagicWoodPrice * TxtBoxMagicWoodUnits.Text) + (Pheonix_EggsPrice * TxtBoxPheonixEggsUnits.Text) + (MetalPrice * TxtBoxMetalUnits.Text) + (Dwarven_GemsPrice * TxtBoxDwarvenGemsUnits.Text) + (LeatherPrice * TxtBoxLeatherUnits.Text) + (Dragon_ScalesPrice * TxtBoxDragonScalesUnits.Text) + (Raw_SilverPrice * TxtBoxRawSilverUnits.Text) + (Raw_GoldPrice * TxtBoxRawGoldUnits.Text) + (DownPrice * TxtBoxDownUnits.Text) + (CottonPrice * TxtBoxCottonUnits.Text) + (QuicksilverPrice * TxtBoxQuicksilverUnits.Text) + (StonePrice * TxtBoxStoneUnits.Text) + (CoalPrice * TxtBoxCoalUnits.Text) + (ThreadPrice * TxtBoxThreadUnits.Text) + (FurPrice * TxtBoxFurUnits.Text)

This is contained in a timer event that shows how much it would cost the player to purchase all of the items. This works as intended, until the text box is blank. Each "section" draws information from one of 16 text boxes.

My question is this: Is it possible to have each text box to have a "default" number that gets put into a text box when it is empty.

This is needed to be done for all of the text boxes, so either a loop or a non specific code slice would be preferred.



Solution 1:[1]

The short answer is no.

The long answer is yes, you can.

Create your own textbox that inherits from the TextBox class, adding a property called "DefaultValue" Override the appropriate property/method adding logic for the default value.

Then simply replace the standard textbox with your textbox.

I hope you are validating the input as well because if TxtBoxWoodUnits.Text = "Foo" and you multiply it by 4.5, it's not gonna be pretty. You should turn on Error notifications for Implicit conversions as well, so you get compiler warnings when you try to multiply strings by numbers.

Public Class MyTextBox
     Inherits TextBox

     Public Property DefaultValue As String


     Public Overrides Property Text As String
        Get
            If String.IsNullOrWhiteSpace(MyBase.Text) Then
                Return DefaultValue 
            Else
                Return MyBase.Text
            End If
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
     End Property

End Class

Solution 2:[2]

Well in the designer you could put the text value as "0", then in the textchanged event handler do this:

If TxtBoxWoodUnits.Text = "" Then
   TxtBoxWoodUnits.Text = "0"
EndIf

Basically everytime the text of your textbox is changed, it checks if it is null, if it is, then make the value of textbox "0".

Hope this helps.

Solution 3:[3]

Private Sub btn_generate_text_array_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ' will generate a group of 10 text boxes
    Dim j As Integer
    For j = 0 To 10
        ReDim Preserve c(j)
        c(j) = New TextBox
        c(j).Name = "txt" & j
        c(j).Parent = Me
        c(j).Top = j * c(j).PreferredHeight + 2
        c(j).Tag = j
        c(j).Visible = True
    Next
End Sub

Private Sub c_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    For j = 0 To 10 'loop will load 0 to all the text boxes having ""(null) values
        If c(j).Text = "" Then
            c(j).Text = "0"
        End If
    Next
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 user959631
Solution 3