How to show/hide label with if and else2019 Community Moderator ElectionHow do I perform an IF…THEN in an SQL SELECT?How do I enumerate an enum in C#?How do I update the GUI from another thread?How do I call an event method in c#?Add new text to labelif else statement in AngularJS templatesWinForms Setting A label Forecolour when the next TextBox Control is focusedHow to set asp.net 's label 's text using javascript and get setted value on server sideHow to transfer info from textboxes into a listbox in another formHow to disable buttons when TextBox is empty?

How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?

Fastest way to pop N items from a large dict

Different outputs for `w`, `who`, `whoami` and `id`

Brexit - No Deal Rejection

World War I as a war of liberals against authoritarians?

Why is there is so much iron?

Simplify an interface for flexibly applying rules to periods of time

What did “the good wine” (τὸν καλὸν οἶνον) mean in John 2:10?

Why is the President allowed to veto a cancellation of emergency powers?

Custom alignment for GeoMarkers

Are ETF trackers fundamentally better than individual stocks?

Are all passive ability checks floors for active ability checks?

PTIJ: Who should I vote for? (21st Knesset Edition)

Is it true that good novels will automatically sell themselves on Amazon (and so on) and there is no need for one to waste time promoting?

Why is a white electrical wire connected to 2 black wires?

Why one should not leave fingerprints on bulbs and plugs?

Is there a place to find the pricing for things not mentioned in the PHB? (non-magical)

Are Roman Catholic priests ever addressed as pastor

Book: Young man exiled to a penal colony, helps to lead revolution

About the actual radiative impact of greenhouse gas emission over time

Non-trivial topology where only open sets are closed

Could this Scherzo by Beethoven be considered to be a fugue?

What is the relationship between relativity and the Doppler effect?

Instead of a Universal Basic Income program, why not implement a "Universal Basic Needs" program?



How to show/hide label with if and else



2019 Community Moderator ElectionHow do I perform an IF…THEN in an SQL SELECT?How do I enumerate an enum in C#?How do I update the GUI from another thread?How do I call an event method in c#?Add new text to labelif else statement in AngularJS templatesWinForms Setting A label Forecolour when the next TextBox Control is focusedHow to set asp.net 's label 's text using javascript and get setted value on server sideHow to transfer info from textboxes into a listbox in another formHow to disable buttons when TextBox is empty?










0















I'm a student and I'm experimenting with c# (I'm noob)
My code:



private void button1_Click(object sender, EventArgs e)

if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else
label1.Hide();
label2.Show();



My problem is this:
I have two labels, one button and one textbox.



When the TextBox equals "test" if I click on it, I want to show label1 and hide label2.



If the TextBox doesn't contain "test" I want to hide label1 and show label2.



Now when I press the button without the text "test":
label2 shows and label1 is hidden



But if the text is "test" label1 shows and label2 still shows, why?










share|improve this question



















  • 4





    Wrap the last two statements with braces and . At the moment, it is always executing label2.show()

    – JayV
    Mar 7 at 15:44












  • there is no label2 in that code

    – Make StackOverflow Good Again
    Mar 7 at 15:45











  • One-liner label2.Visible = !(label1.Visible = textBox1.Text == "test");

    – Reniuz
    Mar 7 at 15:57















0















I'm a student and I'm experimenting with c# (I'm noob)
My code:



private void button1_Click(object sender, EventArgs e)

if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else
label1.Hide();
label2.Show();



My problem is this:
I have two labels, one button and one textbox.



When the TextBox equals "test" if I click on it, I want to show label1 and hide label2.



If the TextBox doesn't contain "test" I want to hide label1 and show label2.



Now when I press the button without the text "test":
label2 shows and label1 is hidden



But if the text is "test" label1 shows and label2 still shows, why?










share|improve this question



















  • 4





    Wrap the last two statements with braces and . At the moment, it is always executing label2.show()

    – JayV
    Mar 7 at 15:44












  • there is no label2 in that code

    – Make StackOverflow Good Again
    Mar 7 at 15:45











  • One-liner label2.Visible = !(label1.Visible = textBox1.Text == "test");

    – Reniuz
    Mar 7 at 15:57













0












0








0








I'm a student and I'm experimenting with c# (I'm noob)
My code:



private void button1_Click(object sender, EventArgs e)

if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else
label1.Hide();
label2.Show();



My problem is this:
I have two labels, one button and one textbox.



When the TextBox equals "test" if I click on it, I want to show label1 and hide label2.



If the TextBox doesn't contain "test" I want to hide label1 and show label2.



Now when I press the button without the text "test":
label2 shows and label1 is hidden



But if the text is "test" label1 shows and label2 still shows, why?










share|improve this question
















I'm a student and I'm experimenting with c# (I'm noob)
My code:



private void button1_Click(object sender, EventArgs e)

if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else
label1.Hide();
label2.Show();



My problem is this:
I have two labels, one button and one textbox.



When the TextBox equals "test" if I click on it, I want to show label1 and hide label2.



If the TextBox doesn't contain "test" I want to hide label1 and show label2.



Now when I press the button without the text "test":
label2 shows and label1 is hidden



But if the text is "test" label1 shows and label2 still shows, why?







c# winforms if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 18:33









d219

1,33911321




1,33911321










asked Mar 7 at 15:42









VR46_doctorVR46_doctor

112




112







  • 4





    Wrap the last two statements with braces and . At the moment, it is always executing label2.show()

    – JayV
    Mar 7 at 15:44












  • there is no label2 in that code

    – Make StackOverflow Good Again
    Mar 7 at 15:45











  • One-liner label2.Visible = !(label1.Visible = textBox1.Text == "test");

    – Reniuz
    Mar 7 at 15:57












  • 4





    Wrap the last two statements with braces and . At the moment, it is always executing label2.show()

    – JayV
    Mar 7 at 15:44












  • there is no label2 in that code

    – Make StackOverflow Good Again
    Mar 7 at 15:45











  • One-liner label2.Visible = !(label1.Visible = textBox1.Text == "test");

    – Reniuz
    Mar 7 at 15:57







4




4





Wrap the last two statements with braces and . At the moment, it is always executing label2.show()

– JayV
Mar 7 at 15:44






Wrap the last two statements with braces and . At the moment, it is always executing label2.show()

– JayV
Mar 7 at 15:44














there is no label2 in that code

– Make StackOverflow Good Again
Mar 7 at 15:45





there is no label2 in that code

– Make StackOverflow Good Again
Mar 7 at 15:45













One-liner label2.Visible = !(label1.Visible = textBox1.Text == "test");

– Reniuz
Mar 7 at 15:57





One-liner label2.Visible = !(label1.Visible = textBox1.Text == "test");

– Reniuz
Mar 7 at 15:57












2 Answers
2






active

oldest

votes


















1














You forgot braces in the else statement:



private void button1_Click(object sender, EventArgs e)

if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();
label2.Show();







share|improve this answer


















  • 1





    thank you my friend, sorry for my stupid question

    – VR46_doctor
    Mar 7 at 15:50


















3














As stated by JayV you are missing curly brackets. The reason that label1 will still show though (if you have the text "test") is because without brackets the next statement will (and only will) be executed as part of the Else (see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else) and anything after that immediate next statement is not part of the if-else i.e. what you have written is:



  • If the text in textbox1 is "test" then show label1, hide label2.

  • If the text in textbox1 is not "test" then hide label1.

  • Regardless of what the text is the code will run the final label2.show.

Effectively you've written



 if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();


label2.Show();


When you want:



 if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();
label2.Show();



I highly recommend you look at debugging, as stepping through your code would quickly show you this (see https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2017)






share|improve this answer




















  • 1





    Very good explanation. Much better than I would've written.

    – JayV
    Mar 9 at 21:27










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047674%2fhow-to-show-hide-label-with-if-and-else%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You forgot braces in the else statement:



private void button1_Click(object sender, EventArgs e)

if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();
label2.Show();







share|improve this answer


















  • 1





    thank you my friend, sorry for my stupid question

    – VR46_doctor
    Mar 7 at 15:50















1














You forgot braces in the else statement:



private void button1_Click(object sender, EventArgs e)

if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();
label2.Show();







share|improve this answer


















  • 1





    thank you my friend, sorry for my stupid question

    – VR46_doctor
    Mar 7 at 15:50













1












1








1







You forgot braces in the else statement:



private void button1_Click(object sender, EventArgs e)

if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();
label2.Show();







share|improve this answer













You forgot braces in the else statement:



private void button1_Click(object sender, EventArgs e)

if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();
label2.Show();








share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 15:46









DenseCrabDenseCrab

1,001520




1,001520







  • 1





    thank you my friend, sorry for my stupid question

    – VR46_doctor
    Mar 7 at 15:50












  • 1





    thank you my friend, sorry for my stupid question

    – VR46_doctor
    Mar 7 at 15:50







1




1





thank you my friend, sorry for my stupid question

– VR46_doctor
Mar 7 at 15:50





thank you my friend, sorry for my stupid question

– VR46_doctor
Mar 7 at 15:50













3














As stated by JayV you are missing curly brackets. The reason that label1 will still show though (if you have the text "test") is because without brackets the next statement will (and only will) be executed as part of the Else (see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else) and anything after that immediate next statement is not part of the if-else i.e. what you have written is:



  • If the text in textbox1 is "test" then show label1, hide label2.

  • If the text in textbox1 is not "test" then hide label1.

  • Regardless of what the text is the code will run the final label2.show.

Effectively you've written



 if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();


label2.Show();


When you want:



 if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();
label2.Show();



I highly recommend you look at debugging, as stepping through your code would quickly show you this (see https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2017)






share|improve this answer




















  • 1





    Very good explanation. Much better than I would've written.

    – JayV
    Mar 9 at 21:27















3














As stated by JayV you are missing curly brackets. The reason that label1 will still show though (if you have the text "test") is because without brackets the next statement will (and only will) be executed as part of the Else (see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else) and anything after that immediate next statement is not part of the if-else i.e. what you have written is:



  • If the text in textbox1 is "test" then show label1, hide label2.

  • If the text in textbox1 is not "test" then hide label1.

  • Regardless of what the text is the code will run the final label2.show.

Effectively you've written



 if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();


label2.Show();


When you want:



 if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();
label2.Show();



I highly recommend you look at debugging, as stepping through your code would quickly show you this (see https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2017)






share|improve this answer




















  • 1





    Very good explanation. Much better than I would've written.

    – JayV
    Mar 9 at 21:27













3












3








3







As stated by JayV you are missing curly brackets. The reason that label1 will still show though (if you have the text "test") is because without brackets the next statement will (and only will) be executed as part of the Else (see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else) and anything after that immediate next statement is not part of the if-else i.e. what you have written is:



  • If the text in textbox1 is "test" then show label1, hide label2.

  • If the text in textbox1 is not "test" then hide label1.

  • Regardless of what the text is the code will run the final label2.show.

Effectively you've written



 if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();


label2.Show();


When you want:



 if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();
label2.Show();



I highly recommend you look at debugging, as stepping through your code would quickly show you this (see https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2017)






share|improve this answer















As stated by JayV you are missing curly brackets. The reason that label1 will still show though (if you have the text "test") is because without brackets the next statement will (and only will) be executed as part of the Else (see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else) and anything after that immediate next statement is not part of the if-else i.e. what you have written is:



  • If the text in textbox1 is "test" then show label1, hide label2.

  • If the text in textbox1 is not "test" then hide label1.

  • Regardless of what the text is the code will run the final label2.show.

Effectively you've written



 if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();


label2.Show();


When you want:



 if (textBox1.Text == "test")

label1.Show();
label2.Hide();

else

label1.Hide();
label2.Show();



I highly recommend you look at debugging, as stepping through your code would quickly show you this (see https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2017)







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 7 at 16:53

























answered Mar 7 at 16:03









d219d219

1,33911321




1,33911321







  • 1





    Very good explanation. Much better than I would've written.

    – JayV
    Mar 9 at 21:27












  • 1





    Very good explanation. Much better than I would've written.

    – JayV
    Mar 9 at 21:27







1




1





Very good explanation. Much better than I would've written.

– JayV
Mar 9 at 21:27





Very good explanation. Much better than I would've written.

– JayV
Mar 9 at 21:27

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047674%2fhow-to-show-hide-label-with-if-and-else%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived