代码之家  ›  专栏  ›  技术社区  ›  g t Omri Btian

如何使用WinForms设置控件的Z顺序

  •  28
  • g t Omri Btian  · 技术社区  · 14 年前

    我在写一个习惯 TextBox 一旦获得焦点就会改变它的边界样式。

    由于添加边框会导致控件与相邻控件重叠,因此我临时将文本框置于对话框的前面(使用 textBox.BringToFront() ).

    这是可能的吗(最好是以一种简单的方式!)

    3 回复  |  直到 14 年前
        1
  •  43
  •   SLaks    14 年前

    打电话给 GetChildIndex SetChildIndex 父对象的方法 Controls 收藏。

        2
  •  27
  •   Iain Ward    9 年前

    在VB中没有Z顺序,但是可以使用 GetChildIndex SetChildIndex 方法来手动获取和设置其索引。

    Here 这里有一个如何使用它的例子。不过,您可能需要保留每个控件索引的记录,以便在完成时将其设置回索引。

    // Get the controls index
    int zIndex = parentControl.Controls.GetChildIndex(textBox);
    // Bring it to the front
    textBox.BringToFront();
    // Do something...
    // Then send it back again
    parentControl.Controls.SetChildIndex(textBox, zIndex);
    
        3
  •  1
  •   gVista    6 年前

    与FlowLayoutPanel一起使用时,这将上下移动控件

        /// <summary>
        /// When used with the FlowLayoutPanel this will move a control up or down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="UpDown"></param>
        private void C_On_Move(object sender, int UpDown)
        {
            //If UpDown = 1 Move UP, If UpDown = 0 Move DOWN
            Control c = (Control)sender;
            // Get the controls index
            int zIndex = _flowLayoutPanel1.Controls.GetChildIndex(c);
            if (UpDown==1 && zIndex > 0)
            {
                // Move up one
                _flowLayoutPanel1.Controls.SetChildIndex(c, zIndex - 1);
            }
            if (UpDown == 0 && zIndex < _flowLayoutPanel1.Controls.Count-1)
            {
                // Move down one
                _flowLayoutPanel1.Controls.SetChildIndex(c, zIndex + 1);
            }
        }
    
    推荐文章