0

I am working on winform application. i have one button and its default text is ON. now when I click on button, its text should be changed to OFF an if i again click the button. is text should be changed back to ON . i have no idea how to do so i used counter logic. but i am not sure in it and not comfortable withit.

one more thing. if text is ON then employee can receive notification. so accept_notf = 1 and if text is OFF employee can not receive notification. so accept_notf =0

i have tried this.:

private void btnNotification_Click(object sender, EventArgs e)
{
    var btn = sender as Button;
    if (btn != null)
    {
        if (btn.Text == "ON")
        {
            accept_notif = "1";
            btn.Text = "OFF";
        }
        else  (btn.Text == "OFF")
        {
            accept_notif = "0";
            btn.Text = "ON";
        }
    }
}
3
  • Question: what purpose does that loop achieve? Can you explain the code you wrote? Commented Jun 24, 2015 at 9:01
  • i am trying that, if odd count then text will be "OFF" . any other idea sir? Commented Jun 24, 2015 at 9:02
  • What's the problem with it? Commented Jun 24, 2015 at 9:20

3 Answers 3

1

Set default text to "ON", When the button is clicked if the text is "ON" set it to "OFF" else set it to "ON"

private void btnNotification_Click(object sender, EventArgs e)
{
    var btn = sender as Button;
    if (btn != null)
    {
        if (btn.Text == "ON")
        {
            btn.Text = "OFF";
            accept_notif = "0";
        }
        else if (btn.Text == "OFF")
        {
            btn.Text = "ON";
            accept_notif = "1";
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

this is working. i have edited my question sir. and additional portion is not working
Why is the else if necessary? Wouldn't a simple else be enough?
@Mitulátbáti because it must be switched between "ON" and "OFF" if somewhere else in code set it to something else it should not overwrite that value.
One liner, for those that care: btnNotification.Text = btnNotification.Text == "ON" ? "OFF" : "ON";
1

It's simple. First, in our event handler we get our button instance from event arguments and Downcast it back to Button type. Then we use conditional operator to find out is it set to "ON" and if it's so we set it to "OFF" otherwise (operator ':') we set it to "ON"

private void btnNotification_Click(object sender, EventArgs e)
{
    var button = sender as Button;
    if (button == null) return;       
    button.Text = button.Text == "ON"? "OFF" : "ON";
    accept_notf = button.Text == "ON"? 1 : 0;
}

6 Comments

this is a code only answer, try add some explanation to it. try change "On" to "ON"
It's almost the same question ... Just added a line and updated again
btn not in current context. some changes are required sir. otherwise code is cool.
Tell me please what is not covered ? What do you mean by 'not in current context' ?
just remove btn.Text
|
-1

In your click handler method you should examine if the text of your button is "ON" or "OFF" (because the changes depend on this).

For example like this:

if ((sender as Button).Text == "ON")
    (sender as Button).Text = "OFF";
else
    (sender as Button).Text = "ON";

...
or you can create a variable and work with that:
Button tmpButton = (sender as Button);

or you can use directly the button itself for accessing its text (I prefer this way when there is only 1 button).

if (btnStatusIndicator.Text == "ON")
{
    btnStatusIndicator.Text = "OFF";
    acceptNotif = 0; // learn camelCase when you write C# code! :)
}
else
{
    btnStatusIndicator.Text = "ON";
    acceptNotif = 1;
}

and if the program allows more values than just ON or OFF, you can use switch statement to determine the text (with this you can prepare your program to deal with other situations, when the text is not either ON or OFF).

switch (btnStatusIndicator.Text)
{
    case "OFF": ... break;
    case "ON": ... break;
    case "your other value": ... break;
    case "your other value": ... break;
    case "your other value": ... break;
    default: ... break;
}

5 Comments

what if sender as Button becomes null?
An exception, sir. But that's not gonna happen ;)
if you assign this function to a Label click event then there will.
And you are trying to cast an object to Button three times. You can do that only once
Well don't assign the function to a Label... we're talking about a button's event handler where the sender definitely will be a Button. Your second comment: That's true. But that wouldn't change anything...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.