Reputation: 3353
I am using a System.Windows.Forms.ListView
with checkboxes = true
.
I can see that when the list items are more than what can fit, I get a horizontal scroll bar. I tried to find any properties to change scroll bar orientation.
Is there any way to make it scrollable in vertical direction?
Upvotes: 41
Views: 93303
Reputation: 39
This suppresses the horizontal scrollbar well enough.
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
public class ThumbCol : ListView
{
private ImageList il;
private Size pThumbSize=new Size (80,80);
private int vsbw=SystemInformation.VerticalScrollBarWidth;
private int basewidth; //with scrollbar visible but not covering right side of thumbnail
private int barlesswidth;
public int ThumbLeftMarginFudge { get; set; } // thumb is drawn at an (undocd?) offset from left, and may clip
// the image on the right in some scenarios. Use this to compensate
private bool initialized;
public ThumbCol()
{
this.ClientSizeChanged += ThumbCol_ClientSizeChanged;
}
[DllImport("user32")]
private static extern bool ShowScrollBar(IntPtr hwnd, int wBar, int bShow);
private const int SB_HORZ = 0;
public void Init()
{
gndx = 0;
Scrollable = true;
Sorting = SortOrder.None;
View = View.Details;
il = new ImageList();
il.ImageSize = ThumbSize;
il.ColorDepth = ColorDepth.Depth16Bit;
SmallImageList = il;
barlesswidth=pThumbSize.Width + Lookup<int>((int)BorderStyle + 1, 0, 2 * SystemInformation.BorderSize.Width, 2 * SystemInformation.Border3DSize.Width) + Margin.Left + Margin.Right + ThumbLeftMarginFudge;
basewidth = barlesswidth + vsbw;
Width = basewidth;
Columns.Clear();
var ch = new ColumnHeader();
ch.Width = Width;
Columns.Add(ch);
HeaderStyle = ColumnHeaderStyle.None;
initialized = true;
}
private bool blocking = false;
public Size ThumbSize
{
get
{
return pThumbSize;
}
set
{
if (!blocking)
{
blocking = true;
pThumbSize = value;
Width = value.Width;
}
else
{
blocking = false;
}
}
}
public void SetThumbSize(byte width, byte height)
{
blocking = false;
ThumbSize = new Size(width, height);
Init();
}
private int gndx = 0;
public void AddThumb(string imgpath)
{
if (!initialized)
throw new Exception("not initialized");
if (imgpath.ToLower().Contains(".jpg") | imgpath.ToLower().Contains(".png") | imgpath.ToLower().Contains(".webp"))
{
Image thm;
if (imgpath.ToLower().Contains(".webp"))
{/*DIY*/ }
else
{
try
{
thm = Image.FromFile(imgpath).GetThumbnailImage(ThumbSize.Width, ThumbSize.Height, thmback, IntPtr.Zero);
il.Images.Add(imgpath, thm);
gndx += 1;
Items.Insert(0, gndx.ToString("00000"), imgpath).Tag = imgpath;
}
catch (OutOfMemoryException ex)
{
//todo
}
}
}
ShowScrollBar(Handle, SB_HORZ, 0);//false
}
private bool thmback()
{
return false;
}
private bool blockingb;
private void ThumbCol_ClientSizeChanged(object sender, EventArgs e)
{
if (DesignMode == true) return;
if (blockingb)
{
blockingb = false;
}
else
{
blockingb = true;
if (basewidth - Width < vsbw / 2) // full width
{
if (basewidth - ClientSize.Width > vsbw / 2) // bar visible
{/* do nothing*/}
else // bar hidden, shrink a bar width from right
{ Width = basewidth - vsbw;}
}
else if (barlesswidth - ClientSize.Width > vsbw / 2) // bar returns, retore full width
{ Width = basewidth;}
else
{/* do nothing*/}
}
ShowScrollBar(Handle, SB_HORZ, 0);
}
public new void Clear()
{
gndx = 0;
base.Items.Clear();
}
public static T Lookup<T>(int index, params T[] args)
{
if (index < 1 || index > args.Length)
{return default(T);}
else
{return args[--index];}
}
}//Thumbcol
Upvotes: 0
Reputation: 231
I think the only way to force the ListView scroll vertically and view items as "Title" mode, is this :
ListView.View = View.Details;
ListView.HeaderStyle = ColumnHeaderStyle.None;
and add JUST ONE Column
Upvotes: 23
Reputation: 895
You'll need
listView1.View = System.Windows.Forms.View.SmallIcon;
Then your control will have vertical scrollbar and behaviour pretty much the same like View.List
Upvotes: -1
Reputation: 5569
You need to Set
Listview1.Scrollable = true;
Listview1.View = View.Details
This will only work correctly if you have added some columns in your Listview1, So add a dummy column. like,
ColumnHeader header = new ColumnHeader();
header.Text = "";
header.Name = "col1";
listView1.Columns.Add(header);
Upvotes: 64
Reputation: 42175
You can't change the scroll bar orientation, per sé.
You get a vertical scrollbar if you have items that go off the bottom of the listview, and a horizontal scrollbar if you have items that go off the right-hand side of the listview.
So if you want to control the scrollbars, you actually do this by controlling the content. Personally I only ever use ListViews in Detail mode, but to take that as an example, you would make sure that your column headers are sized such that they all fit in the horizontal space.
Upvotes: 0
Reputation: 15016
The ListView should also display a vertical scrollbar automatically if you've got enough items in the collection (i.e. more than can be displayed on the ListView currently).
Upvotes: 0