C#中的复选框编码 [英] Check Box coding in C#

查看:131
本文介绍了C#中的复选框编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢。



但我想要



设备1开关



当我选择CheckBox(ON)时,CheckBox(ON)隐藏但CheckBox(OFF)无隐藏

之后我选择CheckBox(OFF),CheckBox(OFF)隐藏,但是CheckBox(ON)没有隐藏(回来)。



当我选择CheckBox(OFF)时,CheckBox(OFF)是隐藏但是CheckBox(ON)没有隐藏

之后我选择CheckBox(ON),CheckBox(ON)隐藏,但CheckBox(OFF)没有隐藏(回来)。



我只想隐藏我选择的内容。



非常感谢。



请告诉我怎么样要做。

请给我建议。



谢谢

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

推荐答案

处理CheckChanged事件,并将Visible属性设置为false:

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







谢谢你的帮助。

当我选择一个复选框时,一个是隐藏。

当我选择另一个时,那个也会再次隐藏。



但我只想隐藏一个。(我的意思是当我选中复选框(1)时,复选框(1)隐藏;当我选择复选框(2)时,复选框(2)隐藏但复选框(1)没有隐藏。




如果这就是你想要的,那么在你的问题中解释一下!你觉得我们能读懂你的想法吗? ?:笑:

所以,你需要两个:而不是一个事件处理程序:




"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:

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





删除了虚假的_CheckChanged位......:O - OriginalGriff [/ edit]



[edit]removed the spurious "_CheckChanged" bits... :O - OriginalGriff [/edit]


<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);
            }
        }
    }
}



这里有两种解决方案:)



here are both solutions :)

// 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;
    }
}


这篇关于C#中的复选框编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆