在复选框选择方面需要帮助 [英] Need help in checkbox selection

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

问题描述

大家好

我在应用程序中使用了3个复选框,我想一次选择一个复选框.请告诉我该怎么做..
我正在使用VB.NET 08和SQL Server 05 ...

Hi all

I am using 3 checkboxes in my application and i want to select one checkbox at a time.please tell me how to do it..
i am using VB.NET 08 and SQL server 05...

推荐答案

将不同的选择集分组到单独的组框(或面板或其他容器控件中,但是组框可能就是您想要的).
请参考如何:将Windows窗体单选按钮控件分组以作为一组功能 [^ ]
或者您可以尝试以下操作:
Group the different choice sets in separate group boxes (or panels, or other container controls, but group boxes are probably what you''re after).
Refer How to: Group Windows Forms RadioButton Controls to Function as a Set[^]
Or you can try this:
//Assign same checked change event for all the CheckBoxes.
private List<CheckBox> _CheckBoxGroup = new List<CheckBox>();
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    if (chk.Checked)
    {
        foreach(CheckBox other in _CheckBoxGroup)
        {
            if (other == chk)
            {
                continue;
            }
            other.Checked = false;
        }
    }
}




--Amit




--Amit




检查这些链接.

CheckedListBox类 [ ^ ]
CheckedListBox.SelectionMode属性 [


check these links.

CheckedListBox Class[^]
CheckedListBox.SelectionMode Property [^]

hope this helps.




参见此VB.NET代码:
Hi,

see this VB.NET code:
Private Sub checkBox_CheckedChanged(sender As Object, e As EventArgs)
    If DirectCast(sender, CheckBox).Checked = False Then
        Return
    End If

    For Each ctrl As Control In DirectCast(sender, CheckBox).Parent.Controls
        If TypeOf ctrl Is CheckBox AndAlso ctrl <> sender Then
            DirectCast(ctrl, CheckBox).Checked = False
        End If
    Next
End Sub


您只需要向上述代码中的三个复选框添加事件处理程序即可,这类似于Amy的解决方案,但是无需收集复选框即可工作.
使用它来添加事件处理程序:


You just need to add event handlers for your three checkboxes to the above code which is similar to the solution of Amy, but works without having a collection of checkboxes.
Use this for adding the event handlers:

Me.checkBox1.CheckedChanged += New System.EventHandler(Me.checkBox_CheckedChanged)
Me.checkBox2.CheckedChanged += New System.EventHandler(Me.checkBox_CheckedChanged)
Me.checkBox3.CheckedChanged += New System.EventHandler(Me.checkBox_CheckedChanged)


这篇关于在复选框选择方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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