如何在C#Windows Form应用程序的列表框中显示结构的内容 [英] how to display the content of a structure in a listbox in a c# windows form application

查看:274
本文介绍了如何在C#Windows Form应用程序的列表框中显示结构的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dunno,如果我对此采取正确的方法?下面结构的内容在其他地方定义.当我运行代码时,它只是输出4个零的列表.任何帮助将不胜感激.....

Dunno if I'm going the right way about this? The contents of the structure below is defined elsewhere. when I run the code it is just outputting a list of 4 zeros. any help would be greatly appreciated.....

public class NativeMethods
{

    public struct FT_DEVICE_LIST_INFO_NODE
    {
        public uint ID;
        public uint LocId;       
        public string SerialNumber;           
        public string Description;
    }

    [DllImportAttribute(@"C:\Users\Brendan\Documents\libMPSSE.dll", EntryPoint = "SPI_GetNumChannels")]
    public static extern uint SPI_GetChannelInfo(uint index, ref FT_DEVICE_LIST_INFO_NODE chanInfo);
}



public partial class Form1 : Form
{
    List<uint> items = new List<uint>();

    public Form1()
    {

        InitializeComponent();

        NativeMethods.FT_DEVICE_LIST_INFO_NODE devlist = new NativeMethods.FT_DEVICE_LIST_INFO_NODE();

        for(uint x=0;x<4;x++)
        {

        index = 0;
        items.Add(NativeMethods.SPI_GetChannelInfo(index, ref devlist));

         }
        listBox.DataSource = items;
    }
}

推荐答案

由于您写道您的结构是在其他地方定义的,所以我假设您无法更改它.

Since you wrote that your structure is defined elsewhere I asume you can't change it.

获取自定义显示字符串的通常方法是在一个最小的类中包装您的结构,也许是这样的:

The usual way to get a custom made display string is to wrap your structure in a minimal class, maybe like this:

class FT_DEVICE_wrapper
{
    public FT_DEVICE_LIST_INFO_NODE INFO_NODE { get; set; }

    public FT_DEVICE_wrapper(FT_DEVICE_LIST_INFO_NODE data_)
    { INFO_NODE = data_; }

    public override string ToString()
    {
        return string.Format("ID = {0} LocID = {1} SNr = {2} ({3}) ", 
          INFO_NODE.ID, INFO_NODE.LocId, INFO_NODE.SerialNumber, INFO_NODE.Description);
    }

}

现在,您可以像这样添加包装器实例:

Now you can add instances of the wrapper like this:

private void button1_Click(object sender, EventArgs e)
{
    FT_DEVICE_LIST_INFO_NODE N1 = new FT_DEVICE_LIST_INFO_NODE();
    N1.ID = 1;
    N1.LocId = 1001;
    N1.SerialNumber = "123-456-00";
    N1.Description = "test 01";
    FT_DEVICE_wrapper W1 = new FT_DEVICE_wrapper(N1);

    listBox1.Items.Add(W1);

}

如您所见,无论采用哪种格式格式化输出字符串,都将显示结构的数据.

As you can see the structure's data are displayed in whichever way you format the output string.

您可以通过投射这样的项目来访问结构

And you can access the structure by casting the Items like this

Console.WriteLine( ((FT_DEVICE_wrapper) listBox1.Items[0]).INFO_NODE.Description );

或者,imo像这样更好一些:

Or, imo a little better like this:

FT_DEVICE_LIST_INFO_NODE node = ((FT_DEVICE_wrapper)listBox1.Items[0]).INFO_NODE;
Console.WriteLine(node.SerialNumber);

可能可能要考虑研究ListViews,该列提供支持;在这里,添加结构的方法会大不相同,因为您希望将某些日期字段放入单独的列中.

You may want to consider looking into ListViews, which support columns; here the way to add the structure would be quite different as you would want to put some of the date fields into separate columns.

如果要使用DataBinding,请先创建一个适当的列表:

If you want to use DataBinding you start by creating a proper List:

List<FT_DEVICE_wrapper> items = new List<FT_DEVICE_wrapper>();

然后替换

listBox1.Items.Add(W1);

通过

items.Add(W1);
listBox1.DataSource = items;

注意:通过简单地在原始结构中添加ToString方法,您还可以使结构在ListBox中正常显示而不被包裹.

Note: By simply adding a ToString method to the original structure you cold also make the structure display fine in the ListBox without being wrapped..

这篇关于如何在C#Windows Form应用程序的列表框中显示结构的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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