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

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

问题描述

问题描述:




  • 创建一个自定义控制。设置它的属性自动滚屏为真。改变它的BG-颜色为绿色。

  • 创建第二个自定义控件。改变它的BG-颜色为红色。

  • 在主窗体的地方第一个自定义的控制

  • 在代码中创建第二个控制的20个实例

  • 添加一个按钮,在按钮:

    • 在代码中设置类似c.Location =新的点(0,y)的环自己的位置;

    • Y + = c.Height;


  • 运行应用程序

  • 按下按钮

  • 滚动容器

  • 再次按下按钮,可以有人请解释一下我的为什么0是不beggining容器形式?这些控制转向...



在你回答:



1)有事情需要是这样



2)以下代码示例:

 公共部分Form1类:表格
{
名单,LT; UserControl2>清单;

公共Form1中()
{
的InitializeComponent();
名单=新名单,LT; UserControl2>();
的for(int i = 0; I< 20;我++)
{
UserControl2 C =新UserControl2();
list.Add(三);
}
}

私人无效Form1_Load的(对象发件人,EventArgs五)
{
的foreach(UserControl2℃的列表)
userControl11。 Controls.Add被(C);
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
INT Y = 0;
的foreach(在列表UserControl2 C)
{
c.Location =新的点(0,Y);
Y + = c.Height;
}
}
}


解决方案地点给出的坐标>

其控制相对于其容器的左上角的的左上角。所以,当你向下滚动,位置将会改变。



下面是如何解决这个问题:

 私人无效的button1_Click(对象发件人,EventArgs五)
{
INT Y =列表[0] .Location.Y;
的foreach(在列表UserControl2 C)
{
c.Location =新的点(0,Y);
Y + = c.Height;
}
}


Description of the problem:

  • Create a 'custom control'. Set it's property AutoScroll to 'true'. Change it's bg-color to green.
  • Create second 'custom control'. Change it's bg-color to red.
  • On main form place first custom control
  • In code create 20 instances of second control
  • Add a button and in the button:
    • In code set their position in loop like c.Location = new Point(0, y);
    • y += c.Height;
  • Run App.
  • Press the button
  • Scroll the container
  • Press the button again and can someone please explain me WHY the 0 is not the beggining of the container form?! The controls are shifted...

Before you answer:

1) Yes the things need to be this way

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;
      }
   }
}

解决方案

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.

Here is how to fix it:

  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;
     }
  }

这篇关于设置控件的位置似乎并不时滚动的'移动'的工作(C#的WinForms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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