使用自动滚动控制 Windows.Forms.Panel 内的位置 [英] Control position inside Windows.Forms.Panel with autoscroll

查看:20
本文介绍了使用自动滚动控制 Windows.Forms.Panel 内的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Control.Location 不考虑父面板控件的滑块位置并返回值,就像没有滚动条一样.那么如何获取可滚动面板中某个控件的真实位置呢?例如在这里我从来没有得到我的按钮的真实位置:

Control.Location doesn't take into account the slider position of the parent panel control and returns the value like if there was no scrollbar. So how to obtain the real position of some control inside scrollable panel? For example here I never get the real location of my button:

Button button;
        public Form1()
        {
            InitializeComponent();
            panel1.Height = 200;
            panel1.AutoScrollMinSize = new Size(0, 2000);
            button = new Button();
            panel1.Controls.Add(button);
            button.Top = 1500;
            button.Click += new EventHandler(button_Click);
        }

        void button_Click(object sender, EventArgs e)
        {
            MessageBox.Show(button.Location.Y.ToString());
        } 

推荐答案

确实如此.如果我将面板放在滚动面板内,位置位置会随着我滚动而改变:

It does. If I put a panel inside a scrolling panel, the location position is changing as I scroll:

Private Sub Panel1_Scroll(ByVal sender As Object, ByVal e As ScrollEventArgs) Handles Panel1.Scroll
  Me.Text = Panel2.Location.ToString
End Sub

确保您的控件没有在另一个滚动的面板中,这样 location 属性就不会改变.

Make sure you don't have your control inside another panel that is getting scrolled, then the location property doesn't change.

否则,请查看 PointToScreen 和 PointToClient 函数或根据滚动父级的 AutoScrollPosition 值调整您的值.

Otherwise, look at the PointToScreen and PointToClient functions or adjust your values according to the AutoScrollPosition value of the scrolling parent.

这篇关于使用自动滚动控制 Windows.Forms.Panel 内的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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