将数组中的名称列表显示为类

时间:2018-02-22 06:55:54

标签: c# winforms class display preload

如何预加载(form1_load)并在数组内显示名称列表或加载到列表中然后显示它?

这就是我得到的:

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); 
}

1 个答案:

答案 0 :(得分:0)

如果我正确理解了类的结构,每个帐户都包含一个名称数组。

您无法在单个组件中显示如此复杂的结构。

在这种情况下,传统的方法是使用Master / Details:在一个控件中我们显示平面数据,在另一个控件中 - 嵌套数据。

请参阅完整的代码示例。

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; }
    }
}