【问题标题】:C# How to add/deduct from a selected value in a listC#如何从列表中的选定值中添加/减去
【发布时间】:2017-05-23 08:43:20
【问题描述】:

我有一个帐户列表,每个帐户都包含名称、帐号和余额。我现在拥有的是一个包含帐户列表和交易表单按钮的主表单。因此,当用户从列表中选择一个帐户时,他们可以单击按钮打开 Transaction 表单。

我正在努力做的是在交易表格上输入一个值以从所选帐户余额中添加/扣除......完成后会在列表中更新。有人可以帮忙吗?

类:

  • Dashboard.cs - 包含打开交易表单的列表和交易按钮。
  • Account.cs - 设置帐户信息。
  • Transaction.cs - 将数据传递给帐户类。
  • AccountList.cs - 创建列表。

Dashboard.cs(主窗体):

//Current Desposit/Withdraw button
private void btn_DepositWithdraw_Click(object sender, EventArgs e)
{
    Transaction TransactionForm = new Transaction();

    //Display form but only process results if OK is pressed
    if (TransactionForm.ShowDialog() == DialogResult.OK)
    {
        //Update the item in the list

    }
}

//The selected item in the list
public void list_Accounts_SelectedIndexChanged(object sender, EventArgs e)
{
    //get index of selected account
    int index = list_Accounts.SelectedIndex;
    Account a = (Account)list_Accounts.Items[index];

    //Update text on the Dashboard form
    txt_AccountName.Text = a.AccountName + " #" + a.AccountNo;
    txt_CurrentBalance.Text = "£" + a.Balance;
}

Account.cs:

[Serializable]
    public class Account
    {
        //Set the Variable
        public string AccountName { get; set; }
        public string AccountNo { get; set; }
        public string Balance { get; set; }
        public string Interest { get; set; }
        public string NewBalance { get; set; }

        public Account()
        {
            AccountName = "Account Name not inserted!";
            AccountNo = "Account Number not inserted!";
            Balance = "Balance not inserted!";
        }

        public Account(string name, string accNo, string staBal, string interest)
        {
            AccountName = name;
            AccountNo = accNo;
            Balance = staBal;
            Interest = interest;
        }

        public override string ToString()
        {
            return String.Format("{0} #{1}", AccountName, AccountNo);
        }
    }

    [Serializable]
    public class CurrentAcc : Account
    {

        public CurrentAcc()
        {
        }

        public CurrentAcc(string name, string accNo, string staBal, string interest) : base(name, accNo, staBal, interest)
        {

        }

        public override string ToString()
        {
            return base.ToString() + String.Format("{0,-17}", " (Current Account)");
        }
    }

    [Serializable]
    public class SavingsAcc : Account
    {

        public SavingsAcc()
        {
        }

        public SavingsAcc(string name, string accNo, string staBal, string interest) : base(name, accNo, staBal, interest)
        {

        }

        public override string ToString()
        {
            return base.ToString() + String.Format("{0,-17}", " (Savings Account)");
        }
    }

    [Serializable]
    public class Deposit : Account
    {

    }

    [Serializable]
    public class Withdraw : Account
    {

    }

Transaction.cs:

public string NewTransactionInformation()
{
    Account nt;

    if (radio_Deposit.Checked)
    {
        nt = new Deposit(input_Amount.Text);
    }
    else
    {
        nt = new Withdraw(input_Amount.Text);
    }

    return nt;
}

AccountList.cs:

class AccountList
{
        private List<Account> allaccounts;
        public List<Account> AllAccounts
        {
            get { return allaccounts; }
        }

        public AccountList()
        {
            allaccounts = new List<Account>();
        }

        public void AddCurrent(Account a)
        {
            allaccounts.Add(a);
        }
}

【问题讨论】:

    标签: c# asp.net list transactions


    【解决方案1】:

    我建议在 TransactionForm 中创建一个公共属性,您可以存储整个 Account 对象或仅存储一个小数余额。

    然后返回Dashboard.cs 更新您编辑的项目,方法是访问TransactionFrom 中的公共属性,如下所示:

    if (TransactionForm.ShowDialog() == DialogResult.OK)
    {
        //Update the item in the list
        var currentBalance = TransactionForm.Balance;
    }
    

    【讨论】:

    • 您正在苦苦挣扎的确切部分是什么?
    • 昨晚试过我尝试在交易表单中创建一个公共属性,然后在 Dashboard.cs 中访问它
    • 那么问题出在哪里?字段是空的?还是 TransactionForm 已经被释放?
    【解决方案2】:

    您可以根据您的 allAccounts 列表使用 linq 提供显示列表。 类似的东西:

    public void list_Accounts_SelectedIndexChanged(object sender, EventArgs e)
    {
        //get index of selected account
        int index = list_Accounts.SelectedIndex;
        Account a = AllAccount.Where( account => account.AccountNo = index;
    
        //Update text on the Dashboard form
        txt_AccountName.Text = a.AccountName + " #" + a.AccountNo;
        txt_CurrentBalance.Text = "£" + a.Balance;
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2022-06-10
      • 2018-10-20
      • 2016-07-12
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多