【问题标题】:drag up and drag down item in many item in listbox in winforms c#在winforms c#中的列表框中的许多项目中向上和向下拖动项目
【发布时间】:2018-06-08 13:33:08
【问题描述】:

我已经创建了列表框。我插入了许多项目,所以列表框有滚动条,而且我把拖动事件放在向上和向下拖动项目。现在我的问题是,如果我在滚动视图中显示多个项目和列表框,而不是如何在列表框中的大量项目中向上滚动和向下滚动我的项目。请给我解决方案。 提前谢谢...

【问题讨论】:

  • 目前还没有 Visual Studio 2018 可用。其次,到目前为止,您做了哪些努力?
  • 对不起,我用的是 vs 2017
  • 我相信这是一项艰苦的工作,因为您必须制作自定义列表框并覆盖 OnDrawItem。不介意的话,用ListView更方便
  • ListView 支持通过 ImageList 开箱即用地显示较小的图像 (

标签: c# winforms drag-and-drop listboxitems


【解决方案1】:

要做到这一点,您需要覆盖 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

或者您可以使用不同的控件,例如 DataGridViewListView
例如:How do add image to System.Windows.Forms.ListBox?

【讨论】:

    【解决方案2】:

    我认为FlowLayoutPanel(或TableLayoutPanel)可以满足这种需求, 这是一个基本的例子,代码里面的cmets:

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 2017-10-29
      相关资源
      最近更新 更多