问题描述
大家好,我确实有一个愚蠢的问题,或一个我无法解决的问题(我真的不知道为什么,这实际上是基础知识).
因此,我有两个文本框.在每个文本框的侧面/末端都有一个设置为Visible:false的标签.
当在文本框中输入错误时,必须显示此标签.
还有一个简单的查询,我可以从中获取值以在文本框中进行比较.
像这样:
Hi guys, I really have a silly question, or problem- which I really can`t solve (I really don`t know why, it`s actually basics).
So, I have two textboxes. On the side/end of each textbox there is a label which is set Visible:false.
This label has to be shown when the false input is in the textbox.
There is also one simple query from which I take values to compare in textboxes.
So it go like this:
if(txt_name!=query.Name) && (txt_lastname!=query.LastName)
{
lbl_nameError.Visible=true;
lbl_lastNameError.Visible=true;
}
如果两个值都是正确的/不正确的,那么它就完美地工作了.
一件事正确而另一件事不正确时,我会遇到问题.
你能帮我吗?
我已经尝试过了,但是标签混乱(如果值正确,标签仍然存在.)
It works just perfect, if both of values are correct /incorrect.
I have a problem when one thing is correct and the other incorrect.
Can you please help me?
I`ve tried like this, but there is a mess up with labels (if the value is correct, the label is still there..)
if(queryUsers!=null)
{
if(txt_name!=query.Name) && (txt_lastname!=query.LastName) //if both of the values are incorrect, make both of labels visible
{
lbl_nameError.Visible=true;
lbl_lastNameError.Visible=false;
}
if(txt_name!=query.Name) //if only name is incorrect, make only name label visible and lastname not visible
{
lbl_nameError.Visible=true;
lbl_lastNameError.Visible=true;
}
if(txt_lastname!=query.LastName) //if only lastname is incorrect, make only lastname label visible and name not visible
{
lbl_lastNameError.Visible=true;
lbl_nameError.Visible=false;
}
}
我想念的是什么?
What am I missing?
推荐答案
lbl_nameError.Visible = txt_name != query.Name;
lbl_lastNameError.Visible = txt_lastname != query.LastName;
但是,如果要使用if:
But if you want to use an if:
if (txt_name!=query.Name)
{
lbl_nameError.Visible = true;
}
else
{
lbl_nameError.Visible = false;
}
if (txt_lastname != query.LastName)
{
lbl_lastNameError.Visible = true;
}
else
{
lbl_lastNameError.Visible = false;
}
if(txt_name!=query.Name) && (txt_lastname!=query.LastName) //if both of the values are incorrect, make both of labels visible
{
lbl_nameError.Visible=true;
lbl_lastNameError.Visible=false;
}
else if(txt_name!=query.Name) //if only name is incorrect, make only name label visible and lastname not visible
{
lbl_nameError.Visible=true;
lbl_lastNameError.Visible=true;
}
else if(txt_lastname!=query.LastName) //if only lastname is incorrect, make only lastname label visible and name not visible
{
lbl_lastNameError.Visible=true;
lbl_nameError.Visible=false;
}
正如评论中指出的那样,与您的评论相比,您的逻辑也是错误的.我既未纠正该错误,也未尝试确切了解您想要的逻辑(您应更正错误,以便我们更容易理解).我认为您的案例作为else if
而不是连续的if更为有意义,这可能会导致您的问题(例如,第二个if消除了第一个的设置)
As was pointed out in the comment your logic is also wrong compared to your comments. I did not correct that nor try to understand exactly the logic you are wanting (you should correct the error so it is easier for us to understand). I am thinking your case makes more sense as an else if
versus continual if, which could be causing your problems (e.g. the second if eliminates the settings of the first)
if(queryUsers!=null)
{
lbl_nameError.Visible = txt_name!=query.Name;
if(!lbl_nameError.Visible)
lbl_lastNameError.Visible = txt_lastname!=query.LastName;
}
这篇关于愚蠢的问题:IF声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!