1
我正在研究一个应用程序,该应用程序显示进入Outlook邮箱的电子邮件报告。我已经用一个丰富的文本框设置它,根据电子邮件类型来格式化文本的前景色或背景色。我想添加功能让用户从显示屏中选择电子邮件。带RichText格式的列表框功能
足够简单的使用列表框...但保持格式化...使其成为一个所有者绘制的固定列表框。
大部分时间这工作正常;直到用户尝试选择电子邮件。
这里是发生了什么
在双击:
- 获得选择的项目,并转换为信息从列表
- 做
- 清楚所选项目拉动outlook.mailItem什么到邮件项目(传递给另一个功能,其中 用户选择他/她想要做什么)
- 呼叫更新
更新
- 明确在列表框中
- 项目拉动电子邮件并将它们添加回列表框中
某处在更新,应用程序锁起来我不知道为什么。当作为“正常”应用程序进程的一部分调用时,报告加载得很好,只在用户交互后调用时才锁定。
75%的时间,我没有错误处理程序,Catch (exception e2)
被调用。但是,偶尔我会得到和索引错误投掷值-1。从逻辑上讲,如果它在一种情况下起作用,它应该在第二种情况下起作用,但证据表明情况并非如此。
任何有关在何处寻找错误的建议,或是探索实现相同目标的不同路线?
private void listBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Graphics g = e.Graphics;
SolidBrush textColor = new SolidBrush(Color.Black);
SolidBrush backColor = new SolidBrush(Color.White);
try
{
ListBox box = (ListBox)sender;
mailInfo email = (mailInfo)box.Items[e.Index];
switch (email.getTypeCode())//based on the e-mail type formats the line
{
case 1:// ejected
textColor = new SolidBrush(Color.LightGray);
backColor = new SolidBrush(Color.White);
break;
case 2://Multi volume
textColor = new SolidBrush(Color.SteelBlue);
backColor = new SolidBrush(Color.White);
break;
case 0://success
textColor = new SolidBrush(Color.DarkGreen);
backColor = new SolidBrush(Color.White);
break;
case -3://not inserted
textColor = new SolidBrush(Color.LightSalmon);
backColor = new SolidBrush(Color.White);
break;
case -2:// not scratch
case -1:// fail
case -4:// write portected
case -5:// not in DB
textColor = new SolidBrush(Color.White);
backColor = new SolidBrush(Color.Red);
break;
default:
textColor = new SolidBrush(Color.Black);
backColor = new SolidBrush(Color.White);
break;
}
}
catch (Exception e2) { MessageBox.Show(e2.Message + "\n\ncode\n" + e2.StackTrace, e2.GetType().ToString()); }
g.FillRectangle(backColor, e.Bounds);
if(e.State == DrawItemState.Selected) {
g.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font, SystemBrushes.MenuHighlight,new PointF(e.Bounds.X, e.Bounds.Y)); }
else {
g.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font, textColor, new PointF(e.Bounds.X, e.Bounds.Y));
}
e.DrawFocusRectangle();
}
private void lstEmail_DoubleClick_1(object sender, EventArgs e)
{
mailInfo email = (mailInfo) lstEmail.SelectedItem;
lstEmail.ClearSelected();
frmEmailSelectedOptions userAction = new frmEmailSelectedOptions();
userAction.ShowDialog(this);
switch (userAction.value)
{
case 0://ignore
// allow function to close, no action neededd
break;
case 1://read/unread
case 2://open
data.performEmailAction(email,userAction.value);
MessageBox.Show(userAction.value.ToString());
break;
case 3://filter
this.filter = new mailFilter(email.getHost().getName(), email.getJobName());
updateEmailList();
break;
}
}public void updateEmailList()/***/
{
lstEmail.Items.Clear();
txtFilter.Text = filter.ToString();// updates the fileter display to the current filter
List<mailInfo> emails = data.getMail();// pulls the e-mails from the application memory
if (!btnSortByTime.Checked)//checks if the data needs to be re-sorted
{
emails.Sort((x, y) => x.getHost().getOrder().CompareTo(y.getHost().getOrder()));
}
foreach (mailInfo email in emails)//iterates over each e-mail in the application memory
{
if (displayEmail(email))//checks if the e-mails passes all the criteria
{
lstEmail.Items.Add(email);//adds the e-mail to the list, plus a line return for the next e-mail.
}
}
}`
什么是例外,你真的从'box.Items [e.Index];''得到异常吗? –
InvalidArgument ='-1'的值对'index'无效。 参数名称:索引 at System.Windows.FormsListBox.ObjectCollection.get_Item(int32 index) at [...] frmMain.listbox_DrawItem(object sender,drawItemEventArgs e) –