drag up and drag down item in many item in listbox in winforms c#

378 Views Asked by At

i have created listbox. i inserted many items so listbox have scroll bar and also i put drag over event for drag up and drag down item. now my problem is that if i have multiple item and listbox display in scroll view than how to scroll up and scroll down my item in large set of item in listbox. please provide me solution. thanks in advance...

2

There are 2 best solutions below

0
Hany Habib On

To Do that you need to override OnDrawItem ex : https://www.codeproject.com/Articles/2091/ListBox-with-Icons

// GListBoxItem class 
public class GListBoxItem
{
    private string _myText;
    private int _myImageIndex;
    // properties 
    public string Text
    {
        get {return _myText;}
        set {_myText = value;}
    }
    public int ImageIndex
    {
        get {return _myImageIndex;}
        set {_myImageIndex = value;}
    }
    //constructor
    public GListBoxItem(string text, int index)
    {
        _myText = text;
        _myImageIndex = index;
    }
    public GListBoxItem(string text): this(text,-1){}
    public GListBoxItem(): this(""){}
    public override string ToString()
    {
        return _myText;
    }
}//End of GListBoxItem class

// GListBox class 
public class GListBox : ListBox
{
    private ImageList _myImageList;
    public ImageList ImageList
    {
        get {return _myImageList;}
        set {_myImageList = value;}
    }
    public GListBox()
    {
        // Set owner draw mode
        this.DrawMode = DrawMode.OwnerDrawFixed;
    }
    protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
    {
        e.DrawBackground();
        e.DrawFocusRectangle();
        GListBoxItem item;
        Rectangle bounds = e.Bounds;
        Size imageSize = _myImageList.ImageSize;
        try
        {
            item = (GListBoxItem) Items[e.Index];
            if (item.ImageIndex != -1)
            {
                imageList.Draw(e.Graphics, bounds.Left,bounds.Top,item.ImageIndex); 
                e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), 
                    bounds.Left+imageSize.Width, bounds.Top);
            }
            else
            {
                e.Graphics.DrawString(item.Text, e.Font,new SolidBrush(e.ForeColor),
                    bounds.Left, bounds.Top);
            }
        }
        catch
        {
            if (e.Index != -1)
            {
                e.Graphics.DrawString(Items[e.Index].ToString(),e.Font, 
                    new SolidBrush(e.ForeColor) ,bounds.Left, bounds.Top);
            }
            else
            {
                e.Graphics.DrawString(Text,e.Font,new SolidBrush(e.ForeColor),
                    bounds.Left, bounds.Top);
            }
        }
        base.OnDrawItem(e);
    }
}//End of GListBox class

Or you can use different controls as DataGridView or ListView
ex:How do add image to System.Windows.Forms.ListBox?

0
Jonathan Applebaum On

I think that FlowLayoutPanel (or TableLayoutPanel ) can be suitable for that need, here is a basic example, comments inside the code:

private void Form1_Load(object sender, EventArgs e)
{
    // declare flowlayout panel
    FlowLayoutPanel fl = new FlowLayoutPanel();
    fl.Size = new Size(500, 800);
    // this will add a scroll bar when the children height are greater than the height
    fl.AutoScroll = true;
    this.Controls.Add(fl);
    // add pictureboxes that shows the bitmaps
    for (int i = 0; i < 20; i++)
    {
        Bitmap b = new Bitmap(@"C:\Users\xxx\xxx\xxx.png");
        PictureBox p = new PictureBox();
        p.Image = b;
        p.Size = new Size(fl.Width, 50);
        fl.Controls.Add(p);
    }
}