I'm new to C# and visual studio
My problem is the following:
I have an access file with foods.
In a form i have a listbox and i have succesfully connected the data source of the listbox with the access file and i get the foods in my listbox
But now i want to copy the items of the listbox to an array
i do that with the following line
listbox1.Items.CopyTo(aa, 0);
but all the values of the array takes the following value "System.Windows.Forms.ListBox + ObjectCollection"
I found the following
By default when you bind the ListBox’s ItemsSource to a collection of objects of a custom type, the ListBox would simply call the object’s ToString() method to determine what to display for each item. The ToString method would normally simply display the type name of the object.
If you instead want a value of a property of the object to be displayed in the listbox, you should set the DisplayMemberPath property of the ListBox to that property name as follows:
<ListBox ItemsSource='{StaticResource myCollection}' DisplayMemberPath='FirstName' />
..but i couldnt figure out how i can make this work (if this is the reason..)
I would really appreciate any help
My code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleEleme nt;
namespace food1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pRODUCTSBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.pRODUCTSBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.foodsDataS et);
}
private void Form1_Load(object sender, EventArgs e)
{
this.pRODUCTSTableAdapter.Fill(this.foodsDataSet.P RODUCTS);
object[] aa = new object[80];
listbox1.Items.CopyTo(aa, 0);
textBox1.Text = “ ”+aa[2];
}
}
}