Dovberman
asked on
How to clear items from a listbox
I have populated a listbox named MyListBox.
How do I clear all the items from MyListBox?
Thanks,
How do I clear all the items from MyListBox?
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This is for asp.net ?
Please post some code and explain how you are populating the listbox and trying to clear it
Please post some code and explain how you are populating the listbox and trying to clear it
ASKER
Yes, this is for asp.net 4.0.
var arrFiles = (from file in System.IO.Directory.GetFil es(strSour cePath, "*.txt")
orderby System.IO.Path.GetFileName WithoutExt ension(fil e) ascending
select new
{
FileName = System.IO.Path.GetFileName WithoutExt ension(fil e),
FullPath = file
}).ToArray();
// Load listbox from array
for (int i = 0; i < arrFiles.Count(); ++i)
{
lstFilesToDo.Items.Add(arr Files(i)Fi leName);
}
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( lstFilesSe nt.Items(i ntCtr));
var arrFiles = (from file in System.IO.Directory.GetFil
orderby System.IO.Path.GetFileName
select new
{
FileName = System.IO.Path.GetFileName
FullPath = file
}).ToArray();
// Load listbox from array
for (int i = 0; i < arrFiles.Count(); ++i)
{
lstFilesToDo.Items.Add(arr
}
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(
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;
If this is not the case then try setting the source property to null :
lstFilesToDo.Items.Clear()
lstFilesToDo.Source = null;
ASKER
protected void cmdFilesToDo_Click(object sender, EventArgs e)
{
// Load listbox from array
for (int i = 0; i < arrFiles.Count(); ++i)
{
lstFilesToDo.Items.Add(arr Files(I)Fi leName);
}
lstFilesToDo.Source = null;
Error: Source is not a listbox property.
{
// Load listbox from array
for (int i = 0; i < arrFiles.Count(); ++i)
{
lstFilesToDo.Items.Add(arr
}
lstFilesToDo.Source = null;
Error: Source is not a listbox property.
sorry meant DataSource :
lstFilesToDo.Items.Clear() ;
lstFilesToDo.DataSource = null;
lstFilesToDo.Items.Clear()
lstFilesToDo.DataSource = null;
ASKER
lstFilesSent.Items.Clear() ;
lstFilesToDo.Items.Clear() ;
Works now. I rebuilt the solution in VS 2012.
Thanks,
lstFilesToDo.Items.Clear()
Works now. I rebuilt the solution in VS 2012.
Thanks,
ASKER
Thank you.
ASKER
How do I refresh the listbox display?