C# question about listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmarid70
    New Member
    • Mar 2023
    • 2

    C# question about listbox

    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 + ObjectCollectio n"
    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 DisplayMemberPa th property of the ListBox to that property name as follows:
    <ListBox ItemsSource='{S taticResource myCollection}' DisplayMemberPa th='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.Collecti ons;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Reflecti on;
    using System.Security .Cryptography;
    using System.Text;
    using System.Threadin g.Tasks;
    using System.Windows. Forms;
    using static System.Windows. Forms.VisualSty les.VisualStyle Element;

    namespace food1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeCompo nent();
    }

    private void pRODUCTSBinding NavigatorSaveIt em_Click(object sender, EventArgs e)
    {
    this.Validate() ;
    this.pRODUCTSBi ndingSource.End Edit();
    this.tableAdapt erManager.Updat eAll(this.foods DataSet);

    }

    private void Form1_Load(obje ct sender, EventArgs e)
    {
    this.pRODUCTSTa bleAdapter.Fill (this.foodsData Set.PRODUCTS);
    object[] aa = new object[80];
    listbox1.Items. CopyTo(aa, 0);
    textBox1.Text = “ ”+aa[2];
    }

    }
    }
Working...