C#如何将对象添加到列表框并使用特定文本显示它们

时间:2014-11-29 11:57:40

标签: c# listbox hid listboxitems

我曾经在delphi中这样做,即同时将对象和项目添加到列表框中:

function TMainForm.JvHidDeviceController1Enumerate(HidDev: TJvHidDevice;
  const Idx: Integer): Boolean;
var
  N: Integer;
  Dev: TJvHidDevice;
begin
  VID := JvValidateEdit1.Value;
  PID := JvValidateEdit2.Value;
  // MessageDlg(IntToStr(DeviceRadioGroup.ItemIndex), mtInformation, mbOKCancel, 0);
  If (HidDev.Attributes.VendorID = VID) And
    (HidDev.Attributes.ProductID = PID) Then
  begin
    N := ListBox1.Items.Add('dsn: ' + HidDev.SerialNumber);
    JvHidDeviceController1.CheckOutByIndex(Dev, Idx);
    ListBox1.Items.Objects[N] := Dev;
    DeviceMemo.Lines.Append(HidDev.SerialNumber +
      ' : connected(enumerated)   - ' + (TimeToStr(Now)));
    InfoBtn.Enabled := true;
    WriteBtn.Enabled := true;
  end;
  Result := true;
end;

我正在起诉Mike的HIDLibrary,我有多个设备,每个设备都有一个不同的序列号,我需要添加到列表框中,问题是我不知道是否可以添加对象列表框并在C#中以不同的文本显示它们,就像我在delphi中所做的那样,默认文本显示了当我将HidDevice对象添加到列表框时设备的路径,而我只想显示serialNo。我提取serialNo。每个有效的HID设备,在一行中,我使用添加serialNo得到的索引将其插入AllValidHIDDevices。到列表框,以便每个HID设备绑定到其serialNo。当我点击序列号时,我实际上正在检索具有该序列号的HID设备software snapshot

HidDevice CurrentHIDDevice;
List<HidDevice> AllValidHIDDevices = new List<HidDevice>();
IEnumerable<HidDevice> USBDevices;

private void timer1_Tick(object sender, EventArgs e)
{

    AllValidHIDDevices.Clear();
    listBox1.Items.Clear();

    USBDevices = HidDevices.Enumerate(0x0483);

    foreach (HidDevice validHIDDevice in USBDevices.Where(x => x.Attributes.ProductHexId == "0xAB12"))
    {
        byte[] serialNo;

        validHIDDevice.Inserted += deviceInserted;
        validHIDDevice.Removed += deviceRemoved;
        validHIDDevice.MonitorDeviceEvents = true;
        if (validHIDDevice.IsConnected)
        {
            timer1.Stop();
            button1.Enabled = true;
            validHIDDevice.ReadSerialNumber(out serialNo);
            AllValidHIDDevices.Insert(listBox1.Items.Add("Serial Number: " + Encoding.Unicode.GetString(serialNo)), validHIDDevice);
        }
    }
    if (listBox1.Items.Count > 0)
    {
        listBox1.SelectedIndex = 0;
        CurrentHIDDevice = AllValidHIDDevices[listBox1.SelectedIndex];  
    }
}

private void deviceInserted()
{
    this.InvokeEx(x => x.toolStripStatusLabel1.Text = listBox1.Items.Count.ToString() + " device(s) attached");
}

private void deviceRemoved()
{
    foreach (HidDevice validHIDDevice in AllValidHIDDevices)
    {
        if (validHIDDevice.IsOpen)
            validHIDDevice.CloseDevice();
        validHIDDevice.Dispose();
    }
    this.InvokeEx(x => x.toolStripStatusLabel1.Text = "No device(s) attached");
    this.InvokeEx(x => x.listBox1.Items.Clear());
    this.InvokeEx(x => x.button1.Enabled = false);
    this.InvokeEx(x => x.timer1.Start());
}

添加项目后,我只需点击它即可选择它并与之互动:

    private void listBox1_Click(object sender, EventArgs e)
    {
        try
        {
            if (listBox1.Items.Count > 0 && listBox1.SelectedIndex >= 0)
                CurrentHIDDevice = AllValidHIDDevices[listBox1.SelectedIndex];
        }
        catch
        {
            MessageBox.Show("Selected item is not a USB device!");
        }
    }

1 个答案:

答案 0 :(得分:0)

好的,我现在回答这个问题。

我正在使用dll,因为我用NuGet下载了它,现在我明白了为什么我无法修改与HidDevice类相关的任何内容,我认为NuGet也下载了这个库的源代码并使用它,但在包文件夹,只有dll。然后我删除它并从GitHub下载了库本身的源代码,学习了如何添加它并使用它。我刚刚修改了HidDevice类中的ToString函数,如下所示:

    public override string ToString()
    {
        byte[] SerialNumber;
        ReadSerialNumber(out SerialNumber);
        string SerialNumberString = "Serial Number = " + Encoding.Unicode.GetString(SerialNumber);

        if (SerialNumberString[0] == '\0')
        {
            return string.Format("VendorID={0}, ProductID={1}, Version={2}, DevicePath={3}",
                                _deviceAttributes.VendorHexId,
                                _deviceAttributes.ProductHexId,
                                _deviceAttributes.Version,
                                _devicePath);
        }
        else
        {
            return SerialNumberString;
        }
    }

解决方案现在变得更加简单了...感谢@TaW首先指出了它。