Click here to Skip to main content
15,909,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Thanks.

But i want

Device 1 ON OFF

when i select CheckBox (ON), CheckBox(ON) is hide but CheckBox(OFF) no hide
After that i select CheckBox (OFF), CheckBox(OFF) is hide, but CheckBox(ON) no hide(come back).

when i select CheckBox (OFF), CheckBox(OFF) is hide but CheckBox(ON) no hide
After that i select CheckBox (ON), CheckBox(ON) is hide, but CheckBox(OFF) no hide(come back).

I want only one hide what i select.

Thanks a lot.

Please tell me how to do.
Please give me advice.

Thanks
Posted
Updated 24-Nov-13 1:37am
v4
Comments
Maciej Los 17-Nov-13 6:57am    
Do you want to hide curently checked Checkbox? Why?
What have you tried till now? Where are you stuck?
CHill60 24-Nov-13 7:39am    
I see you've added something to your post ... does this mean that none of the 3 solutions posted worked for you? In what way?

Handle the CheckChanged event, and set the Visible property to false:
C#
void myCheckBox_CheckedChanged(object sender, EventArgs e)
    {
    CheckBox cb = sender as CheckBox;
    if (cb != null)
        {
        cb.Visible = false;
        }
    }



"Thanks your help.
when i select one Check Box, one is hide.
when i select another one, that one also hide again.

But i want only one hide.( i mean when i select check Box(1),Check Box(1) is hide; When i select Check Box(2),Check Box(2) is hide but Check Box(1) no hide."


Well if that is what you want, then explain that in your question! Do you think we can read your mind? :laugh:
So, instead of one event handler, you need two:
C#
void leftCheckBox_CheckedChanged(object sender, EventArgs e)
    {
    CheckBox cb = sender as CheckBox;
    if (cb != null)
        {
        leftCheckBox.Visible = !cb.Checked;
        rightCheckBox.Visible = cb.Checked;
        }
    }
void rightCheckBox_CheckedChanged(object sender, EventArgs e)
    {
    CheckBox cb = sender as CheckBox;
    if (cb != null)
        {
        rightCheckBox.Visible = !cb.Checked;
        leftCheckBox.Visible = cb.Checked;
        }
    }


[edit]removed the spurious "_CheckChanged" bits... :O - OriginalGriff [/edit]
 
Share this answer
 
v3
Comments
Member 10404120 17-Nov-13 7:13am    
Thanks your help.
when i select one Check Box, one is hide.
when i select another one, that one also hide again.

But i want only one hide.( i mean when i select check Box(1),Check Box(1) is hide; When i select Check Box(2),Check Box(2) is hide but Check Box(1) no hide.
OriginalGriff 17-Nov-13 7:38am    
Answer updated
Member 10404120 18-Nov-13 8:53am    
But have error on
1. leftCheckBox_CheckedChanged.Visible = !cb.Checked;
2. rightCheckBox_CheckedChanged.Visible = !cb.Checked;

How to do ?
OriginalGriff 18-Nov-13 9:41am    
Oops! Finger trouble!
Answer updated
Member 10404120 19-Nov-13 8:51am    
Thanks.
I think answer is a bit away from what i want.
sorry ..
I mean ...

Device 1 ON OFF

when i select CheckBox (ON), CheckBox(ON) is hide but CheckBox(OFF) no hide
When i select CheckBox (OFF), CheckBox(OFF) is hide, but CheckBox(ON) no hide

Thanks a lot.
C#
<pre lang="cs">public class MyCheckBox : CheckBox
{
    private bool _checked;

    public override bool Checked { get { return _checked; } set { _checked = value; } }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        //You must tell the page that you use ControlState.
        Page.RegisterRequiresControlState(this);
    }

    protected override object SaveControlState()
    {
        //You save the base's control state, and add your property.
        object obj = base.SaveControlState();

        return new Pair (obj, _checked);
    }

    protected override void LoadControlState(object state)
    {
        if (state != null)
        {
            //Take the property back.
            Pair p = state as Pair;
            if (p != null)
            {
                base.LoadControlState(p.First);
                _checked = (bool)p.Second;
            }
            else
            {
                base.LoadControlState(state);
            }
        }
    }
}

 
Share this answer
 
here are both solutions :)

C#
// assign this function to both CheckBox events
void myCheckBox_CheckedChanged(object sender, EventArgs e)
{
    // use this code if you want to hide the checked checkbox
    ((CheckBox)sender).Visible = !((CheckBox)sender).Checked;

    // if you want to invert the hidings, use this
    checkBoxOn.Visible = true;
    checkBoxOff.Visible = true;

    switch (((CheckBox)sender).Name)
    {
        case "checkBoxOn":
            if (((CheckBox)sender).Checked)
                checkBoxOff.Visible = false;
            break;
        case "checkBoxOff":
            if (((CheckBox)sender).Checked)
                checkBoxOn.Visible = false;
            break;
    }
}
 
Share this answer
 
v4

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