我的项目中有 2 个 ListView控件,其中一个工作得很好,另一个直到最近才知道为什么它的行为非常奇怪。
有时它不会识别选中的属性,有时它会在我们获得选中的属性时返回 "" 有时工作正常并返回True或False。
访问 ListBox 的代码如下,请记住我有 2 个工作得非常好,它具有相同的代码来获取检查状态。
当其他人工作时,当获取中间列的子文本总是返回“”时,它也开始遇到问题,所以我必须更改代码以从数据表中获取所需的输出。
更新
此外,当我处于调试模式并逐行运行时,它每次都能正常工作并获得价值
for (int i = 0; i < listView1.Items.Count - 1; i++)
{
// string tmpVal = string.Empty;
bool tmpVal = false;
if (listView1.InvokeRequired)
{
listView1.BeginInvoke(new MethodInvoker(
() =>
tmpVal = listView1.Items[i].Checked
));
}
else
{
tmpVal = listView1.Items[i].Checked;
}
if (tmpVal == true)
{
string senderEMail = string.Empty;
if (listView1.InvokeRequired)
{
listView1.BeginInvoke(new MethodInvoker(
() =>
senderEMail = listView1.Items[i].SubItems[1].Text.ToString()
));
}
else
{
senderEMail = listView1.Items[i].SubItems[1].Text.ToString();
}
int rowNumber = 0;
string rNumber = string.Empty;
if (listView1.InvokeRequired)
{
listView1.BeginInvoke(new MethodInvoker(
() =>
rNumber = listView1.Items[i].SubItems[3].Text.ToString()
));
}
else
{
rNumber = listView1.Items[i].SubItems[3].Text.ToString();
}
rowNumber = Convert.ToInt16(rNumber.Trim());
if (checkBox2.Checked)
{
//GET UNSUBSCRUBE URL IF ANY HIT IT AND ALSO SEND EMAIL
string unSubscribeUrl = resultSheet.Rows[rowNumber - 1]["UnsubscribeLink"].ToString();
string receiver = resultSheet.Rows[rowNumber - 1]["receiver"].ToString();
if (unSubscribeUrl != "")
{
unSubscribeUrl = unSubscribeUrl.Replace("amp;", "").Trim();
string html = getHtml(unSubscribeUrl);
}
//SENDING EMAIL
receiver = receiver.ToLower();
OutLook.Application oApp1 = new OutLook.Application();
OutLook.MailItem mail = oApp1.CreateItem(OutLook.OlItemType.olMailItem) as OutLook.MailItem;
mail.Subject = "unsubscribe | uitschrijven | gelieve geen mails te sturen";
mail.To = senderEMail;
SetAccount_2007_2010(mail, receiver);
Marshal.ReleaseComObject(oApp1);
}
if (checkBox3.Checked)
{
deleteSenderList.Add(senderEMail);
}
}
}