0

I am trying to execute a scenario which is Deleting the content/value of textbox and then if the textbox is empty it will become 0 automatically.

because every time I execute it it gives me an error like converting string to integer is invalid etc.

5 Answers5

1

If I understand you question correctly then this is what you need

https://stackoverflow.com/a/41890237/9651031

Dim count1 As Integer = 0
count1 = ConvertToInteger(a.Text) + ConvertToInteger(b.Text) + ConvertToInteger(c.Text)
txt_display.Text = count1




Private Function ConvertToInteger(ByRef value As String) As Integer
    If String.IsNullOrEmpty(value) Then
        value = "0"
    End If
    Return Convert.ToInt32(value)
End Function

play around with it and eventually you will get your desired result

Cremlic
  • 88
  • 9
1

In that case, trigger the TextBox_TextChanged event:

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
  if me.TextBox1.Trim.Lenght = 0 Then
    me.TextBox1.Text = "0"
  End if
End Sub
Máster
  • 981
  • 11
  • 23
  • Will try it, Thanks for the feedback – Jeffrey Oliveras Jun 14 '18 at 00:45
  • 1
    This will result on confusing behaviour for users. If The textbox has 1 digit in it and they delete it to change it to something else, a 0 will appear in the text box. If they try to delete the 0, they won't be able to. While this shouldn't affect the final number, it will be frustrating for the user. – David Wilson Jun 14 '18 at 09:26
  • At least in WebForms the zero `0` won't appear until the textbox loses focus. I don't remember if the same applies for WinForms. – Máster Jun 14 '18 at 15:30
0

Please try this

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) HandlesTextBox1.TextChanged if TextBox1.Text.Trim = "" Then TextBox1.Text = "0" End if End Sub

0

Put this on the event in which you want it to happen:

If Textbox1.text = nothing then
Textbox1.text = "0"
End if

Based on your question, I understand that you want the textbox to display the number zero whenever it is empty. Correct?

BuddyRoach
  • 162
  • 1
  • 19
  • Also, it sounds like you need to learn more on the difference of variable types. So please do some research on that. – BuddyRoach Jun 15 '18 at 05:01
0

You can check what the current text/value is when text changes or when you leave the textbox.

Then you simply do

if textbox.text="" ' or any form of empty/nothing
   textbox.text="0"
end if
CruleD
  • 1,153
  • 2
  • 7
  • 15