Good way to add context-sensitive help to an existing Windows Forms application?
I have to add data开发者_运维百科base-driven tooltips to an existing WinForms App, big one. C# and .NET 3.5
I'd like to avoid having to drop new controls for this, and the user has to be able to edit the help tooltips.
My best guess is to wrap the existing controls in a new type that contains new properties, so that I can assign a new property like "FieldHelpName", and can use that in the administrative module so the user can identify the field clearly. I'd assign a ScreenID to each form and each FieldHelpName record would be linked to a ScreenID. At application startup, load all the help contents, and on form-load, filter by its ScreenID and add the corresponding tooltips using reflection, most likely.
I'm looking for suggestions on how to do this process best or to know if there are any best practices on how to do this...so any help is really appreciated. Thanks.
Why go into such lengths?
You can accomplish the same thing with something simpler:
Private _ToolTipList As New List(Of ToolTip)
<Extension()> _
Public Function CreateForm(ByVal formType As Type) As Form
If (formType Is Nothing) Then
Throw New ArgumentNullException("formType")
End If
If (Not GetType(Form).IsAssignableFrom(formType)) Then
Throw New InvalidOperationException _
(String.Format("The type '{0}' is not a form.", formType.FullName))
End If
Dim ctor = formType.GetConstructor(New Type() {})
If (ctor Is Nothing) Then
Throw New InvalidOperationException _
(String.Format _
("The type '{0}' does not have a public default constructor.", _
formType.FullName))
End If
Dim frm As Form = ctor.Invoke(New Object() {})
Dim toolTip As New ToolTip(New Container())
LoadToolTipData(toolTip, frm)
_ToolTipList.Add(toolTip)
Return frm
End Function
Private Sub LoadToolTipData(ByVal toolTip As ToolTip, _
ByVal ctrl As Control, _
Optional ByVal parentHierarchy As String = "")
Dim currentHierarchy = parentHierarchy & "." & ctrl.Name
Dim toolTipText = LoadDataFromDb(currentHierarchy)
If Not String.IsNullOrEmpty(toolTipText) Then
toolTip.SetToolTip(ctrl, toolTipText)
End If
For Each c As Control In ctrl.Controls
LoadToolTipData(toolTip, c, currentHierarchy)
Next
End Sub
Private Function LoadDataFromDb(ByVal key As String) As String
Return String.Empty
End Function
Ended up creating a database table for configuration and specifying control name for each row, and then looping screen controls recursively to add tooltips if the current control name matches the database record's control name.
精彩评论