简体   繁体   English

C#从类中添加列表框项

[英]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(); 因此,如果一切正确,它将转到: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/ http://www.dreamincode.net/forums/topic/60477-adding-items-to-a-listbox-from-a-class/

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. 您具有定义和定位控件的GUI类。

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?) 然后是一个类,其中单击按钮事件被预订并在其中进行处理(Program.cs类还是可能是同一类?)

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. 然后,您有另一个类,其中定义了NeemOp(int bedag) ,而bankrekeningLinksbankrekeningLinks的一个实例。

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. 您想更新NeemOp函数中的ListBox,但是最有可能NeemOp函数所在的类不了解ListBox。 This can be fixed by either passing the ListBox as a parameter into the NeemOp function like so: 可以通过将ListBox作为参数传递给NeemOp函数来解决此问题,如下所示:

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. 但是,由于新Saldo的计算这一糟糕的设计实际上不必关心GUI,它的工作是计算。 Changing the NeemOp to return the new saldo would be step in the right direction. 更改NeemOp以返回新的Saldo将是朝正确方向迈出的一步。

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. 当然,您可能想验证NeemOp是否成功,并且同时将所有GUI / MesageBoxes代码移到与private void btnOpnemenLinks_Click(object sender, EventArgs e)相同的类中private void btnOpnemenLinks_Click(object sender, EventArgs e)并将计算结果隔离到NeemOp类中。 Fun exercise. 有趣的运动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM