Problem Locating Controls Within a Panel With A Vertical Scroll Bar

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I have found that locating, i.e. placing, controls within a Panel which has a vertical scroll bar results in eratic locations. It seems that so long as the slider is at the top there is no problem, but if the slider is at the bottom when the Controls.Add is done the control does not end up where its Location property specifies.

I experience this problem on two systems, one Vista x64 SP1 and the other WinXP SP3, both running VBE 2008 w/.NET 3.5 SP1.

Really simple code which demonstrates this problem is pasted below. It uses the designer only to get the Form1 control. When it initializes there is a Panel with a Label in it. Each time you click on the Add button another Label is added. A NumericUpDown allows you to add more than one Label at a time. So long as the slider is at the top of the panel the location of each Label is what you'd expect. But if the slider is at the bottom of the panel the new Label will not be placed properly.

I sure hope someone will try this. It's hard to believe that this is bug (which doesn't seem to have been discussed here), but it is also difficult to see how my code could result in such behavior.

Thanks for whatever help anyone can offer. Bob

Public Class Form1

Dim LabelArray() As Label
Dim Panel1 As New Panel
Dim btnAdd As New Button
Dim nudAdd As New NumericUpDown

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Panel1.AutoScroll = True
Panel1.Height = 100
Panel1.Width = 150
Panel1.BorderStyle = BorderStyle.FixedSingle
Panel1.Location = New Point(10, 10)
Me.Controls.Add(Panel1)

btnAdd.Text = "Add"
btnAdd.Location = New Point(10, 140)
Me.Controls.Add(btnAdd)
AddHandler btnAdd.Click, AddressOf btnAdd_Click

nudAdd.Width = 40
nudAdd.Minimum = 1
nudAdd.Location = New Point(120, 140)
Me.Controls.Add(nudAdd)

ReDim Preserve LabelArray(0)
LabelArray(0) = New Label
With LabelArray(0)
.Text = "label " & 0.ToString
.BorderStyle = BorderStyle.FixedSingle
End With
Panel1.Controls.Add(LabelArray(0))
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If nudAdd.Value = 0 Then Exit Sub

For i As Integer = 1 To nudAdd.Value
ReDim Preserve LabelArray(LabelArray.Length)
LabelArray(LabelArray.Length - 1) = New Label
With LabelArray(LabelArray.Length - 1)
.Text = "label " & (LabelArray.Length - 1).ToString
.BorderStyle = BorderStyle.FixedSingle
.Location = New Point(0, (LabelArray.Length - 1) * 30)
End With
Panel1.Controls.Add(LabelArray(LabelArray.Length - 1))
Next
End Sub
End Class


.



Relevant Pages

  • Re: how to have a checkbox change color and text of a label control?
    ... So could you make the label control box change color instead of the font? ... states "Private Sub Form_Current is highlighted yellow ... Name the checkbox and the label with similar names. ...
    (microsoft.public.access.forms)
  • Re: Reversing changes made by mousemove event
    ... I have a label control so this is how I ... It doesn't work with the control array of labels. ... Private Sub Label2_DragDrop(Index As Integer, Source As Control, X As ... Label2.Drag vbEndDrag ...
    (microsoft.public.vb.general.discussion)
  • low quality of render drwaing?!
    ... I Have a user control. ... on paint event of this panel I draw two rectangle in this. ... Private Sub TollsPanel_Paint(ByVal sender As System.Object, ... If TollsPanel.Width NewWidth Then TollsPanel.Width = NewWidth ...
    (microsoft.public.vb.general.discussion)
  • low quality of render drwaing?
    ... I Have a user control. ... on paint event of this panel I draw two rectangle in this. ... Private Sub TollsPanel_Paint(ByVal sender As System.Object, ... If TollsPanel.Width NewWidth Then TollsPanel.Width = NewWidth ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Reversing changes made by mousemove event
    ... like you are saying that your modified code doesn't work with the control ... Private Sub Label2_DragDrop(Index As Integer, Source As Control, X As ... Label2.Drag vbEndDrag ... to another label without going over the image control. ...
    (microsoft.public.vb.general.discussion)