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) –