当滚动被'moved'移动时,控件的设置位置似乎不起作用(c#, winforms) [英] setting position of a control doesn't seem to work when scroll is 'moved' (c#, winforms)

查看:27
本文介绍了当滚动被'moved'移动时,控件的设置位置似乎不起作用(c#, winforms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题描述:

  • 创建一个自定义控件".将其属性 AutoScroll 设置为true".将其背景色更改为绿色.
  • 创建第二个自定义控件".将其背景颜色更改为红色.
  • 在主窗体上放置第一个自定义控件
  • 在代码中创建第二个控件的 20 个实例
  • 添加一个按钮并在按钮中:
    • 在代码中设置它们在循环中的位置,如 c.Location = new Point(0, y);
    • y += c.Height;

    在你回答之前:

    1) 是的,事情需要这样

    1) Yes the things need to be this way

    2) 下面的代码示例:

    2) Code sample below:

    public partial class Form1 : Form
    {
       List<UserControl2> list;
    
       public Form1()
       {
          InitializeComponent();
          list = new List<UserControl2>();
          for (int i = 0; i < 20; i++)
          {
             UserControl2 c = new UserControl2();
             list.Add(c);
          }
       }
    
       private void Form1_Load(object sender, EventArgs e)
       {
          foreach (UserControl2 c in list)
             userControl11.Controls.Add(c);
       }
    
       private void button1_Click(object sender, EventArgs e)
       {
          int y = 0;
          foreach (UserControl2 c in list)
          { 
             c.Location = new Point(0, y);
             y += c.Height;
          }
       }
    }
    

    推荐答案

    这是因为 Location 给出控件左上角相对于其容器左上角的坐标.所以当你向下滚动时,位置会改变.

    Its because Location gives the coordinates of the upper left corner of the control relative to the upper left corner of its container. So when you scroll down, the Location will change.

    修复方法如下:

      private void button1_Click(object sender, EventArgs e)
      {
         int y = list[0].Location.Y;
         foreach (UserControl2 c in list)
         {
            c.Location = new Point(0, y);
            y += c.Height;
         }
      }
    

    这篇关于当滚动被&amp;#39;moved&amp;#39;移动时,控件的设置位置似乎不起作用(c#, winforms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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