I have to create custom buttons but in order to encapsulate the logic of button creation I've this class (VB.net):
Public Class ButtonCreator
Public Shared Function CreateBaseButton(Optional label As String = "") As Button
Dim btn As New Button()
btn.Text = label
btn.Size = New Size(100, 25)
btn.Anchor = CType((AnchorStyles.Top Or AnchorStyles.Bottom), AnchorStyles)
Return btn
End Function
Public Shared Function CreateClickableButton(clickHandler As EventHandler, Optional label As String = "") As Button
Dim btn As Button = CreateBaseButton(label)
AddHandler btn.Click, clickHandler
Return btn
End Function
Private Shared Function CreateCloseButton(result As DialogResult, Optional label As String = "")
Dim btn As Button = CreateClickableButton((Sub(o, e)
o.FindForm().DialogResult = result
o.FindForm().Close()
End Sub), label)
Return btn
End Function
Public Shared Function CreateContinueButton(Optional label As String = "")
Return CreateCloseButton(DialogResult.Yes, label)
End Function
Public Shared Function CreateStopButton(Optional label As String = "")
Return CreateCloseButton(DialogResult.No, label)
End Function
End Class
Other classes can call these method to create very basic buttons and add them to different panel containers.
I've tried to make the methods as cohesive as possible but I'm still uncomfortable with my solution, and I don't know why. I suspect that this looks akward because I have a static class full of static methods that could be somehow refactored, but I can't see any other way to achieve a better design.
Is it possible that this is a code smell? (I couldn't find anything online) What are the possible solutions to this?