【问题标题】:c# listbox, ownerdrawfixed, get selected itemc# listbox,ownerdrawfixed,获取选中项
【发布时间】:2017-04-25 20:22:03
【问题描述】:

我的程序在文件中运行搜索,当找到该字符串时,它会在另一个字符串之间过滤它们,然后在列表框中以相应的颜色显示它们(红色表示坏行由第二个过滤器确定),绿色表示好的,列表框显示位置\文件,并在另一行显示行本身。

现在我想通过在 location\file 上的 dubbelclicking 打开文件但是我的代码在 ownerdrawfixed 中不起作用(我需要/想要颜色) 我试过了

string filename = listBox1.GetItemText(listBox1.selectedItem);
if (file.exists(filename))//to check if i click on a filename or on a line
  {
  try
    {
      System.diagnostics.process.start("scite.exe",filename); //open file with scite
    }
   catch
   {
     system.Diagnostics.Process.start(filename);//open file with windows default
   }

我了解到字符串“文件名”现在包含“Datscan.Form1+MyListboxItem”

找到了很多关于如何将绘图模式设置为正常的答案,但我需要它在 ownerdrawfixed 中。

【问题讨论】:

  • 是什么不工作?当it does not work 时会发生什么?你如何设置双击事件处理程序?只是为了 s* 和咯咯笑,以及故障排除,如果您不使用 ownerdrawfixed,它是否有效?
  • 与 ownerdraw 无关。您的 MyListboxItem 类应覆盖 ToString()。因此,您可以使用 listBox1.SelectedItem.ToString()。 ownerdraw 不是绝对必要的,但如果你不这样做,那么你必须使用强制转换,比如 CType(listBox1.selectedItem, MyListboxItem).Filename,假设 Filename 是准确的(我们看不到它)。最好覆盖 ToString()。

标签: c# winforms listbox listboxitem


【解决方案1】:

这是我刚刚整理的一个最小示例,希望能帮助您缩小问题范围。它不执行选择,而是在列表框旁边的标签中显示选择。但这只是为了向您展示正确的值被拉出。然后将值路由到可以执行它的东西而不是显示它只是微不足道的。

using System.Drawing;
using System.Windows.Forms;


namespace ListBox_15948283
{
    public partial class Form1 : Form
    {
        private int ItemMargin = 5;
        private int labelCount = 0;

        public Form1()
        {
            InitializeComponent();
            ListBox lb = new ListBox();//initialize a new ListBox
            lb.DrawMode = DrawMode.OwnerDrawFixed;//lets meet YOUR requirement of OwnerDrawFixed
            lb.Location = new Point(25, 25);//position the ListBox
            lb.DrawItem += Lb_DrawItem;//give it a Draw event handler
            lb.MeasureItem += Lb_MeasureItem;//give it a MeasureItem event handler
            lb.Name = "lstbx_Yep";//give the listbox a name so we can call it later
            lb.Items.Add("Option 1");//add an option
            lb.Items.Add("Option 2");//add an option
            lb.Items.Add("Option 3");//add an option
            lb.Items.Add("Option 4");//add an option
            lb.MouseDoubleClick += Lb_MouseDoubleClick;//add a doubleClick event handler on the ListBox
            this.Controls.Add(lb);//add the listbox to the form
        }

        private void Lb_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListBox lb = ((ListBox)this.Controls.Find("lstbx_Yep", false)[0]);//get the listbox
            Label lbl = new Label();//make a label to display the result
            lbl.Location = new Point(150, (labelCount * 25) + 50);//position the label
            lbl.Text = lb.SelectedItem.ToString();//get the selected item
            this.Controls.Add(lbl);//add the label to the form
            labelCount++;

        }

        /*
         * Code below taken from
         * http://csharphelper.com/blog/2014/11/make-an-owner-drawn-listbox-in-c/
         */
        private void Lb_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            // Get the ListBox and the item.
            ListBox lst = sender as ListBox;
            string txt = (string)lst.Items[e.Index];

            // Measure the string.
            SizeF txt_size = e.Graphics.MeasureString(txt, this.Font);

            // Set the required size.
            e.ItemHeight = (int)txt_size.Height + 2 * ItemMargin;
            e.ItemWidth = (int)txt_size.Width;
        }

        private void Lb_DrawItem(object sender, DrawItemEventArgs e)
        {
            // Get the ListBox and the item.
            ListBox lst = sender as ListBox;
            string txt = (string)lst.Items[e.Index];

            // Draw the background.
            e.DrawBackground();

            // See if the item is selected.
            if ((e.State & DrawItemState.Selected) ==
                DrawItemState.Selected)
            {
                // Selected. Draw with the system highlight color.
                e.Graphics.DrawString(txt, this.Font,
                    SystemBrushes.HighlightText, e.Bounds.Left,
                        e.Bounds.Top + ItemMargin);
            }
            else
            {
                // Not selected. Draw with ListBox's foreground color.
                using (SolidBrush br = new SolidBrush(e.ForeColor))
                {
                    e.Graphics.DrawString(txt, this.Font, br,
                        e.Bounds.Left, e.Bounds.Top + ItemMargin);
                }
            }

            // Draw the focus rectangle if appropriate.
            e.DrawFocusRectangle();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    相关资源
    最近更新 更多