【问题标题】:Listbox functionality, with RichText Formatting列表框功能,带有 RichText 格式
【发布时间】:2017-07-26 15:18:34
【问题描述】:

我正在开发一个应用程序,该应用程序显示进入 Outlook 邮箱的电子邮件的报告。我已经设置了一个富文本框,以根据电子邮件类型格式化文本的前景色或背景色。我想为用户添加从显示中选择电子邮件的功能。

使用列表框很简单...但保留格式...使其成为所有者绘制的固定列表框。

大部分时间都可以正常工作;直到用户尝试选择电子邮件。

这就是发生的事情

双击时:

  1. 获取选定的项目并投射到信息以拉取outlook.mailItem
  2. 从列表中清除所选项目
  3. 对邮件项做任何事情(传递给另一个函数,其中 用户选择他/她想做的事)
  4. 通话更新

更新

  1. 清除列表框中的项目
  2. 提取电子邮件并将它们添加回列表框

在更新的某个地方,应用程序被锁定,我不知道为什么。当作为“正常”应用程序进程的一部分调用时,报表加载得很好,它只是在用户交互后调用时锁定。

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' 无效。参数名称: System.Windows.FormsListBox.ObjectCollection.get_Item(int32 index) at [...]frmMain.listbox_DrawItem(object sender, drawItemEventArgs e) 处的索引

标签: c# listbox background-color ownerdrawn


【解决方案1】:

通过检查列表框是否有焦点解决了问题。如果列表框有焦点,我选择了另一个控件。如果没有保持焦点不变。这解决了问题。

【讨论】:

    猜你喜欢
    • 2019-11-22
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    相关资源
    最近更新 更多