user4654006
user4654006

Reputation:

C# Add listbox item from class

I'm trying to add a listbox item to my listbox from another class.

So here is the main class:

        private void btnOpnemenLinks_Click(object sender, EventArgs e)
    {
        string bedragInCenten = tbEuroLinks.Text + tbCentenLinks.Text;
        if(int.TryParse(tbEuroLinks.Text, out getal) && int.TryParse(tbCentenLinks.Text, out getal))
        {
            if (Convert.ToInt32(tbEuroLinks.Text) >= 0 && (Convert.ToInt32(tbCentenLinks.Text) >= 0))
            {
                bankrekeningLinks.NeemOp(Convert.ToInt32(bedragInCenten));
                update();
                string bedrag = returnBedragLinks();

            }
            else
                MessageBox.Show("Negatieven getallen worden niet geaccepteerd.");
        }
        else
            MessageBox.Show("Ongeldige invoer.");

So, if everything is true it will go to: bankrekeningLinks.NeemOP(); ->

public void NeemOp(int bedrag)
    {
        // bedrag in hele centen, negatieve bedragen worden genegeerd.
        // vul zelf in
        if (bedrag <= saldo) 
        {
            this.saldo = this.saldo - bedrag;

        }
        else
        {
            MessageBox.Show("Onvoldoende saldo.");
        }

If this also is true, i want to add something in my listbox from:

if (bedrag <= saldo) 
        {
            this.saldo = this.saldo - bedrag;
            // ADD ITEM IN LIST BOX <---------
        }

Find things like this:

http://www.dreamincode.net/forums/topic/60477-adding-items-to-a-listbox-from-a-class/

Upvotes: 0

Views: 535

Answers (1)

kristian mo
kristian mo

Reputation: 1486

I am not totally sure about the design you have but it seems like:

You have a GUI class where the controls are defined and positioned.

Then there is a class where the button clicked event is subscribed to and handled in (the Program.cs class or possibly the same class?)

private void btnOpnemenLinks_Click(object sender, EventArgs e)

And then you have another class where NeemOp(int bedag) is defined and bankrekeningLinks is an instance of this class.

You want to update the ListBox in the NeemOp function, but most likely the class the NeemOp function is in does not have any knowledge of the ListBox. This can be fixed by either passing the ListBox as a parameter into the NeemOp function like so:

public void NeemOp(int bedrag, ListBox listBox)
    {
            // bedrag in hele centen, negatieve bedragen worden genegeerd.
            // vul zelf in
            if (bedrag <= saldo) 
            {
                this.saldo = this.saldo - bedrag;
                listBox.Items.Add(saldo);

            }
            else
            {
                MessageBox.Show("Onvoldoende saldo.");
            }

    }

Called by:

bankrekeningLinks.NeemOp(Convert.ToInt32(bedragInCenten), listBox1);

However this bad design as the caluclation of the new saldo should not really have to care about the GUI, it's job is calculation. Changing the NeemOp to return the new saldo would be step in the right direction.

public int NeemOp(int bedrag)
        {
                // bedrag in hele centen, negatieve bedragen worden genegeerd.
                // vul zelf in
                if (bedrag <= saldo) 
                {
                    this.saldo = this.saldo - bedrag;
                    return this.saldo;

                }
                else
                {
                    MessageBox.Show("Onvoldoende saldo.");
                    return this.saldo;
                }

        }

Called by:

var newSaldo =  bankrekeningLinks.NeemOp(Convert.ToInt32(bedragInCenten));
listBox1.Items.Add(newSaldo);
update();
string bedrag = returnBedragLinks();

Of course you might want to verify that NeemOp was successfull, and at the same time maybe move all GUI/MesageBoxes code into the same class as private void btnOpnemenLinks_Click(object sender, EventArgs e) and isolate the calulations to the NeemOp class. Fun exercise.

Upvotes: 1

Related Questions