How to clear items from a listbox

I have populated a listbox named MyListBox.

How do I clear all the items from MyListBox?

Thanks,
DovbermanAsked:
Who is Participating?
 
Paul JacksonConnect With a Mentor Software EngineerCommented:
mylistbox.items.clear()
0
 
DovbermanAuthor Commented:
I tried this.  The items remained visible.

How do I refresh the listbox display?
0
 
Paul JacksonSoftware EngineerCommented:
This is for asp.net ?

Please post some code and explain how you are populating the listbox and trying to clear it
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
DovbermanAuthor Commented:
Yes, this is for asp.net 4.0.

        var arrFiles = (from file in System.IO.Directory.GetFiles(strSourcePath, "*.txt")
                            orderby System.IO.Path.GetFileNameWithoutExtension(file) ascending
                            select new
                            {
                                FileName = System.IO.Path.GetFileNameWithoutExtension(file),
                                FullPath = file
                            }).ToArray();

            // Load listbox from array

            for (int i = 0; i < arrFiles.Count(); ++i)
            {
                lstFilesToDo.Items.Add(arrFiles(i)FileName);
            }

            int intTotal = arrFiles.Count();
            lstFilesToDo.DataSource = arrFiles;

// This is done by clicking a different button.
// Clear the listbox

            lstFilesToDo.Items.Clear();
     

            lblStatus.Text = "Daily Update Done for " + lblAvailableSource.Text;
            cmdFilesToDo.Focus();

// I could try a for loop for the Count property

int intSentCount = lstFilesSent.Items.Count;
for intCtr =0 to intSentCount

lstFilesSent.Items.Remove(lstFilesSent.Items(intCtr));
0
 
Paul JacksonSoftware EngineerCommented:
What event are you doing the population of the listbox in, if you are doing it in the page_load you need to check for if (!Page.IsPostback) before executing the code to stop it getting populated again when you click the button to clear the listbox.
If this is not the case then try setting the source property to null :

 lstFilesToDo.Items.Clear();
lstFilesToDo.Source = null;
0
 
DovbermanAuthor Commented:
protected void cmdFilesToDo_Click(object sender, EventArgs e)

        {
          // Load listbox from array

            for (int i = 0; i < arrFiles.Count(); ++i)
            {
                lstFilesToDo.Items.Add(arrFiles(I)FileName);
            }
lstFilesToDo.Source = null;  
Error: Source is not a listbox property.
0
 
Paul JacksonSoftware EngineerCommented:
sorry meant DataSource :

 lstFilesToDo.Items.Clear();
lstFilesToDo.DataSource = null;
0
 
DovbermanAuthor Commented:
lstFilesSent.Items.Clear();
            lstFilesToDo.Items.Clear();

Works now.  I rebuilt the solution in VS 2012.

Thanks,
0
 
DovbermanAuthor Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.