Code Says One Thing, Debugger(Watch) Says Another (Update)

Code Says One Thing, Debugger(Watch) Says Another (Update)

Post by » Sun, 19 Apr 2009 05:31:50

The problem continues to mystify me. The code appended below is a bit
cleaner than what I posted earlier but exhibits the same error, i.e. the
value of Panel.ClientSize.Width does not appear to change after a vertical
scroll bar has been added to the control. The addition of the scroll bar
happens automatically, or should, because I use AutoScroll = True and put
more stuff in the Panel than its visible area can accomodate.

I've learned thru further experimentation that the code works correctly if I
1) put a breakpoint in the code AND 2) have a Watch on "somepanel" with the
properties expanded. It's not good enough to just have a Watch on
"somepanel", I also have to have clicked on the + sign in front of
"somepanel" so that all of the properties are displayed.

Most of my experiments I've done on a Vista x64 system. In the few
experiments I've done on an XP box I get the same result. On the Vista x64
system I've tried three different levels of the Framework and get the same
result with all three levels.

I've also tried turning off optimization and that's made no difference.

The good news is than when, by using the de *** tricks described above, I
see the ClientSize.Width change, it changes by 17, which is indeed the value
of System.Windows.Forms.SystemInformation.VerticalScrollBarWidth. Using
that value is clearly the right and most simple way to insure that my
TextBoxes don't stray into the area of the panel which might eventually be
used by the vertical scroll bar and I never would have written the code I
posted if I had been aware of it. (And thank you again Armin for making me
aware of it!)

So, much as I'd like to get to the bottom of this, I can't think of any
futher experiments and I guess I'll be moving on.


Public Class Form1
Dim somepanel As New Panel
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' determine the width of a panel with a vertical scroll bar

Dim DeterminePanelUsableWidth As Integer
Dim stoped_at_index As Integer

Const panel_width As Integer = 275

somepanel.Height = 100
somepanel.AutoScroll = True
somepanel.Width = panel_width

Dim someboxes() As TextBox
Dim someboxes_index As Integer = 0
For someboxes_index = 0 To 999
stoped_at_index = someboxes_index
ReDim Preserve someboxes(someboxes_index)
someboxes(someboxes_index) = New TextBox
With someboxes(someboxes_index)
.Width = 234
If someboxes_index > 0 Then
.Location = New Point(0, _
someboxes_index * (someboxes(0).Height + 1))
End If
If panel_width > somepanel.ClientSize.Width Then Exit For
End With

DeterminePanelUsableWidth = somepanel.ClientSize.Width

MsgBox("somepanel.ClientSize.Width is " &
somepanel.ClientSize.Width.ToString & vbCrLf & _
"DeterminePanelUsableWidth is " & DeterminePanelUsableWidth.ToString
& vbCrLf & _
"stopped at index " & stoped_at_index.ToString)


End Sub
End Class