-2

I know I'm butchering the transaction class. I just have no idea what to do. Nothing gets displayed when I run the form and hit add new transaction. I appreciate any help here's my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AccountBalance
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnNewTran_Click(object sender, EventArgs e)
        {
            string strTransactionType = "";
            if (rbDeposit.Checked)
            {
                strTransactionType = rbDeposit.Text;
            }
            else
            {
                strTransactionType = rbWithdrawal.Text;
            }



            Transaction newTransaction = new Transaction();
            newTransaction.Amount = decimal.Parse(txtTAmount.Text);
            newTransaction.Type = strTransactionType;
            newTransaction.Date = txtTDate.Text;
            lstTransactions.Items.Add(newTransaction);



            decimal decBalance = decimal.Parse(lblCBalance.Text);
            if (newTransaction.Type == "Deposit")
            {
                decBalance += newTransaction.Amount;
                lblCBalance.Text = decBalance.ToString("c");
            }
            else
            {
                decBalance -= newTransaction.Amount;
                lblCBalance.Text = decBalance.ToString("c");
            }



            if (decBalance < 0)
            {
                lblCBalance.ForeColor = Color.Red;
            }



            rbDeposit.Checked = false;
            rbWithdrawal.Checked = false;
            txtTAmount.Clear();
            txtTDate.Clear();
            txtTAmount.Focus();
        }



        private void btnRemTran_Click(object sender, EventArgs e)
        {
            int intSelectedIndex = lstTransactions.SelectedIndex;
            Transaction selectedTransaction = (Transaction)lstTransactions.SelectedItem;
            lstTransactions.Items.RemoveAt(intSelectedIndex);



            decimal decBalance = decimal.Parse(lblCBalance.Text);
            if (selectedTransaction.Type == "Deposit")
            {
                decBalance -= selectedTransaction.Amount;
                lblCBalance.Text = decBalance.ToString("c");
            }
            else
            {
                decBalance += selectedTransaction.Amount;
                lblCBalance.Text = decBalance.ToString("c");
            }



            if (decBalance >= 0)
            {
                lblCBalance.ForeColor = Color.Black;
            }
        }



        private void btnClear_Click(object sender, EventArgs e)
        {
            rbDeposit.Checked = false;
            rbWithdrawal.Checked = false;
            txtTAmount.Clear();
            txtTDate.Clear();
            txtTAmount.Focus();
        }



        private void lstTransactions_Click(object sender, EventArgs e)
        {
            Transaction selectedTransaction = (Transaction)lstTransactions.SelectedItem;
            if (selectedTransaction.Type == "Deposit")
            {
                rbDeposit.Checked = true;
            }
            else
            {
                rbWithdrawal.Checked = true;
            }
            txtTAmount.Text = selectedTransaction.Amount.ToString("c");
            txtTDate.Text = selectedTransaction.Date;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }

    public class Transaction
    {
        internal decimal Amount;
        internal string Type;
        internal string Date;
    }
}
namespace AccountBalance
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lblTDate = new System.Windows.Forms.Label();
            this.txtTDate = new System.Windows.Forms.TextBox();
            this.lblTAmount = new System.Windows.Forms.Label();
            this.txtTAmount = new System.Windows.Forms.TextBox();
            this.lblTType = new System.Windows.Forms.Label();
            this.rbDeposit = new System.Windows.Forms.RadioButton();
            this.rbWithdrawal = new System.Windows.Forms.RadioButton();
            this.rbServFee = new System.Windows.Forms.RadioButton();
            this.lblPayee = new System.Windows.Forms.Label();
            this.txtPayee = new System.Windows.Forms.TextBox();
            this.lblCheckNum = new System.Windows.Forms.Label();
            this.txtCheckNum = new System.Windows.Forms.TextBox();
            this.lblCBalance = new System.Windows.Forms.Label();
            this.txtCBalance = new System.Windows.Forms.TextBox();
            this.lstTransactions = new System.Windows.Forms.ListBox();
            this.btnNewTran = new System.Windows.Forms.Button();
            this.btnRemTran = new System.Windows.Forms.Button();
            this.btnClear = new System.Windows.Forms.Button();
            this.btnExit = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // lblTDate
            // 
            this.lblTDate.AutoSize = true;
            this.lblTDate.Location = new System.Drawing.Point(12, 9);
            this.lblTDate.Name = "lblTDate";
            this.lblTDate.Size = new System.Drawing.Size(104, 13);
            this.lblTDate.TabIndex = 0;
            this.lblTDate.Text = "Date of Transaction:";
            // 
            // txtTDate
            // 
            this.txtTDate.Location = new System.Drawing.Point(15, 25);
            this.txtTDate.Name = "txtTDate";
            this.txtTDate.Size = new System.Drawing.Size(100, 20);
            this.txtTDate.TabIndex = 1;
            // 
            // lblTAmount
            // 
            this.lblTAmount.AutoSize = true;
            this.lblTAmount.Location = new System.Drawing.Point(263, 9);
            this.lblTAmount.Name = "lblTAmount";
            this.lblTAmount.Size = new System.Drawing.Size(105, 13);
            this.lblTAmount.TabIndex = 0;
            this.lblTAmount.Text = "Transaction Amount:";
            // 
            // txtTAmount
            // 
            this.txtTAmount.Location = new System.Drawing.Point(266, 25);
            this.txtTAmount.Name = "txtTAmount";
            this.txtTAmount.Size = new System.Drawing.Size(100, 20);
            this.txtTAmount.TabIndex = 2;
            // 
            // lblTType
            // 
            this.lblTType.AutoSize = true;
            this.lblTType.Location = new System.Drawing.Point(473, 9);
            this.lblTType.Name = "lblTType";
            this.lblTType.Size = new System.Drawing.Size(93, 13);
            this.lblTType.TabIndex = 0;
            this.lblTType.Text = "Transaction Type:";
            // 
            // rbDeposit
            // 
            this.rbDeposit.AutoSize = true;
            this.rbDeposit.Location = new System.Drawing.Point(481, 28);
            this.rbDeposit.Name = "rbDeposit";
            this.rbDeposit.Size = new System.Drawing.Size(61, 17);
            this.rbDeposit.TabIndex = 3;
            this.rbDeposit.TabStop = true;
            this.rbDeposit.Text = "Deposit";
            this.rbDeposit.UseVisualStyleBackColor = true;
            // 
            // rbWithdrawal
            // 
            this.rbWithdrawal.AutoSize = true;
            this.rbWithdrawal.Location = new System.Drawing.Point(481, 51);
            this.rbWithdrawal.Name = "rbWithdrawal";
            this.rbWithdrawal.Size = new System.Drawing.Size(78, 17);
            this.rbWithdrawal.TabIndex = 4;
            this.rbWithdrawal.TabStop = true;
            this.rbWithdrawal.Text = "Withdrawal";
            this.rbWithdrawal.UseVisualStyleBackColor = true;
            // 
            // rbServFee
            // 
            this.rbServFee.AutoSize = true;
            this.rbServFee.Location = new System.Drawing.Point(481, 74);
            this.rbServFee.Name = "rbServFee";
            this.rbServFee.Size = new System.Drawing.Size(82, 17);
            this.rbServFee.TabIndex = 5;
            this.rbServFee.TabStop = true;
            this.rbServFee.Text = "Service Fee";
            this.rbServFee.UseVisualStyleBackColor = true;
            // 
            // lblPayee
            // 
            this.lblPayee.AutoSize = true;
            this.lblPayee.Location = new System.Drawing.Point(12, 53);
            this.lblPayee.Name = "lblPayee";
            this.lblPayee.Size = new System.Drawing.Size(40, 13);
            this.lblPayee.TabIndex = 0;
            this.lblPayee.Text = "Payee:";
            // 
            // txtPayee
            // 
            this.txtPayee.Location = new System.Drawing.Point(15, 71);
            this.txtPayee.Name = "txtPayee";
            this.txtPayee.Size = new System.Drawing.Size(230, 20);
            this.txtPayee.TabIndex = 6;
            // 
            // lblCheckNum
            // 
            this.lblCheckNum.AutoSize = true;
            this.lblCheckNum.Location = new System.Drawing.Point(263, 53);
            this.lblCheckNum.Name = "lblCheckNum";
            this.lblCheckNum.Size = new System.Drawing.Size(81, 13);
            this.lblCheckNum.TabIndex = 0;
            this.lblCheckNum.Text = "Check Number:";
            // 
            // txtCheckNum
            // 
            this.txtCheckNum.Location = new System.Drawing.Point(266, 71);
            this.txtCheckNum.Name = "txtCheckNum";
            this.txtCheckNum.Size = new System.Drawing.Size(100, 20);
            this.txtCheckNum.TabIndex = 7;
            // 
            // lblCBalance
            // 
            this.lblCBalance.AutoSize = true;
            this.lblCBalance.Location = new System.Drawing.Point(12, 109);
            this.lblCBalance.Name = "lblCBalance";
            this.lblCBalance.Size = new System.Drawing.Size(86, 13);
            this.lblCBalance.TabIndex = 0;
            this.lblCBalance.Text = "Current Balance:";
            // 
            // txtCBalance
            // 
            this.txtCBalance.Location = new System.Drawing.Point(145, 102);
            this.txtCBalance.Name = "txtCBalance";
            this.txtCBalance.ReadOnly = true;
            this.txtCBalance.Size = new System.Drawing.Size(100, 20);
            this.txtCBalance.TabIndex = 0;
            // 
            // lstTransactions
            // 
            this.lstTransactions.FormattingEnabled = true;
            this.lstTransactions.Location = new System.Drawing.Point(15, 125);
            this.lstTransactions.Name = "lstTransactions";
            this.lstTransactions.Size = new System.Drawing.Size(439, 316);
            this.lstTransactions.TabIndex = 0;
            // 
            // btnNewTran
            // 
            this.btnNewTran.Location = new System.Drawing.Point(460, 171);
            this.btnNewTran.Name = "btnNewTran";
            this.btnNewTran.Size = new System.Drawing.Size(99, 23);
            this.btnNewTran.TabIndex = 8;
            this.btnNewTran.Text = "New Transaction";
            this.btnNewTran.UseVisualStyleBackColor = true;
            // 
            // btnRemTran
            // 
            this.btnRemTran.Location = new System.Drawing.Point(460, 252);
            this.btnRemTran.Name = "btnRemTran";
            this.btnRemTran.Size = new System.Drawing.Size(99, 23);
            this.btnRemTran.TabIndex = 9;
            this.btnRemTran.Text = "Remove";
            this.btnRemTran.UseVisualStyleBackColor = true;
            // 
            // btnClear
            // 
            this.btnClear.Location = new System.Drawing.Point(460, 333);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(99, 23);
            this.btnClear.TabIndex = 10;
            this.btnClear.Text = "Clear";
            this.btnClear.UseVisualStyleBackColor = true;
            // 
            // btnExit
            // 
            this.btnExit.Location = new System.Drawing.Point(460, 414);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(99, 23);
            this.btnExit.TabIndex = 11;
            this.btnExit.Text = "Exit";
            this.btnExit.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(578, 450);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnClear);
            this.Controls.Add(this.btnRemTran);
            this.Controls.Add(this.btnNewTran);
            this.Controls.Add(this.lstTransactions);
            this.Controls.Add(this.txtCBalance);
            this.Controls.Add(this.lblCBalance);
            this.Controls.Add(this.txtCheckNum);
            this.Controls.Add(this.lblCheckNum);
            this.Controls.Add(this.txtPayee);
            this.Controls.Add(this.lblPayee);
            this.Controls.Add(this.rbServFee);
            this.Controls.Add(this.rbWithdrawal);
            this.Controls.Add(this.rbDeposit);
            this.Controls.Add(this.lblTType);
            this.Controls.Add(this.txtTAmount);
            this.Controls.Add(this.lblTAmount);
            this.Controls.Add(this.txtTDate);
            this.Controls.Add(this.lblTDate);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lblTDate;
        private System.Windows.Forms.TextBox txtTDate;
        private System.Windows.Forms.Label lblTAmount;
        private System.Windows.Forms.TextBox txtTAmount;
        private System.Windows.Forms.Label lblTType;
        private System.Windows.Forms.RadioButton rbDeposit;
        private System.Windows.Forms.RadioButton rbWithdrawal;
        private System.Windows.Forms.RadioButton rbServFee;
        private System.Windows.Forms.Label lblPayee;
        private System.Windows.Forms.TextBox txtPayee;
        private System.Windows.Forms.Label lblCheckNum;
        private System.Windows.Forms.TextBox txtCheckNum;
        private System.Windows.Forms.Label lblCBalance;
        private System.Windows.Forms.TextBox txtCBalance;
        private System.Windows.Forms.ListBox lstTransactions;
        private System.Windows.Forms.Button btnNewTran;
        private System.Windows.Forms.Button btnRemTran;
        private System.Windows.Forms.Button btnClear;
        private System.Windows.Forms.Button btnExit;
    }
}

I pretty much tried to use quick fixes and watched some random videos and came up with nothing.

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
  • The list box has no idea how to display your transaction. To start, you could provide a `public override string ToString()` method in the `Transaction` class. – Klaus Gütter Feb 05 '23 at 08:25
  • You seem to have posted more code than what would be reasonable for your issue. Please read [ask] and how to make a [mre]; providing a MRE helps users answer your question and future users relate to your issue. – Jim G. Feb 07 '23 at 19:09

1 Answers1

1

Don't add your items to the ListBox directly. Create an appropriate list and bind that to the ListBox, then add your items to that list. If you use a BindingList<Transaction> then adding an item to the list will automatically update the bound control, which will not happen with a List<Transaction>.

var transactions = new BindingList<Transaction>();

myListBox.DataSource = transactions;

By default, when you add an item to a ListBox, ToString is called on that object and the result displayed. If you don't override the ToString method, that will just be the name of the type. You can set the DisplayMember of the ListBox and the specified property value will be displayed, e.g.

var transactions = new BindingList<Transaction>();

myListBox.DisplayMember = nameof(Transaction.Amount);
myListBox.DataSource = transactions;

Note that only properties can be specified as the DisplayMember or ValueMember. Fields will not work.

If you want some custom text displayed then you need to override the ToString method and have it return the desired text.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46