28 May 2006

Resizing Label Control for VB.NET

Filed under: Programming — Laura @ 11:02 pm

…And now for something completely different, as they say.

I’ve tried and failed to do something similar to this before, but finally with the help of jo0ls on XVBT, the problem has been tracked down to a rounding error. The idea is to have a control (a label in this case, although the same method should work for a textbox or other type of control as well) that automatically adjusts its height to fit its contents. A simple concept, one would think. And at first glance, one might also think that the AutoSize property of a label would already do that, but no, a look at MSDN explains that AutoSize is of no use once you have text that wraps.

So here’s my solution; feel free to use it as you see fit (and leave a comment if you find it useful, problematic, etc.).

Public Class SmartSizeLabel
  Inherits System.Windows.Forms.Label
#Region " Local declarations "
  Private _noReentry As Boolean = False
#End Region

#Region " Public properties "
  Public Overrides Property AutoSize() As Boolean
    'Force AutoSize to false; there's not much point in this control if we aren't word-wrapping
    Get
      Return False
    End Get
    Set(ByVal value As Boolean)
      MyBase.AutoSize = False
    End Set
  End Property
#End Region

#Region " Overridden methods "
  Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
    MyBase.OnResize(e)

    If Not _noReentry Then _SetBestHeight()
  End Sub

  Protected Overrides Sub OnStyleChanged(ByVal e As System.EventArgs)
    MyBase.OnStyleChanged(e)

    _SetBestHeight()
  End Sub

  Protected Overrides Sub OnMarginChanged(ByVal e As System.EventArgs)
    MyBase.OnMarginChanged(e)

    _SetBestHeight()
  End Sub

  Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
    MyBase.OnTextChanged(e)

    _SetBestHeight()
  End Sub

  Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    'No need to take the bottom margin into account here; it's already been done
    '  in _SetBestHeight(), when adjusting the size of the control itself (and
    '  thus its ClientRectangle).
    Dim rect As New RectangleF(Me.Margin.Left, Me.Margin.Top, _
        Me.ClientRectangle.Width - Me.Margin.Left - Me.Margin.Right, Me.ClientRectangle.Height)

    e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), rect, _
        StringFormat.GenericDefault)
  End Sub
#End Region

#Region " Local methods "
  Private Sub _SetBestHeight()
    Dim gfx As Graphics = Me.CreateGraphics()

    _SetBestHeight(gfx)
    gfx.Dispose()
  End Sub

  Private Sub _SetBestHeight(ByVal gfx As Graphics)
    'This takes into account any space taken up by the control border
    Dim diff As Integer = Me.Height - Me.ClientSize.Height

    'This will get called from OnResize, and also will generate a resize event, so
    '  use a flag to avoid unnecessary recursion
    _noReentry = True

    'Adjust height for border and margins
    Me.Height = _BestHeight(gfx) + diff + Me.Margin.Top + Me.Margin.Bottom
    _noReentry = False
  End Sub

  Private Function _BestHeight(ByVal gfx As Graphics) As Integer
    'Get size of the part of the ClientRectangle that the text will go in; use a really
    '  big height so that all the string will fit (hopefully -- if the height's larger
    '  than MaxValue, we've got other issues =P) in order to measure it accurately.
    Dim srcSize As New SizeF(Me.ClientRectangle.Width - Me.Margin.Left - Me.Margin.Right, _
        Single.MaxValue)
    Dim bestSize As SizeF = gfx.MeasureString(Me.Text, Me.Font, srcSize, _
       Drawing.StringFormat.GenericDefault)

    Return Convert.ToInt32(Math.Ceiling(bestSize.Height))
  End Function
#End Region
End Class

26 May 2006

Silence

Filed under: > Miscellaneous, Poetry — Laura @ 10:27 pm

Silence comes between us
Like a gauze curtain
Blurring what we see
My silence my self defense
Your silence my greatest fear
Silence warm and damp and comfortably numb
Like a womb
Like a shroud
I find myself wrapped in layers of it
No longer sure if you are even there
No longer able to reach out to confirm
I fade away

5 May 2006

Extreme Programming

Filed under: Programming — Laura @ 10:38 pm

A friend in the game development industry recently pointed out this article to me. It’s an interesting walkthrough of a typical day at software engineer Noel Llopis’ job. Reading the description of the work flow, however, I found it anything but typical. Perhaps I’ve been under a rock for awhile, but the concept of pair programming was quite new to me, and a rather fascinating idea. Is this approach a practical one for other industries? For smaller teams? It’s definitely one that deserves more thought and research.

Edit: Thanks to Adam, here is a related study: Crosstalk article

4 May 2006

Introversion: Evening’s End

Filed under: > Miscellaneous, Poetry — Laura @ 10:45 pm

Peace comes in unexpected guises

Feeling my way up the sidewalk
By the light of the half moon
Standing for a moment outside the dark house
Within which no welcome waits

Still there is a rightness
Sated with food and drink and pleasant company
And now alone here in the warm night breeze
A slow smile greets the solitude

Peace beckons.

Top of Page ©2006 Laura Barkman. All rights reserved.