【发布时间】:2019-03-24 01:35:06
【问题描述】:
我有一个自定义组合框控件,它应该显示可用网络摄像头的列表。
代码很小。
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using DirectShowLib;
namespace CameraSelectionCB
{
public partial class CameraComboBox : ComboBox
{
protected BindingList<string> Names;
protected DsDevice[] Devices;
public CameraComboBox()
{
InitializeComponent();
Devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
Names = new BindingList<string>(Devices.Select(d => d.Name).ToList());
this.DataSource = Names;
this.DropDownStyle = ComboBoxStyle.DropDownList;
}
}
}
但是,我遇到了几个错误。 首先,每当我放置此组合框的实例时,设计器都会生成以下代码:
this.cameraComboBox1.DataSource = ((object)(resources.GetObject("cameraComboBox1.DataSource")));
this.cameraComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cameraComboBox1.Items.AddRange(new object[] {
"HP Webcam"});
这会在运行时导致异常,因为在设置 DataSource 时不应修改 Items。即使我不触摸设计器中的 Items 属性也会发生这种情况。
“HP 网络摄像头”是当时我电脑上唯一的摄像头。
如何抑制这种行为?
【问题讨论】:
标签: c# winforms combobox user-controls designer