C#Listview(with viewmode = Details)没有水平滚动条

时间:2018-03-18 07:15:46

标签: c# winforms

标题为ListView View = Details有很多列,所以我希望用户能够滚动列。但是,虽然我将Scrollable属性设置为true,但不会显示水平滚动条。我该怎么办?

我搜索了SO,但之前没有人经历过这个。

更新:我刚刚刷新了设计器并且设计器中出现了滚动条,但是当我执行/调试时,没有滚动条出现。

1 个答案:

答案 0 :(得分:-1)

我有另一种解决方法,这是我的解决方案:

step1.create一个派生类型:

public class ListViewEx : ListView
{
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern int ShowScrollBar(IntPtr hWnd, int iBar, int bShow);

    const int SB_HORZ = 0;
    const int SB_VERT = 1;
    protected override void WndProc(ref Message m)
    {
        if (this.View == View.List)
        {
            ShowScrollBar(this.Handle, SB_VERT, 1);
            ShowScrollBar(this.Handle, SB_HORZ, 0);
        }
        base.WndProc(ref m);
    }
}

step2.use派生listview的类型:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ListViewItem lvi1 = new ListViewItem("1");
        lvi1.SubItems.Add(Guid.NewGuid().ToString());
        ListViewItem lvi2 = new ListViewItem("2");
        lvi2.SubItems.Add(Guid.NewGuid().ToString());

        ListViewEx lve = new ListViewEx();

        ColumnHeader headerID = lve.Columns.Add("ID");
        ColumnHeader headerGUID = lve.Columns.Add("GUID");

        lve.View = View.Details;
        Controls.Add(lve);
        lve.Size = new Size(100, 100);
        lve.Items.AddRange(new ListViewItem[] { lvi1, lvi2 });
    }
}

step3.结果是:

ListView with horizontal scrollbar

希望这有帮助!