简体   繁体   English

创建一个带标题的DataGridView(嵌入到GroupBox中)

[英]Create a DataGridView with a Caption (Embed in GroupBox)

I have a DataGridView and I want to put it into a GroupBox. 我有一个DataGridView,我想将其放入GroupBox中。 In VB6 it looked like this: 在VB6中,它看起来像这样:

在VB6中带有字幕的MsFlexGrid

So it is just a MsFlexGrid wrapped by a GroupBox. 因此,它只是由GroupBox包裹的MsFlexGrid。 I've absolutely no idea, how to implement that in VB.NET. 我完全不知道如何在VB.NET中实现它。

I'd let it inherit from DataGridView, so it is a Control, and it has every Property of the DataGridView by default. 我让它继承自DataGridView,因此它是一个控件,并且默认情况下具有DataGridView的每个属性。

Public Class CaptionedDataGridView
    Inherits DataGridView

There would also have to be a GroupBox: 还必须有一个GroupBox:

Private xGroupBox as GroupBox

The text property would be overrided by the text of the group box as well as some size and placement properties (Top, Left, Width, Height) 文本属性将被组框的文本以及某些大小和放置属性(顶部,左侧,宽度,高度)覆盖。

Public Overrides Property Text As String
  Get
    Return xGroupBox.Text
  End Get
  Set(ByVal value As Integer)
    xGroupBox.Text = value
  End Set
End Property

Finally, if I'd create a new CaptionedDataGridView somewhere it should draw itself with the GroupBox sorrounded. 最后,如果我要在某个地方创建一个新的CaptionedDataGridView,则应该使用已环绕的GroupBox进行绘制。 How do I get from where I am right now to where I want to be? 我如何从现在的位置到想要的位置?

I think you have to do that the other way around. 我认为您必须反过来做。 Inherit from the GroupBox and add the DataGridView to it. 从GroupBox继承并向其中添加DataGridView。

Simple example: 简单的例子:

Public Class MyGrid
  Inherits GroupBox

  Private _Grid As DataGridView

  Public Sub New()
    _Grid = New DataGridView()
    _Grid.Dock = DockStyle.Fill
    Me.Controls.Add(_Grid)
  End Sub

  ReadOnly Property Grid As DataGridView
    Get
      Return _Grid
    End Get
  End Property

End Class

Of course, you don't have to do this as a custom control. 当然,您不必将此作为自定义控件。 You can just put a GroupBox on your form and add the DataGridView to it with that same DockStyle.Fill property. 您可以将GroupBox放在窗体上,并使用相同的DockStyle.Fill属性将DataGridView添加到窗体上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM