0

ListBox "txtAcao" の DrawItem にこのメソッドがありますが、正しく動作していません:

  private void txtAcao_DrawItem(object sender, DrawItemEventArgs e)
    {
        MyListBoxItem item = txtAcao.Items[e.Index] as MyListBoxItem; // Get the current item and cast it to MyListBoxItem
        if (item != null)
        {
            int index = 0; //Posicoes de inicio do texto
            //Pintando a nome de preto
            string message = item.Message.Substring(0, item.Message.IndexOf(":") + 1);
            e.Graphics.DrawString(message, txtAcao.Font, new SolidBrush(Color.Black), new PointF(index, e.Index * txtAcao.ItemHeight));

            index = item.Message.IndexOf(":") + 1;

            message = item.Message.Substring(index, item.Message.IndexOf("->") - index).Trim();
            index = item.Message.IndexOf(message);
            //Pintando o valor antigo
            e.Graphics.DrawString(message, txtAcao.Font, new SolidBrush(item.ItemColor), new PointF(index, e.Index * txtAcao.ItemHeight));
            message = item.Message.Substring(item.Message.IndexOf("->"), 2);
            index = item.Message.IndexOf("->") + 2;
            //Pintando a seta

            e.Graphics.DrawString(message, txtAcao.Font, new SolidBrush(Color.Black), new PointF(index, e.Index * txtAcao.ItemHeight));


           message = item.Message.Substring(index);
           index = item.Message.IndexOf(message);
            //Pintando o novo valor
           e.Graphics.DrawString(item.Message.Substring(index), txtAcao.Font, new SolidBrush(Color.Blue), new PointF(index, e.Index * txtAcao.ItemHeight));
        }
        else
        {
            e.Graphics.DrawString(txtAcao.Items[e.Index].ToString(), txtAcao.Font, new SolidBrush(txtAcao.ForeColor), new PointF(0, e.Index * txtAcao.ItemHeight));
            // The item isn't a MyListBoxItem, do something about it
        }

しかし、文字列は重なります。

インデックス値は常に正しいです。誰でも私を助けることができますか?

4

1 に答える 1

0

の呼び出しではDrawStringindex変数 (文字列内の文字数) を使用しますが、代わりに測定されたテキストの幅を使用する必要があります。試す:

new PointF(
    e.Graphics.MeasureString(message, txtAcao.Font).Width, 
    e.Index * txtAcao.ItemHeight)

それ以外の

new PointF(index, e.Index * txtAcao.ItemHeight)
于 2013-06-07T15:41:03.930 に答える