如何不计算代码中的空间 [英] How to not count Space in the code

查看:90
本文介绍了如何不计算代码中的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我做了一个表格来添加具有3个基本功能的注释
1.输入笔记
2.清除按钮
3.保存按钮.
基本功能是,直到或除非我在文本框中输入注释,否则应禁用清除和保存按钮.输入注释后,应立即启用清除和保存按钮.
但是
如果我在文本框内放置空格",则也应禁用两个按钮,即不应将空格计为一个值.
我已经在下面编写了代码条件,这是我使用的粗略逻辑,并且工作正常.



I have made a form to add notes which has 3 basic fucntionalities
1.to enter notes
2.clear button
3.save button.
The basic functionality is, until or unless i enter notes in the textbox the clear and save button should be disabled.As soon as i enter notes the clear and save button should be enabled.
BUT
If i put "space"inside the textbox then also both the buttons should be disabled i.e space should not be counted as a value.
i have written a code condition below this is the rough logic i used and is working fine.


public CanClearNotes()
{
 If(AddNotes !=null)
{
(AddNotes !=null && !AddNotes.Notes.Equals(string.Empty))
}
return true;
}
return false;



注释是我们在文本框中输入的注释.


返回true将启用清除按钮(为保存按钮编写了相同的代码)

我应该进行哪些修改,以使其不将空格"作为一个值,并且如果我在开头输入空格,则应禁用两个按钮.

P.S:应该在两个单词之间启用空格.



Notes is the notes which we enter into the textbox.


return true will enable clear the button(same code is written for save button)

What modification should i make so that it does not count "Space" as a value and if i enter space in the beginning both the buttons should be disabled.

P.S: Space should be enabled in between two words.

推荐答案

尝试此操作
注意:我在示例中使用了一个文本框.

try this
Note: I used a text box in my example.

if (!textBox1.Text.Trim().Equals(String.Empty))
{
    button1.Enabled = true;
}
else
{
    //clear the whitespace from the textbox
    textBox1.Clear();
    button1.Enabled = false;
}



所以您可能想要这样的东西



so you would probably want something like this

public CanClearNotes()
{
    if(AddNotes != null && !AddNotes.Notes.Trim().Equals(String.Empty))
    {
        return true;
    }
    else
    {
        //clear the whitespace from the textbox (if AddNotes is a textBox?)
        AddNotes.Clear();
        return false;
    }
}


这是您要找的吗?

Is this what you are looking for?

public bool CanClearNotes()
{
    return !String.IsNullOrWhiteSpace(AddNotes);
}



更好



Better yet

public bool ButtonsEnabled
{
     get
     {
         return !String.IsNullOrWhiteSpace(AddNotes);
     }
}


尝试!AddNotes.Notes.Contains(" ").
这只会检查单个空格,而不是多个空格.
Try !AddNotes.Notes.Contains(" ").
This will only check for a single space, not for multiple spaces together.


这篇关于如何不计算代码中的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆