I'd like to have an autosize textbox and combobox. While these controls do have a (hidden) AutoSize property, setting this property doesn't seem to have any impact. So I have come up with the following code, which you will appreciate is not perfect. Any suggestions for improvements, especially removal of the figure 6?
Visual Basic:
Public Class Form1
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, ComboBox1.TextChanged
With CType(sender, Control)
Dim lbl As New Label
Me.Controls.Add(lbl)
lbl.Font = .Font
lbl.AutoSize = True
lbl.Text = .Text
Dim W As Integer = lbl.Width
If TypeOf sender Is ComboBox Then W = W + SystemInformation.VerticalScrollBarWidth + 6
.Width = W
lbl.Dispose()
End With
End Sub
End Class