-1

How do I preload(form1_load) and display the list of name inside of an array or loaded to a list then display it?

This is what I got:

private void Form1_Load(object sender, EventArgs e) {
        Random rand = new Random();
        int index = 0;
        string[] names = new string[] {
            "Josh",
            "Waylon",
            "Alan",
            "Jack",
            "John",
            "elaine",
            "Monica",
            "Kaithe",
            "Kim",
            "Jazy"
        };

////preload the boxlist with 10 boxes
for (int i = 1; i <= 10; i++) {
    int accountNumber = rand.Next(0, 10);
    decimal accountBalance = rand.Next(0, 10);
    string accountName = names[];

    Account account = new Account(accountNumber, accountBalance, accountName);
    //save the box in the boxlist
    accountList.Add(account); 
}
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
ki ng
  • 1
  • 2
  • what do you mean by preload? and in which control do you want to display the names? in a `ListBox` ? there is **no boxlist** – Mong Zhu Feb 22 '18 at 07:02

1 Answers1

0

If I understand the structure of your classes correctly, each account contains an array of names.

You cannot show such a complex structure in a single component.

The traditional approach in this case is to use Master/Details: in one control we show flat data, in another - nested data.

See the complete code sample.

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        List<Account> accountList;
        DataGridView dataGridViewAccounts;
        ListBox listBoxNames;

        public Form1()
        {
            //InitializeComponent();
            Size = new Size(400, 350);

            accountList = new List<Account>();
            dataGridViewAccounts = new DataGridView { Parent = this, Dock = DockStyle.Left };

            listBoxNames = new ListBox { Parent = this, Dock = DockStyle.Right };

            Load += Form1_Load;                
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Random rand = new Random();

            string[] names = new string[] {
                "Josh", "Waylon", "Alan", "Jack", "John", "Elaine", "Monica", "Kaithe", "Kim", "Jazy" };

            for (int i = 1; i <= 10; i++)
            {
                int accountNumber = i;
                decimal accountBalance = rand.Next(0, 10);
                string[] accountNames = names.Select(n => n + i).ToArray();

                var account = new Account(accountNumber, accountBalance, accountNames);
                accountList.Add(account);
            }

            var bindingSourceAccounts = new BindingSource();
            bindingSourceAccounts.DataSource = accountList;

            var bindingSourceNames = new BindingSource(bindingSourceAccounts, "Names");

            dataGridViewAccounts.DataSource = bindingSourceAccounts;
            listBoxNames.DataSource = bindingSourceNames;
        }
    }

    public class Account
    {
        public Account(int number, decimal balance, string[] names)
        {
            Number = number;
            Balance = balance;
            Names = names;
        }

        public int Number { get; set; }
        public decimal Balance { get; set; }
        public string[] Names { get; set; }
    }
}
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49