Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm new to wpf. I want to make the items in a listbox, which matches items in a string array, as selected (using c# coding). I'm giving my code here:
C#
string str1 = myReader["Books"].ToString();
                string[] arr = str1.Split(',');
                foreach (string word in arr)
                {
                   foreach (ListBoxItem x in listBox1.Items)
                        {
                            if (x.Content.ToString() == word)
                            {
                                listBox1.SelectedItems.Add(x);
                            }
                        }
                }


The content of string str1 is 'Book1,Book2,Book3,Book4'.
But the problem is, I've to do it using a single foreach loop. ie., I've to change the line 'foreach (ListBoxItem x in listBox1.Items)' as some if condition.
Can anyone help me???
Posted
Updated 16-Dec-12 19:46pm
v2
Comments
[no name] 17-Dec-12 1:47am    
like, you want to add book1, book2 etc in Listbox ??
Juhi saf 26-Dec-12 0:32am    
No, I've to use an observablecollection and bind it to the listbox. But still its not working correctly. I'll attatch the modified code here.
namespace WpfApplication1
{
public class Class1 : INotifyPropertyChanged
{

private ObservableCollection<book> b1 = new ObservableCollection<book>();
public ObservableCollection<book> B1
{
get
{
return b1;
}
set
{
b1 = value;
OnPropertyChanged("B1");
}
}


public Class1()
{
LoadBookList();

}
private void LoadBookList()
{
try
{
SqlConnection con1 = new SqlConnection("Data Source=.; Initial Catalog= test; User ID= sa; Password=nest123@!;");
con1.Open();
SqlCommand cmd1 = new SqlCommand("select Books from authorinfo where Id=(select max(Id) from authorinfo)", con1);
SqlDataReader myReader = cmd1.ExecuteReader();
while (myReader.Read())
{
string str1 = myReader["Books"].ToString();
string[] arr = str1.Split(',');
foreach (string word in arr)
{
b1.Add(new Book(word));
}
}
myReader.Close();
myReader.Dispose();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex.Message);
}
}


public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyname)
{

if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
}
public class Book: INotifyPropertyChanged
{
private string item;
public string ITEM
{
get
{
return item;
}
set
{
item = value;
OnPropertyChanged("ITEM");
}
}

public Book(string x)
{
item = x;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyname)
{

if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
}
}

1 solution

Check this modified code.

C#
string str1 = myReader["Books"].ToString();
string[] arr = str1.Split(',');
foreach (string word in arr)
{
  int index = listbox1.FindString(word)
  if (index != -1 )
  {
     listBox1.SelectedItems.Add(word);
  }
 
  }
};


Read more about ListBox.FindString @ MSDN[^]
 
Share this answer
 
Comments
Juhi saf 26-Dec-12 0:34am    
Thank you. But the FindString() method is not working here.
Juhi saf 26-Dec-12 0:36am    
I've to do it by using observable collection.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900