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?
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
add a comment |
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
4
Wrap the last two statements with bracesand
. At the moment, it is always executing
label2.show()
– JayV
Mar 7 at 15:44
there is nolabel2
in that code
– Make StackOverflow Good Again
Mar 7 at 15:45
One-linerlabel2.Visible = !(label1.Visible = textBox1.Text == "test");
– Reniuz
Mar 7 at 15:57
add a comment |
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
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
c# winforms if-statement
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 bracesand
. At the moment, it is always executing
label2.show()
– JayV
Mar 7 at 15:44
there is nolabel2
in that code
– Make StackOverflow Good Again
Mar 7 at 15:45
One-linerlabel2.Visible = !(label1.Visible = textBox1.Text == "test");
– Reniuz
Mar 7 at 15:57
add a comment |
4
Wrap the last two statements with bracesand
. At the moment, it is always executing
label2.show()
– JayV
Mar 7 at 15:44
there is nolabel2
in that code
– Make StackOverflow Good Again
Mar 7 at 15:45
One-linerlabel2.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
add a comment |
2 Answers
2
active
oldest
votes
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();
1
thank you my friend, sorry for my stupid question
– VR46_doctor
Mar 7 at 15:50
add a comment |
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 showlabel1
, hidelabel2
. - If the text in
textbox1
is not "test" then hidelabel1
. - 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)
1
Very good explanation. Much better than I would've written.
– JayV
Mar 9 at 21:27
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
1
thank you my friend, sorry for my stupid question
– VR46_doctor
Mar 7 at 15:50
add a comment |
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();
1
thank you my friend, sorry for my stupid question
– VR46_doctor
Mar 7 at 15:50
add a comment |
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();
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();
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
add a comment |
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
add a comment |
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 showlabel1
, hidelabel2
. - If the text in
textbox1
is not "test" then hidelabel1
. - 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)
1
Very good explanation. Much better than I would've written.
– JayV
Mar 9 at 21:27
add a comment |
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 showlabel1
, hidelabel2
. - If the text in
textbox1
is not "test" then hidelabel1
. - 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)
1
Very good explanation. Much better than I would've written.
– JayV
Mar 9 at 21:27
add a comment |
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 showlabel1
, hidelabel2
. - If the text in
textbox1
is not "test" then hidelabel1
. - 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)
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 showlabel1
, hidelabel2
. - If the text in
textbox1
is not "test" then hidelabel1
. - 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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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