簡體   English   中英

將數組中的名稱列表顯示到類中

[英]Displaying list of name from an array into a class

如何預加載( 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); 
}

如果我正確理解您的類的結構,每個帳戶都包含一個名稱數組。

您無法在單個組件中顯示如此復雜的結構。

這種情況下的傳統方法是使用主/詳細信息:在一個控件中我們顯示平面數據,在另一個控件中顯示嵌套數據。

查看完整的代碼示例。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM