2017-03-06 5 views
1

Outlookのメールボックスに送られる電子メールのレポートを表示するアプリケーションで作業しています。私は電子メールの種類に応じてテキストの前景色または後色をフォーマットするリッチテキストボックスを設定しました。私はユーザーがディスプレイから電子メールを選択する機能を追加したかったのです。リッチテキスト形式のリストボックス機能

リストボックスを使用するだけで簡単ですが、書式設定を維持してください...所有者が描画したリストボックスにします。

ほとんどの場合、これは問題なく動作します。ユーザーが電子メールを選択しようとするまで。ここ

ダブルクリックで

を何が起こるかです:outlook.mailItemを引っ張るため

  1. は、情報に選択した項目とキャストを取得
  2. 明確なリストから選択した項目
  3. ものは何でも(別の機能に渡され、ユーザーが何をしたいかを が選択した)
  4. 通話の更新

  1. 明確なアップデートのリストボックス
  2. 内の項目は、電子メールを引き出し、アップ、更新に

どこかはlistBoxに戻すアプリケーションロックをそれらを追加し、なぜ私は分からない。 「通常の」アプリケーションプロセスの一部として呼び出されると、レポートはうまく読み込まれ、ユーザー操作後に呼び出されるとロックされます。

時間の75%、私が持っているエラーハンドラのどれも、Catch (exception e2)が呼び出されます。しかし、ときどき私は取得し、-1の値をスローするインデックスエラー。

論理的には、1つのケースで動作する場合、2番目のケースで動作するはずですが、証拠にはそうではないことが示されています。

同じ目的を達成するために、どこを探してエラーを探すか、または別のルートを探索するための提案はありますか?

 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. 
      } 
     } 
    }` 
+0

例外は、実際には 'box.Items [e.Index];'の例外を受け取りますか? –

+0

InvalidArgument = '-1'の値は 'index'には無効です。 パラメータ名:index at System.Windows.FormsListBox.ObjectCollection.get_Item(int32 index) at [...] frmMain.listbox_DrawItem(オブジェクト送信者、drawItemEventArgs) –

答えて

0

リストボックスにフォーカスがあるかどうかを確認して問題を解決しました。リストボックスにフォーカスがある場合は、別のコントロールを選択しました。フォーカスをそのままにしない場合。これで問題は解決しました。

関連する問題