?
Solved

How to clear items from a listbox

Posted on 2013-11-29
9
Medium Priority
?
327 Views
Last Modified: 2013-11-29
I have populated a listbox named MyListBox.

How do I clear all the items from MyListBox?

Thanks,
0
Comment
Question by:Dovberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 1200 total points
ID: 39686011
mylistbox.items.clear()
0
 

Author Comment

by:Dovberman
ID: 39686019
I tried this.  The items remained visible.

How do I refresh the listbox display?
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39686037
This is for asp.net ?

Please post some code and explain how you are populating the listbox and trying to clear it
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:Dovberman
ID: 39686069
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
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39686124
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
 

Author Comment

by:Dovberman
ID: 39686144
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
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39686150
sorry meant DataSource :

 lstFilesToDo.Items.Clear();
lstFilesToDo.DataSource = null;
0
 

Author Comment

by:Dovberman
ID: 39686167
lstFilesSent.Items.Clear();
            lstFilesToDo.Items.Clear();

Works now.  I rebuilt the solution in VS 2012.

Thanks,
0
 

Author Closing Comment

by:Dovberman
ID: 39686169
Thank you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question