Winforms SplitterPanel, z-index of child overlap split

谁说胖子不能爱 提交于 2019-12-13 08:26:26

问题


I am working with a SplitterPanel in winforms where on the right hand side I want a custom dropdown list control which displays a 2 columns dropdown list.

The problem is that with there being two columns I want to be able to have a larger dropdown list area than the actual dropdown, and therefore overlap the SplitterPanel if the list doesn't fit in the split area.

I have tried using .BringToFront();, however this does not work on the SplitterPanel and the control is hidden. I come from a web background where I would have used z-index for this but I am stumped with winforms. See below image of my issue.

Does anyone know how I can resolve this?


回答1:


The z-index will determine which child controls sit higher and can overlap which others child controls. But it never helps when you want a (real as opposed to drop downs or menues) child overlap its own container. This never happens; and since the CheckedListBox is the child of the split panel it will never overlap it.

You will need to make the CheckedListBox sit not inside the splitter panel but in its Parent so the they are 'brethren'. Let's assume the SplitContainer sits in a TabPage tabPage8. Then you can show it fully by moving it to that tabPage:

moveCtlToTarget(tabPage8, checkedListBox1);

using this function.

void moveCtlToTarget(Control target, Control ctl)
{
    // get screen location of the control
    Point pt = ctl.PointToScreen(Point.Empty);
    // move to the same location relative to the target
    ctl.Location = target.PointToClient(pt);
    // add to the new controls collection
    ctl.Parent = target;
    // move all up
    ctl.BringToFront();
}

As I don't know how you create and show it, resetting it is up to you. Note that as it now is no longer in the split panel it will not move when you move the splitter..

You may want to do this only the first time and later align it with the ComboBox..




回答2:


TaW's answer above helped my solve my issue. I modified it slightly for my situation where I moved the parameters into the method as I already had my checkbox control set as a property of the control and got the target by looping up the parents until I got to the top.

    private void moveCtlToTarget()
    {
        Control Target = Parent;
        while (Target.Parent != null)
            Target = Target.Parent;

        Point pt = CheckBox.PointToScreen(Point.Empty);
        CheckBox.Location = Target.PointToClient(pt);
        CheckBox.Parent = Target;
        CheckBox.BringToFront();
    }


来源:https://stackoverflow.com/questions/39637490/winforms-splitterpanel-z-index-of-child-overlap-split

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!