水平列表框

时间:2015-10-13 17:36:58

标签: c# winforms listbox

我正在一个需要水平显示一些项目的项目中工作,项目就像列表框一样,所以我可以在将项目添加到列表框之前更改项目的文本和属性..是吗可以制作像这个数字的水平列表框? enter image description here 图片来源:Horizontal ListView Xamarin.Forms

EDIT;我正在使用Windows窗体应用程序

2 个答案:

答案 0 :(得分:3)

您还可以使用ListView控件:

listView1.View = View.Tile;
listView1.Alignment = ListViewAlignment.Left;
listView1.OwnerDraw = true;
listView1.DrawItem += listView1_DrawItem;
listView1.TileSize = new Size(48,
  listView1.ClientSize.Height - (SystemInformation.HorizontalScrollBarHeight + 4));
for (int i = 0; i < 25; ++i) {
  listView1.Items.Add(new ListViewItem(i.ToString()));
}

void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) {
  Color textColor = Color.Black;
  if ((e.State & ListViewItemStates.Selected) != 0) {
    e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
    textColor = SystemColors.HighlightText;
  } else {
    e.Graphics.FillRectangle(Brushes.White, e.Bounds);
  }
  e.Graphics.DrawRectangle(Pens.DarkGray, e.Bounds);

  TextRenderer.DrawText(e.Graphics, e.Item.Text, listView1.Font, e.Bounds,
                        textColor, Color.Empty,
                        TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
}

结果:

enter image description here

答案 1 :(得分:2)

虽然我不确定这是否符合您的要求,但您可以尝试以下方式:

ListBox lb = new ListBox();
lb.MultiColumn = true; //If this is set to false, you see vertical listbox.
lb.Items.AddRange(new object[] { 1, 2, 3, 4, 5, 6, 7, 8 });
lb.Height = 50;
lb.Width = 200;
this.Controls.Add(lb); //I took this dynamic listbox approach just for demo purpose.