Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Windows窗体应用程序列表框控件已移动_C#_Winforms_Listbox_User Controls - Fatal编程技术网

C#Windows窗体应用程序列表框控件已移动

C#Windows窗体应用程序列表框控件已移动,c#,winforms,listbox,user-controls,C#,Winforms,Listbox,User Controls,我在windows窗体应用程序中遇到所有者绘制的列表框问题。 列表框中填充了包含其自己的UserControl的对象。每个项目的用户控件显示在列表框中。 这一切都可以,但当我向上或向下滚动时,用户控件似乎有点移位。 一旦我点击它们,它们就会跳到正确的位置 在图中,您可以看到白色用户控件向右移动了一点,向下移动了一点 这是它们在滚动之前的样子 列表中填充了以下类型的对象: class Class1 { public UserControl1 UC; public string

我在windows窗体应用程序中遇到所有者绘制的列表框问题。 列表框中填充了包含其自己的UserControl的对象。每个项目的用户控件显示在列表框中。 这一切都可以,但当我向上或向下滚动时,用户控件似乎有点移位。 一旦我点击它们,它们就会跳到正确的位置

在图中,您可以看到白色用户控件向右移动了一点,向下移动了一点

这是它们在滚动之前的样子

列表中填充了以下类型的对象:

class Class1
{
    public UserControl1 UC;
    public string Text;

    public Class1(UserControl1 uc, string text)
    {
        UC = uc;
        Text = text;
    }
}
这是控制列表的类:

class ListDrawer
{
    public ListBox LB;
    public int HeaderHeight = 25;

    public ListDrawer(ListBox lb)
    {
        LB = lb;
        LB.DrawMode = DrawMode.OwnerDrawVariable;
        LB.DrawItem += LB_DrawItem;
        LB.MeasureItem += LB_MeasureItem;
    }

    private void LB_MeasureItem(object sender, MeasureItemEventArgs e)
    {
        ListBox lst = sender as ListBox;
        Class1 c = (Class1)lst.Items[e.Index];
        e.ItemHeight = HeaderHeight;
        e.ItemHeight = e.ItemHeight + c.UC.Height;
    }

    private void LB_DrawItem(object sender, DrawItemEventArgs e)
    {
        ListBox lst = sender as ListBox;
        Class1 c = (Class1)lst.Items[e.Index];
        e.DrawBackground();
        e.Graphics.FillRectangle(Brushes.DarkSeaGreen, e.Bounds);
        e.Graphics.DrawString(c.Text, LB.Font, SystemBrushes.HighlightText, e.Bounds.Left, e.Bounds.Top);
        if (!lst.Controls.Contains(c.UC))
        {
            lst.Controls.Add(c.UC);
        }
        c.UC.Top = e.Bounds.Top + HeaderHeight;
    }
}
单击按钮即可填写列表:

    private void button1_Click(object sender, EventArgs e)
    {
        UserControl1 uc = new UserControl1();
        Class1 c = new Class1(uc, "text 1");
        ListDrawer LD = new ListDrawer(listBox1);
        listBox1.Items.Add(c);
        uc = new UserControl1();
        c = new Class1(uc, "text 2");
        listBox1.Items.Add(c);
    }
希望这可以解决

干杯,
Robert。

在用户控件中覆盖onMove事件:


它可能会闪烁,但会解决您的问题

您是否在绘制对象时尝试了
列表框
上的
Refresh()
和/或
Update()
,用户控件是相对元素,因此从(0,0)开始的位置将相对于其容器创建它们。这些项必须完全显示在列表框中。例如,列表框的高度必须是项目高度的精确倍数。@DzNiT0-使用位置具有相同的效果,用户控件在第一次绘制时处于正确的位置,但在滚动之后,它们似乎向右和向下移动了一个像素,如图中所示。
protected override void OnMove( EventArgs e ) {
    base.OnMove( e );

    this.Parent.Invalidate();
}