Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing Labels in .net

I'm using VB 2008 express but this probably applies to all .net environments.

My question has to do with labels that do nothing. No events, and their text never changes. They just sit there above textboxes and other gizmos so the user knows what each doohickey is for. That's their whole job. Simple. No code whatsoever. (Other than the code written by the form designer.)

My issue is there are so damned MANY of them! My app has multiple tabs on the same form, and each tab has a lot of objects and each has an associated label. Since I never bothered to name them, I now have a label with the name Label224. That's right, 224 labels! It's getting ridiculous. When I view the properties window, sometimes I wish to select an object by name using the drop down and they force a lot of scrolling.

QUESTION: Is there a way to make the sheer number of unnamed labels (or any type of object that isn't referenced in code) less obtrusive during development?

I could load them in code of course. That would be fine, but it's nice to lay them out graphically without the trial and error of running the code to see if the positioning is right.

like image 414
PaulOTron2000 Avatar asked Jan 18 '12 19:01

PaulOTron2000


2 Answers

A couple of things.

Set each label's GenerateMember property to false:

label1.GenerateMember = False

This will prevent the Label from appearing in the code view combo boxes and intellisense.

Other than that, you just have too many controls.

You mentioned "tabs", in which case, you can try to move each tab of stuff into a separate usercontrol, and then you are only loading a single usercontrol for each tab. It doesn't reduce the number of controls, but it makes it a little more manageable.

The other "job" of a label is to provide mnemonic control activation for the next control in the tab order. If you are not using this, you can try "cheating" and getting rid of the labels all together, and "paint" the label next to each control in the container's paint event. You could put the display of the label in a control's tag property, and paint on that. Not necessarily the greatest suggestion.

Example:

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
  For Each c As Control In Me.Controls
    If c.Tag IsNot Nothing Then
      TextRenderer.DrawText(e.Graphics, c.Tag.ToString, Me.Font, _
                            New Rectangle(c.Left - 100, c.Top, 100, c.Height), _
                            Color.Black, Color.Empty, TextFormatFlags.VerticalCenter)
    End If
  Next
End Sub
like image 199
LarsTech Avatar answered Sep 22 '22 03:09

LarsTech


If you have a lot of TextBox controls with adjacent Labels, you could create a usercontrol which consists of both the TextBox and the Label. You can add whatever properties you like for customizing the appearance.

enter image description here

This is such a common UI pattern, I really think VS should come with a control like this.

It also means you can create new forms more quickly because you only have one control to add for each edit field, instead of two.

Here's another example of a usercontrol I have - this is just two labels, but the principle is the same:

enter image description here

Here it is being used several times on one form:

enter image description here

Instead of 18 labels on the form, I have 9 instances of my usercontrol.

like image 28
Igby Largeman Avatar answered Sep 18 '22 03:09

Igby Largeman