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.

1
  • 1
    You should post your code and provide more details please. Commented Jun 14, 2018 at 0:39

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome bud! BTW. this is not my code just sharing it. Cheers!!
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

3 Comments

Will try it, Thanks for the feedback
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.
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.
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

Comments

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?

1 Comment

Also, it sounds like you need to learn more on the difference of variable types. So please do some research on that.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.