Solved

How to clear items from a listbox

Posted on 2013-11-29
9
322 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
  • 5
  • 4
9 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 300 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

840 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