.NET Compact 框架 (C#):将复选框添加到列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/768157/
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-08-05 00:23:24  来源:igfitidea点击:

.NET Compact framework (C#) : Add checkbox to Listbox

c#compact-frameworklistboxcheckbox

提问by

I would like to add a checkbox control to a listbox control.

我想将复选框控件添加到列表框控件。

The listbox has to contain A couple of tasks, and I have to check if a tasks has been opened before.

列表框必须包含几个任务,我必须检查之前是否打开过任务。

I have a code sample, but it adds the checkbox as an object, not as a control

我有一个代码示例,但它将复选框添加为对象,而不是控件

while (reader.Read())   
{                    
   CheckBox c = new CheckBox().Enabled = false;
   c.Text = reader.GetString(0) + ". " + reader.GetString(1);

   try 
   { 
      if (int.Parse(reader.GetString(2)) > 1) c.Checked = true; 
   } 
   catch(Exception ex)    
   {

      MessageBox.Show(ex.Message);    
   }    
   listTasks.Items.Add(c);
}

Can someone help me out

有人可以帮我吗

Thnx, Ruben

谢谢,鲁本

回答by Eoin Campbell

A list box only supports a collection of string ListItems.

列表框仅支持字符串 ListItems 的集合。

Adding a checkbox to it in that manner will result in the checkbox.ToString() value appearing.

以这种方式向其添加复选框将导致 checkbox.ToString() 值出现。

You might want to look at the "CheckedListBox" control though I'm not sure if that exists in the CompactFramework.

您可能想查看“CheckedListBox”控件,但我不确定 CompactFramework 中是否存在该控件。

回答by Phaedrus

The Compact Framework doesn't support the CheckedListBox control. You could use a ListViewwith the CheckBoxesproperty set to true.

Compact Framework 不支持 CheckedListBox 控件。你可以使用一个ListView控件的CheckBox属性设置为true。

回答by Klee

The Resco Mobileforms Toolkitallows you to do this. The package isn't but it does have this functionality (As well as a fair few other useful bits that make CF apps feel a lot better than the standard Windows Mobile apps).

器Resco Mobileforms工具包可以让你做到这一点。该软件包不是,但它确实具有此功能(以及其他一些使 CF 应用程序感觉比标准 Windows Mobile 应用程序好很多的其他有用位)。

N.B. I know this question was asked a long time ago, but we've recently been asked to write new software for one of these devices so I figure that even if you no longer need to solve this problem there are others out there that might benefit from this.

注意,我知道这个问题很久以前就有人问过了,但我们最近被要求为其中一个设备编写新软件,所以我认为即使您不再需要解决这个问题,还有其他人可能会从中受益由此。

回答by Damon8or

Your only other option using stock controls is to use Panel and perform the layout manually, and set AutoScroll true. You can subclass Panel to expose your own Add/Remove type method to perform the layout when Items are added and removed. You can use something like the swatch below to layout a list. With this approach you can now compose your list with any kind of Control, but you loose programmatic convenience of the ListBox. I suppose you could add such functionality into the Panel subclass if necessary.

使用股票控件的唯一其他选择是使用 Panel 并手动执行布局,并将 AutoScroll 设置为 true。您可以子类化 Panel 以公开您自己的添加/删除类型方法,以便在添加和删除项目时执行布局。您可以使用类似于下面的样本来布置列表。使用这种方法,您现在可以使用任何类型的 Control 组合您的列表,但是您失去了 ListBox 的编程便利性。我想您可以在必要时将此类功能添加到 Panel 子类中。

        public static void VerticalListLayoutStrategy(Panel panel)
    {
        int top = 0;
        foreach (Control control in panel.Controls)
        {
            control.Location = new Point(0, top);
            control.Width = panel.ClientSize.Width;
            top += control.Height;
        }
    }