windows 带复选框的下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1492405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:12:17  来源:igfitidea点击:

DropDown list with checkboxes

.netwindowsvb.netwinforms

提问by Iulian

How can i make a dropdown list with checkboxes ? I have a windows app ( vb.net) and the checkboxlist control is not real an option with the available space on my form.

如何制作带有复选框的下拉列表?我有一个 Windows 应用程序 (vb.net) 并且复选框列表控件不是我表单上可用空间的真正选项。

Thanks.

谢谢。

采纳答案by Larry Lustig

The easiest way to create this effect is to use three controls — an edit, a button (to the right of the edit, with a drop down icon) and a checkboxlist as you're using now.

创建此效果的最简单方法是使用三个控件 - 一个编辑、一个按钮(在编辑的右侧,带有一个下拉图标)和一个复选框列表,就像您现在正在使用的那样。

Position the checkboxlist under the edit so its width is the same as the edit and the button next to the edit, and make it invisible. Now, add code to the button to:

将复选框列表放在编辑下,使其宽度与编辑和编辑旁边的按钮相同,并使其不可见。现在,将代码添加到按钮以:

  1. Make the checklistbox visible if it is not visible and to make it not visible if it is visible (that is, flip the value of .Visible).

  2. Call code to create a "summarized" version of what was checked and not check and display it in the edit. For instance, if your "dropdown" contains color names, you might have your code create a comma-delimited list of the checked colors and display that in the edit.

  1. 如果不可见则使 checklistbox 可见,如果可见则使其不可见(即翻转 .Visible 的值)。

  2. 调用代码以创建已检查内容的“摘要”版本,而不是检查并在编辑中显示它。例如,如果您的“下拉列表”包含颜色名称,您可能会让您的代码创建一个以逗号分隔的已检查颜色列表,并在编辑中显示该列表。

You can take this a little further and put the checkboxlist into a panel container and add little buttons to the "dropdown" to execute whatever special functions might be appropriate in your application (check all, uncheck all, whatever).

您可以更进一步,将复选框列表放入面板容器中,并在“下拉列表”中添加一些小按钮,以执行您的应用程序中可能适用的任何特殊功能(全部选中、全部取消选中等等)。

回答by Jon Seigel

You'd have to go owner-drawn to get that kind of functionality.

您必须自行开发才能获得这种功能。

If your form is out of space, I would suggest first looking to see if you can refactor the GUI to make it simpler.

如果您的表单空间不足,我建议您首先查看是否可以重构 GUI 以使其更简单。

回答by hground

I found a simpler solution and thought I would post it if anyone searching like I was could use it. What I did was insert a single-column Listview onto my form with View set to Detail mode, MultiSelect set to True, and Checkboxes set to True. The following code will allow the list to drop down. Since I have multi-selection there is no need to display the selected value like you would do in a combo box so the drop-down appears to be a button to Show/Hide the list. In the code below I am dividing the height by 4 because my list is static and that is how many check boxes I have in my list. If you have a dynamic list then just read the number of items from the Listview and use that. You may have to play with it some to get the appearance that you want. Here are the events that I use:

我找到了一个更简单的解决方案,并认为如果有人像我一样搜索可以使用它,我会发布它。我所做的是在我的表单中插入一个单列 Listview,其中 View 设置为 Detail 模式,MultiSelect 设置为 True,Checkboxes 设置为 True。以下代码将允许下拉列表。由于我有多项选择,因此无需像在组合框中那样显示所选值,因此下拉列表似乎是一个用于显示/隐藏列表的按钮。在下面的代码中,我将高度除以 4,因为我的列表是静态的,这就是我的列表中有多少个复选框。如果您有一个动态列表,那么只需从 Listview 中读取项目数并使用它。您可能需要玩弄它以获得您想要的外观。以下是我使用的事件:

Dim iListHt As Integer 'Set global variable to save height of list

        'Save initial height of list and then collapse it to a button
        iListHt = lvList.Height
        lvList.Height = CInt(iListHt / 4)
        lvList.Columns(0).Text = "Display List"

Private Sub lvList_ColumnClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvList.ColumnClick
    Dim iHt As Integer = lvList.Height
    If (iHt < iListHt) Then
        lvList.Height = iListHt
        lvList.Columns(0).Text = "Hide List"
    Else
        lvList.Height = CInt(iHt / 4)
        lvList.Columns(0).Text = "Display List"
    End If
    lvList.Refresh()
End Sub

Private Sub lvList_ItemChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles lvList.ItemChecked
    'Read values from list check boxes and update settings
    UpdateSettings()
End Sub

The ColumnClick event handles when they click on what looks like the Show/Hide List button and the ItemChecked event lets me handle anything that needs to respond to a change in the checkboxes. The value of each is read by accessing the "lvList.Items(iRowNum).Checked" value where iRowNum is the row you want to check. Hope this helps the next person that needs something like this.

当他们单击“显示/隐藏列表”按钮时会处理 ColumnClick 事件,而 ItemChecked 事件让我可以处理需要响应复选框更改的任何内容。通过访问“lvList.Items(iRowNum).Checked”值读取每个值,其中 iRowNum 是您要检查的行。希望这可以帮助下一个需要这样的人。

hground

地面

回答by Terry_Brown

I suspect your question may well be edited as it's not very clear at present. I'm guessing you want a dropdown list control on the form, but when the list drops down you have checkboxes as the options within it? If so, I suspect you may be looking at a custom set of controls (the Telerikcontrols will allow you to template a RadComboBox for example), but I don't think there are any standard controls that will allow this.

我怀疑您的问题很可能会被编辑,因为目前还不是很清楚。我猜你想在表单上有一个下拉列表控件,但是当列表下拉时,你有复选框作为其中的选项?如果是这样,我怀疑您可能正在查看一组自定义控件(例如,Telerik控件将允许您对 RadComboBox 进行模板化),但我认为没有任何标准控件允许这样做。

回答by Abacus

Props to hground for using a ListView for this purpose. I will try to give an improved version of his concept.

为此目的使用 ListView 的 hground 道具。我将尝试给出他的概念的改进版本。

Private Shared Sub Expand(lv As ListView)
    Dim expandedHeight As Integer = 20 + (15 * lv.Items.Count)
    lv.Height = expandedHeight
End Sub
Private Shared Sub Contract(lv As ListView)
    Dim contractedHeight As Integer = 20
    lv.Height = contractedHeight
End Sub
Private Sub uiIssuesStatusListView_GotFocus(sender As Object, e As System.EventArgs) _
Handles uiIssuesStatusListView.GotFocus, uiIssuesStatusListView.ColumnClick
    Call Expand(uiThisListView)
End Sub
Private Sub uiIssuesStatusListView_LostFocus(sender As Object, e As System.EventArgs) _
Handles uiIssuesStatusListView.LostFocus, uiIssuesStatusListView.MouseLeave
    Call Contract(uiThisListView)
End Sub

You might want to tweak the above literal values based on the font size or something.

您可能希望根据字体大小或其他内容调整上述文字值。