2016-04-26 5 views
0

私はwinboxカスタムコントロールを作成しました。このコントロールには、同じバインディングソースを共有するテキストボックスとリストボックスがあり、リストボックスをテキストボックス入力でフィルタリングできるようになっています。winform listbox drawitem changeサブストリングの色

フィルタリングされたアイテムが部分文字列としてテキストを検索して異なる色または強調表示されるように、lisboxドローアイテムをオーバーライドする必要があります。 (例:)以下のサンプル画像のような黄色のハイライトが必要です。 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を使用する必要があります容疑者と、カスタムのBackColor使用を使用するには、その長方形 – Pikoh

+1

上巾着DrawStringではなくTextRenderer! 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

うん、これは、最高の作品、今の組み合わせです。 – TaW

関連する問題