Click here to Skip to main content
12,356,154 members (69,676 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
hii
I dont want to allow all space in textbox in c# windows applications means if i give all spaces in textbox the form should not submit

thank you
Regards
Mahesh
Posted 13-Oct-12 4:53am
Comments
Wes Aday 13-Oct-12 10:58am
   
Okay so don't allow all spaces. What is the problem?
aspnet_regiis -i 13-Oct-12 14:27pm
   
Yes.. I too don't see any problem.. Where's the question?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

hi,
here is the small code for you, write the following code under validating event procedure of the textBox.
private void mytextBox_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (string.IsNullOrWhiteSpace(tb.Text) == true)
            {
                MessageBox.Show("Your error message for users");
                e.Cancel = true;
                return;
            }
        }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Handle the TextBox.TextChanged Event.
In it:
private void myTextBox_TextChanged(object sender, EventArgs e)
    {
    TextBox tb = sender as TextBox;
    if (tb != null)
        {
        myOKButton.Enabled = !string.IsNullOrWhiteSpace(tb.Text);
        }
    }
  Permalink  
Comments
aspnet_regiis -i 13-Oct-12 14:28pm
   
@OG .. I think the tb.Text needs a .Trim()..
OriginalGriff 13-Oct-12 15:33pm
   
Nope - that's the difference between IsNullorEmpty and IsNullOrWhietspace (introduced at .NET 4.0) :D
aspnet_regiis -i 13-Oct-12 15:41pm
   
Then it deserves a 5 :)
aspnet_regiis -i 13-Oct-12 15:42pm
   
Somebody voted a nasty 1.. Sick...!!!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

For .Net Framework 3.0 and lower, you can use String.IsNullOrEmpty(string) function which returns true if the string argument is Null or is empty. But this function does not check if the string parsed contains only spaces(WhiteSpace), so you you also have to call the Trim() function of string.You can create a validate function to check your controls before remitting it...

public bool ValidateControls()
        {
            if (String.IsNullOrEmpty(myTextBox.Text))
            {
                //Tell the user that the text provided is unacceptable.
                MessageBox.Show("The content of the Textbox is not valid.");
                //Validation was unsuccessful.
                return false;
            }
            //Validation was successful.
            return true;
        }

However, if you're using .Net Framework 3.5 and higher, you should use String.IsNullOrWhiteSpace(string) function which returns true if the string argument is empty, Null or contains only spaces. So, you would not have to Trim the string again, and the above code could be rewritten as

public bool ValidateControls()
        {
            if (String.IsNullOrWhiteSpace(myTextBox.Text))
            {
                //Tell the user that the text provided is unacceptable.
                MessageBox.Show("The content of the Textbox is not valid.");
                //Validation was unsuccessful.
                return false;
            }
            //Validation was successful.
            return true;
        }
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Hi,
There are two excellent methods included in .NET framework:

1. String.IsNullOrWhiteSpace() Method[^] available starting from .NET 4.0
2. String.IsNullOrEmpty Method[^] available starting from .NET 2.0

The use of (1) is recommended, like: String.IsNullOrWhiteSpace(TextBox1.Text), though in legacy applications (prior to .NET 4.0) the other option, as specified at Microsoft MSDN (2), is to use the expression: String.IsNullOrEmpty(value) || value.Trim().Length == 0;. The latter is essentially equivalent to (1), but (1) has better performance.

I hope this will help.
Kind regards,
AB
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Though you don't provide enough information about your form.So i assume you have a form of different textboxes and also may contain a Save button to save all those info.
So you may add this to your button event..
private void button1_Click(object sender, EventArgs e)
{
    if (textBox2.Text != "")
    {
        //do your saving information code here
    }
    else
    {
        //you can provide notification for user here
        MessageBox.Show("Please fill up all textbox information");
    }
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

I believe, you should put a custom validator with regex as to now allow all space i.e. atleast one character or numeric as per your requirement. For example, the below one is asking for atleast one character and one number.

^.*(?=.{4,10})(?=.*\d)(?=.*[a-zA-Z]).*$

Or the below regex will check for any space
^[\S]*$

Now, unless the user enters at-least one non-space character, the form will not submit. This will also avoid any server trip which will be the case if you use button click, keypress or text change event.

Hope that helps
Milind
If the answer helps, mark it as solution/upvote.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

if(textbox1.Text.Trim() == "") 
{
    MessageBox.Show("Error", "Error");
}
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 25 Jun 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100