Problem Locating Controls Within a Panel With A Vertical Scroll Bar

E

eBob.com

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
 
C

Cor Ligthert[MVP]

Hi Bob,

I see the same strange behaviour. I have changed your code a little bit,
with things not influencing the behaviour (I did not expect that because I
assume it is something of the adding to the scrolled panel). However, using
for instance Redim in Net is not done anymore (A redim creates every time a
new array).

As nobody else can give an "exact" explanation of this behaviour (not a
workaround) then it would be nice as you send this to connect with your
comments, then you can exactly give all parameters from your situation (OS,
Framework, version of VB etc).

http://connect.microsoft.com/

In fact you can use simple a count, but I let it like this to show you the
generic list instead of an array

My goal was however also to show you how OOP is working. I made it like this
because a lot of people have in the beginning problems with what referencing
means (at least me). You are working with references, the actual values are
always in an object and therefore it is not important where you add an
object to another, it is simply the reference that is add. The labels are
not removed, because the controls from the form are pointing to them,
therefore the labels exist as long as the form exist (in this case there are
even two references, because there is also one from the list).

I hope this gives some ideas

Cor

\\\
Public Class Form1
Dim LabelList As New List(Of Label)
Dim Panel1 As New Panel
Private WithEvents 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)
nudAdd.Width = 40
nudAdd.Minimum = 1
nudAdd.Location = New Point(120, 140)
Me.Controls.Add(nudAdd)
Dim thisLabel As New Label
LabelList.Add(thisLabel)
thisLabel.Text = "label " & 0.ToString
thisLabel.BorderStyle = BorderStyle.FixedSingle
Panel1.Controls.Add(thisLabel)
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles
btnAdd.Click
For i As Integer = 1 To CInt(nudAdd.Value)
Dim thisLabel As New Label
LabelList.Add(thisLabel)
thisLabel.Text = "label " & (LabelList.Count - 1).ToString
thisLabel.BorderStyle = BorderStyle.FixedSingle
thisLabel.Location = New Point(0, (LabelList.Count - 1) * 30)
Panel1.Controls.Add(thisLabel) 'This is where it probably goes
wrong
Next
End Sub
End Class
///
 
E

eBob.com

Thanks Cor. I would certainly like to report the bug to Microsoft via
Connect, but it seems that I'd need OneCare to do that and OneCare costs
money. Just on principal I am not willing to pay Microsoft money to do them
the favor of reporting a bug. And besides, these days I am being very
careful about every $50 I spend.

Thanks, Bob
 
C

Cor Ligthert[MVP]

Bob

I do it tomorrow, the problem is, I cannot reply about the result because
that takes sometimes a little bit long.

:)

Cor
 
E

eBob.com

OK, thanks Cor.

Bob

Cor Ligthert said:
Bob

I do it tomorrow, the problem is, I cannot reply about the result because
that takes sometimes a little bit long.

:)

Cor
 
E

eBob.com

Sorry, I was on the wrong page in Connect when I saw the requirement for a
OneCare log report. No need for OneCare to report a bug in Visual Studio,
..Net, and related products.

Bob
 
E

eBob.com

Hi Cor,

As noted in another post I was wrong about needing OneCare. I have now
submitted the bug report, ID 434531.

Thanks again for your help.

Bob
 
C

Cor Ligthert[MVP]

Hi Bob,

That is better because now you can watch it yourself.

Thanks for telling this to me.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top