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 );
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);
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.
If you want to use DataBinding you start by creating a proper List:
List<FT_DEVICE_wrapper> items = new List<FT_DEVICE_wrapper>();
and then replace
listBox1.Items.Add(W1);
by
items.Add(W1);
listBox1.DataSource = items;
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..