获取列表框项方法返回空值 [英] get listbox items method returns null value
问题描述
我编写了以下方法来获取CheckedListBox
Items
并进行一些修改,但是它并没有在
I wrote the following method to get CheckedListBox
Items
and do some modifications, but it doesn''t loop inside the
foreach (object o in lstKeyWordsForSearch.CheckedItems)
,然后返回
and return null
value
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>
其中lstKeyWordsForSearch
声明为CheckedListBox
的名称,我从另一个Windows窗体获取该返回值 ,如下所示:
where lstKeyWordsForSearch
declared as name of CheckedListBox
and I am getting that return value from another windows Form like following:
frmMainForm mainForm = new frmMainForm();
List<string> SearchKeyWords = mainForm.GetKeyWordsFormList();
foreach (string item in SearchKeyWords)
{
MessageBox.Show(item);
}</string>
但是当我调用 public List< string>时,
方法可以提供输出,但是当我从另一种形式进行调用时,它什么也没有提供.MessageBox.Show(item);
不会显示任何内容.具有相同形式的GetKeyWordsFormList()</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.
推荐答案
选中的列表框中是否有任何项目?
如果您没有检查任何项目,则foreach将按不进行处理的设计完成
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
这是您的问题:
This is your problem:
frmMainForm mainForm = new frmMainForm();
List SearchKeyWords = mainForm.GetKeyWordsFormList();
在调用"mainForm.GetKeyWordsFromList"之前,您没有调用"frmMainForm.ShowDialog".
You are not calling "frmMainForm.ShowDialog" before you call "mainForm.GetKeyWordsFromList".
这篇关于获取列表框项方法返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!