Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, May 13, 2008 10:19 AM
I am using asp.net with c#. I have a listbox with selection mode as multiple. I stored my selected items from my listbox into an arraylist. Now I want my stringbuilder variable to store my arraylist items. How can I do this?
Here is what I have so far.
StringBuilder txtValue1 = new StringBuilder();
ListBox kwlbxList = new ListBox();
ArrayList ListArray = new ArrayList();
if (kwlbxList.SelectedIndex >= 0)
{
for (int i = 0; i < kwlbxList.Items.Count; i++)
{
if (!ListArray.Contains(kwlbxList.Items[i].Selected))
{
ListArray.Add(kwlbxList.Items[i]);
txtValue1 = txtValue1 + " " + ListArray.ToString();
}
}
}
What am i doing wrong, I am getting Object[] placed inside my stirng variable.
All replies (5)
Tuesday, May 13, 2008 11:02 AM âś…Answered
Here is some code which I think does what you need it to:
StringBuilder txtValue1 = new StringBuilder();
// ListBox kwlbxList = new ListBox();
ArrayList ListArray = new ArrayList();
//if (kwlbxList.SelectedIndex >= 0)
//{
// for (int i = 0; i < kwlbxList.Items.Count; i++)
// {
// if (!ListArray.Contains(kwlbxList.Items[i].Selected))
// {
// ListArray.Add(kwlbxList.Items[i]);
// //txtValue1 = txtValue1 + " " + ListArray.ToString();
// foreach(string item in ListArray)
// txtValue1.AppendLine(item);
// }
// }
// lblStringBuilderResults.Text = txtValue1.ToString();
//}
// try it this way
for (int i = 0; i <= kwlbxList.Items.Count - 1; i++)
{
if (kwlbxList.Items[i].Selected)
{
// add to arraylist
ListArray.Add(kwlbxList.Items[i].Value);
}
}
// now add each item in the ArrayList to the string builder
foreach (object obj in ListArray)
{
txtValue1.Append(obj.ToString());
}
// display the results for testing purposes
lblStringBuilderResults.Text = txtValue1.ToString();
Tuesday, May 13, 2008 10:41 AM
This is because the stringbuilder object isn't like a simple string value, where you append to the end of the string when you want to add something to it. There are a few items in your code that needs to be addressed:
1.) The following line of code lookeds to see if the ArrayList contains "false", because the first index (zero) is never evaluated (becaus tyou check to see if the listbox has a selected index greater than zero)...
if (!ListArray.Contains(kwlbxList.Items[i].Selected)) // checks to see if the arraylist contains ("false")
so I think perhaps you meant to only add items selected in your listbox to the array list, right?
2.) txtValue1 = txtValue1 + " " + ListArray.ToString();
should be
txtValue1.AppendLine("some value")
if you can place some debug points in your code, you should be able to determine where the breakdown is.
Tuesday, May 13, 2008 10:44 AM
When you call ListArray.ToString(), or any collection where the ToString() isn't implicitly overridden to call each member's ToString(), it will just return the type name (which is why you would see "Object[]"). You need to be more specific about what you want to append to your string builder ("((ListBox)ListArray[0]).SelectedValue;").
You are doing several things I wanted to mention, which caught my attention.
First, you may need 2 for loops - one to iterate thru each ListItem to see if you need to add it to your ArrayList, and another to append values to your StringBuilder.
Second, "if (!ListArray.Contains(kwlbxList.Items[i].Selected))" is literally checking if your ListArray does not contain true or false. Did you actually mean SelectedValue or SelectedText?
Finally, and this is just a personal preference, it would appear more intuitive if you were calling the StringBuilder's Append() method to concatenate, instead of the traditional string replacement / concatenation syntax.
Tuesday, May 13, 2008 10:47 AM
kwlbxList hava not items???
Tuesday, May 13, 2008 10:51 AM
When using the StringBuilder you should use the method Append as shown below. I am not sure why you want to store ListBox items into an ArrayList and then to a StringBuilder. You can simply take the ListBox items and directly put to the StringBuilder. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p> StringBuilder sb = new StringBuilder();
ArrayList ListArray = new ArrayList();
if (ListBox1.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (!ListArray.Contains(ListBox1.Items[i].Selected))
{
ListArray.Add(ListBox1.Items[i]);
// put listbox items to StringBuilder
sb.Append(ListBox1.Items[i].Value.ToString());
}
}
}
txtStringBuilder.Text = sb.ToString();