Member Avatar for Annieken

Hi

I've got a listbox which has a datasource. Now, when someone press on a button 'delete'. I want that the last created object is deleted. And then you return to the panel with the listbox. You always see the object you deleted. This I want to remove.

public void btnCreate_Click(object sender, EventArgs e)
        {
                // more code above
                  ProcessList.Add(p);
                  lbCreatedProcesses.DataSource = null;
                  lbCreatedProcesses.DataSource = ProcessList;   
                // more code under           
         }
private void llProcesDelete_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Proces leeg = null;
                int totaal = ProcessList.Count;
                ProcessList[totaal - 1] = leeg;

                //MessageBox.Show(leeg.PNaam + " wordt verwijderd");

                pnlPMaken.Visible = true;
                pnlBMaken.Visible = false;
                pnlOverzicht.Visible = false;
                pnlConti.Visible = false;
                pnlStap.Visible = false;
                // empty everything
                number--;
                tBArrival.Text = "";
                tBName.Text = "Proces ";
                cmbStatus.SelectedIndex = -1;

                // this can't because ProcessList is DataSource
                lbCreatedProcesses.Items.RemoveAt(totaal - 1);         
     
                //lbCreatedProcesses.DataSource = null;
                lbCreatedProcesses.DataSource = ProcessList;
                pnlOverzicht.Invalidate();
               
            }
            catch (ArgumentNullException ane) { ane.ToString(); }

           
        }

Does someone knows a way to solve this?

You are removing the item from the listview and resetting the datasource directly after.

Either don't set the datasource after that, or have a list that contains your items, and remove from that list, not just the listview.

Member Avatar for Annieken

That is what I did, I think --> Removing the last object

Proces leeg = null;
                int totaal = ProcessList.Count;
                ProcessList[totaal - 1] = leeg;

And setting the datasource again

lbCreatedProcesses.DataSource = ProcessList;

The last item from the ProcessList is deleted so I set the datasource again in.

When you set the datasource again, the process list still contains the deleted item,

This will remove the item from the end of the list, not sure what you are needing as far as position in the list, but this is the concept.

ProcessList.RemoveAt(totaal - 1);
Member Avatar for Annieken

That is what I tried but I get the error, that it is not possible because I've set before the datasource (in another methode - which is necessary)

Surely the datasource should be already set? it would seem the more important question is why is it getting unset?

Member Avatar for Annieken
public void btnCreate_Click(object sender, EventArgs e)
        {
            Proces p = null;           
            try
            {
                // Alle gegevens opvragen die door de gebruiker zijn ingegeven.                
                //number++;
                tBNumber.Text = number.ToString();
                int arrival = int.Parse(tBArrival.Text);
                String name = tBName.Text.ToString();                
                String status = "";
                if (cmbStatus.SelectedIndex.Equals(0))
                    status = "New";
                if (cmbStatus.SelectedIndex.Equals(1))
                    status = "Ready";
                if (cmbStatus.SelectedIndex.Equals(2))
                    status = "Wait";
                
                // Een nieuw proces aanmaken
                 p = new Proces(number, arrival, name, status);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message + "\n" + "Inner Error: " + ex.InnerException, "Er is een fout", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
                
                // Ons Process toevoegen aan de lijst.
                ProcessList.Add(p);

                lbCreatedProcesses.DataSource = null;
                lbCreatedProcesses.DataSource = ProcessList;                

                pnlPMaken.Visible = false;
                pnlBMaken.Visible = true;
                LaatstAangemaaktProces = p;
                pnlOverzicht.Visible = false;
                pnlConti.Visible = false;
                lbProcesNaam.Text = p.PNaam;           
        }

It is needed to first set the DataSource, like you can see, I first have a panel where data is entered. This is than an object. All those objects are set in a list -> ProcessList. In the pnlPMaken (panel) there is also a listbox. In the listbox you see all the objects. That is way I've set the DataSource. And I needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.