C # - Disable button if line is empty or has spaces?

I just started to learn C #.

Here is my code:

private void button1_Click(object sender, EventArgs e)
{
    object Nappi1 = ("Nice button");
    MessageBox.Show(Nappi1.ToString());
}

I got a text box that should disable button1if it is empty or empty.

I already worked at some level, but it checks the status textboxon button1_Click.

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1 = "") 
    {
        button1.enabled = false;
    }
    else 
    {
        button1.enabled = true;
        object Nappi1 = ("Nice button");
        MessageBox.Show(Nappi1.ToString());
    }
}

A fictional example:

 if (textBox1 = "" or textBox1 = whitespace[s])

  • How can I check the status textbox onLoad(as soon as the program starts)?
  • How can I check it if (several) whitespace, and can I write it to the same if-statement?

Please keep it simple.

+4
source share
6 answers

To answer exactly the title of the question, In short, clearer:

button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text);
+8

if-else , string:

if (string.IsNullOrWhiteSpace(textBox1)) {
    button1.enabled = false;
}
else {
    button1.enabled = true;
    ...
}

textBox1.Text, Textbox, :

if (string.IsNullOrWhiteSpace(textBox1.Text)) {
    button1.enabled = false;
}
else {
    button1.enabled = true;
    ...
}
+4

String.IsNullOrWhiteSpace:

if (String.IsNullOrWhiteSpace(textBox1.Text)) {
    button1.enabled = false;
}

:

if (textBox1 = "") {
button1.enabled = false;
}

textbox - , Text, . # = , ==, .

.NET 4 .NET 4.5, :

String.IsNullOrEmpty

+3

, , . :

  • ( ) / .

    private void textBox1_TextChanged(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(textBox1.Text)) button1.Enabled = false; else button1.Enabled = true; }

  • InitializeComponent

    button1.Enabled = false;

0

textBox1 , :

button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text);

string.IsNullOrWhiteSpace( " " ) , , button1.Enabled false.

0

# , .

Text null, TextBox1 null, . , Textbox .

char , . , , TextBox1.Text , length TextBox1.Text.

, . , (, ) .

If (Textbox1.Text.Length > 0) {
  // First Condition: Checking whether the whole value is a space
  Button1.Enabled = string.IsWhitespace(Textbox1.Text);
  // Alternative First Condition: Button1.Enabled = Textbox1.Text.ToList().All(Char.IsWhitespace);

  // Second Condition: Checking for the existence of spaces (whitespace characters) in the string
  Button1.Enabled = !(TextBox1.Text.ToList().Any(Char.IsWhitespace));
} else {
  Button1.Enabled = False; 
}

, (.. List ( char)) (.. All, Any, FindAll) .

, , ( ) TextBox.Text List(of string) :

List(of string) l_tb_RequiredText = {TextBox1.Text, TextBox2.Text, TextBox3.Text};

Button1.Enabled = !(l_tb_RequiredText.Any(string.IsNullEmptyWhitespace));

, - .


- () , "addressof" . ( ) #.. .

public bool hasSpaceInString(string value) {
   return !(value.ToList().Any(Char.IsWhitespace));
}

...

List(of string) l_tb_RequiredText = {TextBox1.Text, TextBox2.Text, TextBox3.Text};

Button1.Enabled = !(l_tb_RequiredText.Any(hasSpaceInString));

, , - , ( delegate). IEnumerable, . IEnumerable(of string) List(of string), delegate, .

Additionally (starting with and C # 3.0) you can use lambda expressions to create delegateand attach it to an anonymous function at runtime. http://msdn.microsoft.com/en-us/library/bb397687.aspx

-1
source

Source: https://habr.com/ru/post/1529769/


All Articles