Click here to Skip to main content
15,886,705 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I wrote the following method to get CheckedListBox Items and do some modifications, but it doesn't loop inside the
C#
foreach (object o in lstKeyWordsForSearch.CheckedItems)
and return null value
C#
public List<string> GetKeyWordsFormList()
{
    List<string> words = new List<string>();
    foreach (object o in lstKeyWordsForSearch.CheckedItems)
    {
        string s = o.ToString().ToLower();
        if (s != null)
        {
            string[] separated = s.Split(',');
            string[] arrremove = null;
            frmStopWordList stopWords = new frmStopWordList();
            arrremove = stopWords.getStopWords();
            for (int i = 0; i < separated.Length; i++)
            {
                for (int j = 0; j < arrremove.Length; j++)
                {
                    if (separated[i] == arrremove[j])
                    {
                        separated[i] = "";
                    }
                }
            } // remove stop words
            foreach (string word in separated)
            {
                if (!string.IsNullOrEmpty(word))
                {
                    if (!words.Contains(word))
                    {
                        words.Add(word);
                    }
                }
            }
        }
    }
    return words;
}</string></string></string>

where lstKeyWordsForSearch declared as name of CheckedListBox and I am getting that return value from another windows Form like following:
C#
frmMainForm mainForm = new frmMainForm();
List<string> SearchKeyWords = mainForm.GetKeyWordsFormList();
foreach (string item in SearchKeyWords)
{
    MessageBox.Show(item);
}</string>

but MessageBox.Show(item); doesn't show anything when I call that public List<string> GetKeyWordsFormList()</string> method in the same form it gives output but when I call from another form it gives nothing.
Posted
Updated 18-Aug-10 9:36am
v5
Comments
Niklas L 18-Aug-10 12:54pm    
..and what is lstKeyWordsForSearch declared as?
Kasunmit 18-Aug-10 12:56pm    
thnx for ur attention lstKeyWordsForSearch declared as name of checked list box pls help
Toli Cuturicu 18-Aug-10 15:37pm    
Corrected some spelling and code formatting.

Do you have any items in the checked list box checked?

if you do not have any items checked in it then the foreach would be completing as designed by not processing
 
Share this answer
 
v2
Comments
Kasunmit 18-Aug-10 13:37pm    
yes i am having 5 items ..
Toli Cuturicu 18-Aug-10 15:38pm    
Reason for my vote of 2
no answer
fatalwall 18-Aug-10 20:03pm    
How many items do you have loaded into the listboxselect. How many of the itesm do you have marked as selected? Based on the code you show here none of them are actually marked as selected. If none of them are marked as selected then you would no get anything returned.
This is your problem:
C#
frmMainForm mainForm = new frmMainForm();
List SearchKeyWords = mainForm.GetKeyWordsFormList();

You are not calling "frmMainForm.ShowDialog" before you call "mainForm.GetKeyWordsFromList".
 
Share this answer
 
Comments
Kasunmit 18-Aug-10 14:57pm    
no i dont need to load the mainForm .. from main form i getting that list items to next form using method ..
AspDotNetDev 18-Aug-10 15:06pm    
Huh? Post the code for your frmMainForm constructor. I doubt it is checking items in ITS lstKeyWordsForSearch.

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