Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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:

C#
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..)

C#
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?
Posted
Comments
[no name] 28-Jun-12 13:10pm    
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=false;
}
ellisbay 28-Jun-12 13:12pm    
That was mistake while I was writing.. sadly, that`s not the problem..

You don't even need an if here:
C#
lbl_nameError.Visible = txt_name != query.Name;
lbl_lastNameError.Visible = txt_lastname != query.LastName;


But if you want to use an if:

C#
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;
}
 
Share this answer
 
Seems like you should be using else if. Not if -> if.

You will override any logic in later if statements.

C#
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;
}


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)
 
Share this answer
 
v3
Hello,

Try this:

C#
if(queryUsers!=null)
{
    lbl_nameError.Visible = txt_name!=query.Name;
       if(!lbl_nameError.Visible)
          lbl_lastNameError.Visible = txt_lastname!=query.LastName;
}
 
Share this answer
 
You were working way too hard:

C#
if(queryUsers!=null)
{
  lbl_lastNameError.Visible=txt_lastname!=query.LastName;
  lbl_nameError.Visible=txt_name!=query.Name;
}
 
Share this answer
 
Comments
ellisbay 29-Jun-12 3:34am    
Works only with one textbox :/
I really don`t know where is a problem.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900