I have a Windows Forms Application with an MDIParent Form and Windows Form. The Form has a handful of controls and on Form_Load I add a User Control.
Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim Sample1 As New ucBODSample
With Sample1
.Name = "Sample_1"
.Text = "Sample 1"
.Location = New Point(10, 497)
End With
m_intSampleCount = 1
Me.Controls.Add(Sample1)
End Sub
On Button click, I want to load additional instances of this user control:
Private Sub cmdAddSample_Click(sender As Object, e As EventArgs) Handles cmdAddSample.Click
m_intSampleCount = m_intSampleCount 1
Dim y_uc As Integer
Dim y_b As Integer
Dim y_Me As Integer
If Me.Height <> 910 Then
y_Me = 910
Me.Height = y_Me
Me.Width = Me.Width 15
Me.HorizontalScroll.Maximum = 0
Me.AutoScroll = True
End If
Dim SampleX As New ucBODSample
With SampleX
.Name = "Sample_" & m_intSampleCount
.Text = "Sample " & m_intSampleCount
.grbSample.Text = "Sample " & m_intSampleCount
y_uc = 317 (m_intSampleCount * 175)
.Location = New Point(10, y_uc)
End With
y_b = 495 (m_intSampleCount * 175)
Me.Controls.Add(SampleX)
Me.cmdAddSample.Location = New Point(10, y_b)
End Sub
The Native Height of the Form is 754. On the first button click, the height of the form is set to 910 and the width of the form is increase by 15 and the "Sample 2" UserControl adds as expected.
On the second button click, the scroll bar appears and the "Sample 3" UserControl adds as expected and we auto scroll to the bottom of the form.
On the third button click, the "Sample 4" UserControl adds, but there is a large gap between it and the previous user control.
On subsequent button clicks, the "Sample X" UserControl adds, but the gap between user controls keeps getting wider and wider.
When "Sample 3" UserControl is added, the y coordinate is 842 (less than 910, the height of the form). "Sample 4" is the first control added where the y coordinate is greater than the height of the form and it is also the first one mispositioned.
The AddSample Button is always at the bottom of the form, the appropriate distance from the last added user control.
How should I adjust the code to continue to stack instances of this user control tightly, even after I have exceeded the height of the form?
CodePudding user response:
I added the following code to resolve the issue:
If y_uc > 842 Then
y_uc = y_uc - Me.VerticalScroll.Value
End If
If y_b > 1020 Then
y_b = y_b - Me.VerticalScroll.Value
End If
The y position is based on the top of the form. And since the form has numerous controls scrolled above the top of the form, the 0 of y has changed.
UPDATE: I removed the if and just set y_uc = y_uc - Me.VerticalScroll.Value because until it scrolls, the Me.VerticalScroll.Value = 0.
CodePudding user response:
First we need to find the location of the last control that was added, then add the height of the UserControl (ucBODSample).
Dim yLoc As Integer = Me.Controls.OfType(Of ucBODSample).LastOrDefault().Location.Y 317
Next we use that as the Y location for the new UserControl.
Dim SampleX As New ucBODSample
With SampleX
.Name = "Sample_" & m_intSampleCount
.Text = "Sample " & m_intSampleCount
.Title = "Sample " & m_intSampleCount
y_uc = yLoc '317 (m_intSampleCount * 175)
.Location = New Point(10, y_uc)
End With
Lastly we need to adjust the button's location.
y_b = SampleX.Location.Y SampleX.Height cmdAddSample.Height
Put it all together and we get
Private Sub cmdAddSample_Click(sender As Object, e As EventArgs) Handles cmdAddSample.Click
Dim y_b As Integer = 0
If Me.Height <> 910 Then
Me.Height = 910
Me.Width = Me.Width 15
Me.HorizontalScroll.Maximum = 0
Me.AutoScroll = True
End If
'' Find the last control on the Form of type ucBODSample. Get the location of Y. Location Y is
'' only the starting point of the Y axis on the user control. Since we need the location of the
'' bottom of the control we add the height of the user control to the Y location. We're using
'' 317 as a constant value for height. I'd recommend using the height of the control instead.
'' Leaving as is for simplicity.
Dim yLoc As Integer = Me.Controls.OfType(Of ucBODSample).LastOrDefault().Location.Y 317
'' Create an instance of the user control and set its location using the found value for Y axis.
Dim SampleX As New ucBODSample
With SampleX
.Name = "Sample_" & m_intSampleCount
.Text = "Sample " & m_intSampleCount
.Title = "Sample " & m_intSampleCount '' changed grbSample.Text for a property on ucBODSample
.Location = New Point(10, yLoc)
End With
'' Since the button should always be at the bottom we use the location of the user control that
'' was just added and add the height of the user control and the button.
y_b = SampleX.Location.Y SampleX.Height cmdAddSample.Height '495 (m_intSampleCount * 175)
Me.Controls.Add(SampleX)
Me.cmdAddSample.Location = New Point(10, y_b)
End Sub
Property on User Control (ucBODSample)
Public Property Title As String
Get
Return grbSample.Text
End Get
Set(value As String)
grbSample.Text = value
Me.Invalidate()
End Set
End Property