Checking for a null entry in a textbox

Lets say I have some code and I want to check to see if the user has not entered anything into a textbox; how do I do this In order for some code to work the textbox should contain a number, however, the user may not have entered the textbox yet. The value of the textbox is used in a calculation so I need to be sure the textbox does not contain a null.

I have tried a couple of differnt lines of code to check for a null value but they have not worked. What is the right syntax



Answer this question

Checking for a null entry in a textbox

  • MAPIdoneit

    Hope you will bear with me.

    >in c# you can assign a textbox's text to null but it doesn't actually stay a null value.

    I am not trying to assign a textbox's text to a null. I am trying to find out if it is already null. I assume if no data has been entered into it, such is the case. How do I do this

    Are you saying that if noone has yet entered data into the textbox that it will = "" anyway.

    If so, fine. I don't understand why and I would appreaciate it if someone would explain it to me, but if this is the case at least that solves the problem.




  • GhadaKamel

    in c# you can assign a textbox's text to null but it doesn't actually stay a null value.   The property will assign the text to be "" if it's assigned to null.  Also "" is not equal to null

    textBox1.Text = null;

    if( textbox1.Text == null)
    // will not ever hit this...
    else if ( textbox.Text == "")
    // instead it will be an empty string and hit this

    The only way to check if someone has typed something in is to write code to handle the TextChanged event or the Validating event if checking if the text == "" is not good enough.

  • Jster

    Like Alan says:

     

    You cannot check textBox1.Text for being a true NULL.

     

    textBox1.Text is a String and String types can never be NULL.

     

    Behind-the-scenes, a String is just a sequence of Unicode characters.

     

    An “empty” textBox1.Text is a string that looks empty or null but it really has a 1 character sequence, /0.

     

    Your quote-quote(“”) is also a string that looks empty or null, but it also really has a 1 character sequence, /0.

     

    So, when you do this: if (textBox1.Text == “”),  the system will see that both textBox1.Text and “” really contain the same character sequence(/0), and will report to you that they are equal.


  • JainNitin

    So you're saying that this is something that C# handles differently that VB. If so, That's fine. I'm not complaining, just need to understand.

    To summarize: When a textbox is put on a form and you run the form, if no value has been entereed into the textbox and you check for its value it is not going to be null, but rather "". Is this correct


  • Gutenberg

    You could try String.IsNullOrEmpty(<string>). Works for me anyway

    Example
    if(String.IsNullOrEmpty(TextBox.Text))
    ...do something interesting...

    http://msdn2.microsoft.com/en-us/library/system.string.isnullorempty(d=ide).aspx


  • SonaliC

    Does C# consider "" as a null In VB you can move to a text box, put something in it, take it out again and the value of the textbox will be "". But if you've never put data into the textbox at all it is considered a null and its value is something else (nothing as I remember it or maybe it's dbNull, can't remember).

    In C# does "StrValue = textbox1.text" check for a true null or an empty string


  • D_S_Clark

    This is correct. When a textbox is created (textbox.Text == "") is true. It is different from VB.

    The only reason I showed the assignment was to show you that the string can never be null, and was supposed to also imply at creation.

  • mohammedAhmed_1978

    hi am new to vb and I was wondering about this as well I basically want to make it so that when the user clicks a radio button and the text box has no text nothing happens. if there is text in the text box everything works as it should.  here is the code

     


    Public Class Form1

    Private Sub RadioButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.Click

     If Me.RadioButton1.Checked Then PictureBox1.Image = PictureBoxSunny.Image

    With Me

    With .TextBox2

    'Clear the text box

    .Clear()

    'Reset focus

    .Focus()

    End With

    .TextBox2.Clear()

    End With

    With Me

    .TextBox2.Text = .TextBox1.Text & ", today is going to be Sunny. " & .TextBox2.Text

     End With

    End Sub

     Private Sub RadioButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton2.Click

    If Me.RadioButton2.Checked Then PictureBox1.Image = PictureBoxSnowy.Image

    With Me

    With .TextBox2

    'Clear the text box

    .Clear()

    'Reset focus

    .Focus()

    End With

    .TextBox2.Clear()

    End With

     With Me

    .TextBox2.Text = .TextBox1.Text & ", today its going to Snow. " & .TextBox2.Text

     End With

    End Sub

    Private Sub RadioButton3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton3.Click

    If Me.RadioButton3.Checked Then PictureBox1.Image = PictureBoxRainy.Image

    'Clear the previous text

    With Me

    With .TextBox2

    'Clear the text box

    .Clear()

    'Reset focus

    .Focus()

    End With

    .TextBox2.Clear()

    End With

    With Me

    .TextBox2.Text = .TextBox1.Text & ", today its going to Rain. " & .TextBox2.Text

     End With

    End Sub

    Private Sub RadioButton4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton4.Click

    If Me.RadioButton4.Checked Then PictureBox1.Image = PictureBoxCloudy.Image

    'Display the name that was typed into the first textbox along with a message into the second textbox

    With Me

    With .TextBox2

    'Clear the text box

    .Clear()

    'Reset focus

    .Focus()

    End With

    .TextBox2.Clear()

    End With

     With Me

    .TextBox2.Text = .TextBox1.Text & ",today is going to be Cloudy." & .TextBox2.Text

     End With

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    With Me

    With .TextBox1

    'Clear the text box

    .Clear()

    'Reset focus

    .Focus()

    End With

    .TextBox1.Clear()

    .TextBox2.Text = ""

    End With

    End Sub

     End Class

     



  • Checking for a null entry in a textbox