c# 带自动滚动的面板 - 控件焦点上的滚动条位置重置 [英] c# Panel with autoscroll - Srollbar position reset on a control focus

查看:69
本文介绍了c# 带自动滚动的面板 - 控件焦点上的滚动条位置重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个windows窗体.

This is for a windows form.

面板具有 AutoScroll = True

Panel has AutoScroll = True

我正在向主面板动态添加面板,但最终超出了主面板显示矩形.然后将标签、组合框和文本框添加到添加的面板中.

I am adding panels dynamically to the main panel which end up exceeding the main panel display rectangle. Then adding Labels, Combo Boxes, and Text Boxes to the added panels.

如果我选择一个组合框或文本框,它会将主面板滚动条位置重置为 0,并且组合框的下拉菜单将放置在屏幕 X、Y 上,如果滚动条没有重置.

If I select a Combo Box or Text Box it resets the main panels scroll bar position to 0 and also the drop down menu for a Combo Box will be placed on the screen X,Y where it should be if the scrollbar had not reset.

我正在考虑在选择控件时保存滚动位置.经过测试,似乎滚动位置尚未重置,因此我可以在此处捕获它.然后我希望在面板的某些事件上恢复滚动位置.我正在尝试准确找出我将使用什么事件来恢复滚动位置.我还希望在执行此操作时下拉菜单将放置在正确的 x,y 处.

I am thinking of saving the scroll position when a control is selected. After testing it seems the scroll position is not reset yet so I am able to capture it here. Then restoring the scroll position on some event of the panel I hope. I am trying to find out exactly what event I would use to restore the scroll position. I also hope that the drop down menu will be placed at the correct x,y when I do this.

更好的解决方案是基于面板控件创建自定义控件并可能覆盖事件?这样我就不需要在每次使用滚动面板时保存滚动位置来弄乱我的项目.

A better solution would be to create a custom control based off the panel control and override an event possibly? This way I would not need to clutter up my project with saving the scroll position every time I use a scrolling panel.

推荐答案

我在这里找到了问题的答案:回答

I found the answer to the problem I was having here: Answer

public class CustomPanel : System.Windows.Forms.Panel
{
    protected override System.Drawing.Point ScrollToControl(System.Windows.Forms.Control activeControl)
    {
        // Returning the current location prevents the panel from
        // scrolling to the active control when the panel loses and regains focus
        return this.DisplayRectangle.Location;
    }
}

这篇关于c# 带自动滚动的面板 - 控件焦点上的滚动条位置重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆