2016-04-26 76 views
0

我創建了一個winform自定義控件,它具有共享相同綁定源的文本框和列表框,以便可以使用文本框輸入過濾列表框。winform listbox drawitem更改子串顏色

我需要覆蓋lisbox drawitem,以便過濾後的項目將搜索到的文本作爲子字符串變爲不同顏色或突出顯示。 (即,)期望黃色突出顯示,如下面的示例圖像。 sample reference

我做了如下

private void DrawItemHandler(object sender, DrawItemEventArgs e) 
     { 
      this.Invoke((MethodInvoker)delegate 
      { 
       e.DrawBackground(); 
       e.DrawFocusRectangle(); 

       string MyString = listBox.GetItemText(listBox.Items[e.Index]); 
       string stringToFind = searchInput.Text ; 

       if (!string.IsNullOrEmpty(stringToFind)) 
       { 
        List<int> positions = new List<int>(); 
        int pos = 0; 
        while ((pos < MyString.Length) && (pos = MyString.IndexOf(stringToFind, pos, StringComparison.InvariantCultureIgnoreCase)) != -1) 
        { 
         positions.Add(pos); 
         pos += stringToFind.Length; 
        } 

        int c = 0, nLen = 0, width = 0; 
        Rectangle rect = e.Bounds; 
        rect.X = width; 
        do 
        { 
         if (positions.Contains(c)) 
         { 
          //int opacity = 128; 
          e.Graphics.DrawString(MyString.Substring(c, stringToFind.Length), 
                e.Font, 
           //new SolidBrush(Color.FromArgb(opacity, Color.LightYellow)), 
                new SolidBrush(Color.LightYellow), 
                rect); 
          nLen = MyString.Substring(c, stringToFind.Length).Length; 
          width += nLen; 
         } 
         else 
         { 
          e.Graphics.DrawString(MyString[c].ToString(), 
          e.Font, 
          new SolidBrush(listBox.ForeColor), 
          rect); 
          nLen = MyString[c].ToString().Length; 
          width += nLen; 
         } 
         rect.X = width; 
        } 
        while ((c += nLen) < MyString.Length); 
       } 
       else 
       { 
        e.Graphics.DrawString(MyString, 
         e.Font, 
         new SolidBrush(listBox.ForeColor), 
         e.Bounds); 
       } 

      }); 

     } 

,結果是該項目的文本是覆蓋字符。

initially after search

我不能識別錯誤的部分,它是在矩形範圍或束帶的部分。除了項目背景顏色之外,如何更改項目文本中子字符串的背景。請幫助我。

+1

要更改子的研究背景,我懷疑你必須首先使用FillRectangle,然後的DrawString在該矩形 – Pikoh

+1

要使用自定義背景色使用TextRenderer而不是DrawString!您前進rectangle.X的方法是使用字符串中的字符長度而不是像素長度。使用Graphics.MeasureString(... Typographics)查找高亮部分的寬度。 – TaW

+0

嘗試使用FillRectangle建議的MeasureString,我可以看到差異,但仍然需要調整。多謝你們。 – madmonk88

回答

2

嗯,這個任務並不像應該那麼容易,因爲TextRenderer.MeasureTextGraphics.MeasureString似乎都不是很準確。但使用不同的超載Graphics.MeasureString傳遞矩形寬度和StringFormat.GenericTypographic它似乎工作更好一點。

這是我對你的問題的嘗試,希望它有助於:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     ListBox listBox = (ListBox)sender; 
     this.Invoke((MethodInvoker)delegate 
     { 
      e.DrawBackground(); 
      e.DrawFocusRectangle(); 

      string MyString = listBox.GetItemText(listBox.Items[e.Index]); 
      string stringToFind = searchInput.Text; 


      if (!string.IsNullOrEmpty(stringToFind)) 
      { 
       string[] strings = MyString.Split(new string[] { stringToFind }, StringSplitOptions.None); 

       Rectangle rect = e.Bounds; 

       for (int i=0;i<strings.Length;i++) 
       { 
        string s = strings[i]; 
        if (s != "") 
        { 
         int width = (int)e.Graphics.MeasureString(s, e.Font,e.Bounds.Width, StringFormat.GenericTypographic).Width; 
         rect.Width = width; 
         TextRenderer.DrawText(e.Graphics, s, e.Font, new Point(rect.X, rect.Y), listBox.ForeColor); 
         rect.X += width; 
        } 

        if (i < strings.Length - 1) 
        { 
         int width2 = (int)e.Graphics.MeasureString(stringToFind, e.Font, e.Bounds.Width, StringFormat.GenericTypographic).Width; 
         rect.Width = width2; 
         TextRenderer.DrawText(e.Graphics, stringToFind, e.Font, new Point(rect.X, rect.Y), listBox.ForeColor, Color.Yellow); 
         rect.X += width2; 
        } 
       } 
      } 
      else 
      { 
       TextRenderer.DrawText(e.Graphics, MyString, e.Font, new Point(e.Bounds.X, e.Bounds.Y), listBox.ForeColor); 
      } 

     }); 


    } 
+0

終於!!!我用DrawString一路試過,但在搜索模式中的字符之間留有空白。你的解決方案是現貨。 – madmonk88

+0

很高興幫助:) – Pikoh

+0

是的,這是最好的組合,imo。 – TaW