Article 4ZSJ0 CodeSOD: The Label Printer

CodeSOD: The Label Printer

by
Remy Porter
from The Daily WTF on (#4ZSJ0)

If you create a UI object in code, you have to go through all that pesky, annoying work of initalizing that object so it displays correctly. I mean, who wants to write VB.NET code which looks like this:

Label = New LabelLabel.Size = New Size(710, 300)Label.TextLB = "Operation:"someForm.Controls.Add(Label)

Here, we've created a label object which overrides a handful of default properties, and then gets added to the screen. You'll have to do that for every control! Obviously, you need a method.

No, not a method called something like OperationLabel() which creates and adds the Operation: label to the form. You have to think more generic than that. We want a method which allows us to add any label, with any configuration, to the form.

Benjamin's co-worker has an idea exactly about how to accomplish that. Instead of that ugly block of code above, you can write this gorgeous code:

CreateRectLabel(Label, "Label", String.Empty, New Point(290, 5), someForm, Color.Red, Color.White, Color.White, 710, 300, Color.White, "SomeText", String.Empty, New Font(MainFontName, 48, FontStyle.Bold), New Font(MainFontName, 16, FontStyle.Bold), New Font(MainFontName, 28, FontStyle.Bold), TextEffects.Floating, TextEffects.Floating, TextEffects.Floating, Lang.InstructionErrors.PushInRedButton, TextEffects.Floating, New Font(MainFontName, 34, FontStyle.Bold), Color.White, Color.White, TextEffects.None, "", Nothing, Color.White, TextEffects.None, "", New Font(MainFontName, 12, FontStyle.Regular), Color.White, TextEffects.None, "", New Font(MainFontName, 12, FontStyle.Regular))

Now you have to directly set every one of those parameters, no exceptions! You'll always have to be perfectly explicit about what settings you're using.

Here is the implementation of that CreateRectLabel method:

 Public Sub CreateRectLabel(ByRef LabelObj As Label, ByVal LabelName As String, ByVal TextLT As String, ByVal LabelLocation As Point, ByRef PanelContainer As Object, ByVal LabelColor As Color, ByVal TextColorLT As Color, ByVal TextColorCaption As Color, ByVal LabelWidth As Integer, ByVal LabelHeight As Integer, ByVal TextColorLB As Color, ByVal TextLB As String, ByVal TextCaption As String, ByVal FontLT As Font, ByVal FontCaption As Font, ByVal FontLB As Font, ByVal TextEffectLT As TextEffects, ByVal TextEffectCaption As TextEffects, ByVal TextEffectLB As TextEffects, ByVal TextLM As String, ByVal TextEffectLM As TextEffects, ByVal FontLM As Font, ByVal TextColorLM As Color, ByVal TextColorRM As Color, ByVal TextEffectRM As TextEffects, ByVal TextRM As String, ByVal FontRM As Font, ByVal TextColorCB As Color, ByVal TextEffectCB As TextEffects, ByVal TextCB As String, ByVal FontCB As Font, ByVal TextColorCT As Color, ByVal TextEffectCT As TextEffects, ByVal TextCT As String, ByVal FontCT As Font, Optional ByVal LabelSpecialEffects As SpecialEffects = SpecialEffects.None) LabelObj = New Label LabelObj.Name = LabelName LabelObj.Size = New Size(LabelWidth, LabelHeight) LabelObj.Location = LabelLocation LabelObj.Shape = Shapes.Rectangle LabelObj.ShadowMode = ShadowModes.Blurred LabelObj.ColorSurfaceNormal.Render3DType = ColorRenderType.Best3D LabelObj.ColorSurfaceNormal.BackColor = LabelColor LabelObj.ColorSurfaceNormal.BackColor2 = LabelColor LabelObj.ColorSurfaceNormal.GradientType = ColorGradientType.Classic LabelObj.ColorSurfaceNormal.GradientFactor = 4 LabelObj.Surface = Surfaces.HardPillow LabelObj.SmoothEdges = Smooths.High LabelObj.SpecialEffect = LabelSpecialEffects ' Left Top Text LabelObj.TextDescrLT.ColorNormal = TextColorLT LabelObj.TextDescrLT.SpecialEffect = TextEffectLT LabelObj.TextDescrLT.Text = TextLT LabelObj.FontLeftTop = FontLT ' Center Top Text LabelObj.TextDescrCT.ColorNormal = TextColorCT LabelObj.TextDescrCT.SpecialEffect = TextEffectCT LabelObj.TextDescrCT.Text = TextCT LabelObj.FontCenterTop = FontCT ' Caption Text LabelObj.ForeColor = TextColorCaption LabelObj.FontCaption = FontCaption LabelObj.TextDescrCaption.SpecialEffect = TextEffectCaption LabelObj.Text = TextCaption ' Left Middle Text LabelObj.TextDescrLM.ColorNormal = TextColorLM LabelObj.TextDescrLM.SpecialEffect = TextEffectLM LabelObj.TextDescrLM.Text = TextLM LabelObj.FontLeftMiddle = FontLM ' Left Bottom Text LabelObj.TextDescrLB.ColorNormal = TextColorLB LabelObj.TextDescrLB.SpecialEffect = TextEffectLB LabelObj.TextDescrLB.Text = TextLB LabelObj.FontLeftBottom = FontLB ' Center Bottom Text LabelObj.TextDescrCB.ColorNormal = TextColorCB LabelObj.TextDescrCB.SpecialEffect = TextEffectCB LabelObj.TextDescrCB.Text = TextCB LabelObj.FontCenterBottom = FontCB ' Right Middle Text LabelObj.TextDescrRM.ColorNormal = TextColorRM LabelObj.TextDescrRM.SpecialEffect = TextEffectRM LabelObj.TextDescrRM.Text = TextRM LabelObj.FontRightMiddle = FontRM LabelObj.Visible = True PanelContainer.Controls.Add(LabelObj) End Sub

No, there are no overloads. Yes, they quite clearly understood what optional paremeters are. No, they didn't want to use them for anything else but the LabelSpecialEffects parameter.

In any case, when you want to change a setting on a label, enjoy counting parameters to figure out where exactly you should make the change.

proget-icon.png [Advertisement] ProGet can centralize your organization's software applications and components to provide uniform access to developers and servers. Check it out! TheDailyWtf?d=yIl2AUoC8zAZhlWKEG6uU0
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments