我如何对齐文本在使用C#一个ListView一个子项? [英] How do I align text for a single subitem in a ListView using C#?

查看:332
本文介绍了我如何对齐文本在使用C#一个ListView一个子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是不是可以在任何地方找到答案这个看似简单的题目?是可以对齐一个WinForms ListView控件一个子项的文字

I wasn't able to find an answer anywhere about this seemingly simple topic: is it possible to align text of a single subitem in a WinForms ListView control?

如果是这样,怎么样?

我想在不同的排列在同一列的文本。

I would like to have text in the same column aligned differently.

推荐答案

有关备查,这里是我如何解决它:

For future reference, here's how I solved it:

// Make owner-drawn to be able to give different alignments to single subitems
lvResult.OwnerDraw = true;
...

// Handle DrawSubItem event
private void lvResult_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    // This is the default text alignment
    TextFormatFlags flags = TextFormatFlags.Left;

    // Align text on the right for the subitems after row 11 in the 
    // first column
    if (e.ColumnIndex == 0 && e.Item.Index > 11)
    {
        flags = TextFormatFlags.Right;
    }

    e.DrawText(flags);
}

// Handle DrawColumnHeader event
private void lvResult_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    // Draw the column header normally
    e.DrawDefault = true;
    e.DrawBackground();
    e.DrawText();
}

有必要处理DrawColumnHeader,否则没有文字或列分隔符会被绘制。

It was necessary to handle the DrawColumnHeader, otherwise no text or column separators would be drawn.

这篇关于我如何对齐文本在使用C#一个ListView一个子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆