Solved

How to clear items from a listbox

Posted on 2013-11-29
9
325 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 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

705 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