【问题标题】:Center Checkboxes in Table Layout Panel表格布局面板中的中心复选框
【发布时间】:2013-08-06 15:46:26
【问题描述】:

我试图将我的复选框置于TableLayoutPanel 的中心,但由于复选框控件的性质,它们最终看起来总是左对齐。见下图:

我希望每行检查都是左对齐的,但它看起来更居中。类似于以下内容:

我在网上查了一下,我可以通过设置AnchorStyles.None 使复选框居中,这不是我想要的,因为复选框没有对齐。我将它们设置为Dock.Fill,因此您可以单击单元格中的任意位置以激活复选框。

我目前只是填充我的桌子以达到类似的效果,但从长远来看,这不是一个可接受的解决方案。此外,填充单元格将换行复选框文本,而不会占用行上的所有可用空间(因为某些行正在被填充占用)。使用表格左侧的间隔单元也是如此,这不是理想的解决方案。

有人有什么想法吗?谢谢!

【问题讨论】:

    标签: c# .net winforms checkbox tablelayout


    【解决方案1】:

    这可能对你有用:

    1. 将你的TableLayoutPanel中的所有ColumnStyles设置为.SizeType = SizeType.AutoSize

    2. 设置你的TableLayoutPanel.AutoSize = trueTableLayoutPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink

    3. 添加此代码以使您的复选框(以及您的TableLayoutPanel)动态居中:

      //SizeChanged event handler of your tableLayoutPanel1
      private void tableLayoutPanel1_SizeChanged(object sender, EventArgs e){
         //We just care about the horizontal position
         tableLayoutPanel1.Left = (tableLayoutPanel1.Parent.Width - tableLayoutPanel1.Width)/2;
         //you can adjust the vertical position if you need.
      }
      

    更新

    关于你添加的问题,我认为我们必须改变一些事情:

    1. 将您的 CheckBox AutoSize 设置为 false。之前的解决方案要求是true

    2. 添加更多代码(在上面的代码旁边):

       int checkWidth = CheckBoxRenderer.GetGlyphSize(yourCheckBox.CreateGraphics(),System.Windows.Forms.VisualStyles.CheckBoxState.MixedNormal).Width;
       //TextChanged event handler of your CheckBoxes (used for all the checkBoxes)
       private void checkBoxes_TextChanged(object sender, EventArgs e){
         UpdateCheckBoxSize((CheckBox)sender);
       }
       //method to update the size of CheckBox, the size is changed when the CheckBox's Font is bolded and AutoSize = true.
       //However we set AutoSize = false and we have to make the CheckBox wide enough
       //to contain the bold version of its Text.
       private void UpdateCheckBoxSize(CheckBox c){
          c.Width = TextRenderer.MeasureText(c.Text, new Font(c.Font, FontStyle.Bold)).Width + 2 * checkWidth;
       }
       //initially, you have to call UpdateCheckBoxSize
       //this code can be placed in the form constructor
       foreach(CheckBox c in tableLayoutPanel1.Controls.OfType<CheckBox>())
          UpdateCheckBoxSize(c);
      
       //add this to make your CheckBoxes centered even when the form containing tableLayoutPanel1 resizes
       //This can also be placed in the form constructor
       tableLayoutPanel1.Parent.SizeChanged += (s,e) => {
          tableLayoutPanel1.Left = (tableLayoutPanel1.Parent.Width - tableLayoutPanel1.Width)/2;
       };
      

    【讨论】:

    • 这是目前为止效果最好的,但我还要给你一个谜语:假设当用户滚动复选框时复选框变为粗体。这会移动复选框,为其新的粗体尺寸腾出空间。我如何才能将单元格大小强制为未来可能的粗体大小,以便当用户滚动它们时它们不会“摆动”?
    • 好些了,但还是偏左。值得庆幸的是,粗体不再移动复选框。感谢您的所有帮助,虽然它并不完美,但它比我拥有的更好......i.imgur.com/S9SjAcL.png
    • @DTI-Matt 很容易修复它,我发布的代码以tableLayoutPanel1 为中心,它要求复选框(在同一行)具有相同的Text length。如果你更改tableLayoutPanel1_SizeChanged中的代码,就可以实现你想要的。重点是不是tableLayoutPanel1.Width,你必须得到同一行2个复选框的总文本长度的Width,你可以在UpdateCheckBoxSize(...)方法中更新这个Width...希望你得到这个想法。
    • @DTI-Matt,如果您在第 1 列和第 2 列中的 checkBoxestextlength 有很大差异,则它不会居中,新代码必须为 bolded 版本腾出空间文本越长,复选框需要的空间就越大。如果更正布局以便您可以看到居中的复选框(使用普通字体),则当您的鼠标悬停在其上时,文本可能会被剪掉(意味着粗体文本没有足够的空间来显示)。我认为您可以尝试在鼠标悬停时更改颜色而不是 font-weight
    【解决方案2】:

    与将复选框放在单元格中不同,将面板中的每个复选框都放在组框内将允许复选框填充每个面板并在它们周围有一个可点击区域。然后将 groupbox 停靠设置为填充,并将面板的锚点设置为顶部,底部它们都保持居中。

    【讨论】:

      猜你喜欢
      • 2013-09-05
      • 2023-03-30
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多