簡體   English   中英

C#列表框,ownerdrawfixed,獲取選定的項目

[英]c# listbox, ownerdrawfixed, get selected item

我的程序在文件中進行搜索,當找到該字符串時,它會在另一個字符串之間進行過濾,然后在列表框中顯示相應的顏色(紅色表示由第二個過濾器確定的壞行),綠色表示相應的顏色,列表框顯示location \\ file,並在另一行顯示行本身。

現在我想通過dubbelclicking location \\ file來打開文件,但是我的代碼在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”

找到了很多關於如何將drawmode設置為normal的答案,但是我需要ownerdrawfixed中使用它。

這是我整理的一個最小示例,希望可以幫助您縮小問題范圍。 它不執行選擇,而是在列表框旁邊的標簽中顯示選擇。 但這只是向您顯示適當的值被提取出來。 然后,將值路由到將執行該值而不是顯示該值的瑣事是微不足道的。

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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM