2017-04-25 109 views
0

我的程序在文件中運行搜索,當找到字符串時,它會在另一個字符串之間過濾它們,然後在相應顏色的列表框中顯示它們(紅色代表壞行由第二個過濾器),綠色爲好,列表框顯示位置\文件,並在不同的行上顯示行本身。c#listbox,ownerdrawfixed,獲取所選項

現在我想通過dubbelclicking上的位置\打開文件filebut我的代碼不工作在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設置爲正常,但我需要它在ownerdrawfixed。

+0

這是什麼不起作用?當它不起作用時會發生什麼?你如何設置雙擊事件處理程序?如果你不使用'ownerdrawfixed',它會工作嗎?只是爲了S *和咯咯,並排除故障。 –

+0

與ownerdraw無關。你的MyListboxItem類應該覆蓋ToString()。所以你可以使用listBox1.SelectedItem.ToString()。對於ownerdraw來說不是必須的,但是如果你不這樣做,那麼你必須使用類型轉換,比如CType(listBox1.selectedItem,MyListboxItem).Filename,假設文件名是準確的(我們看不到它)。重寫ToString()是最好的。 –

回答

0

這是我剛纔放在一起的一個小例子,希望能幫助你縮小問題的範圍。它不會執行選擇,而是將選擇顯示在ListBox旁邊的標籤中。但這只是爲了告訴你,適當的價值被拉出。然後,將值傳遞給EXE執行它,而不是顯示它是微不足道的。

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