Back to Devexpress

How to: Implement Hyperlink Functionality by Handling OpenLink Event

windowsforms-9489-controls-and-libraries-editors-and-simple-controls-examples-how-to-implement-hyperlink-functionality-by-handling-openlink-event.md

latest2.8 KB
Original Source

How to: Implement Hyperlink Functionality by Handling OpenLink Event

  • Oct 25, 2019
  • 2 minutes to read

The following example shows how to implement hyperlink functionality by handling the HyperLinkEdit.OpenLink event. The hyperlink command is not required by this example.

Instead we handle the HyperLinkEdit.OpenLink event, which occurs before activating the hyperlink, and then run a custom command. In this example, we open Explorer displaying the path for the current executable file. The OpenLinkEventArgs.Handled property of the event parameter is set to true. This disables subsequent default processing.

To specify the display text for the editor, the RepositoryItemHyperLinkEdit.Caption property is set to a specific string (“Show Startup Directory”). If RepositoryItemHyperLinkEdit.Caption contains a non-empty string, the editor will always display this value in the edit box. This allows you to store specific data in the edit value while representing them using the RepositoryItemHyperLinkEdit.Caption string.

The following image shows a hyperlink editor with a customized caption and Explorer activated by the editor:

csharp
hyperLinkEdit1.Properties.Caption = "Show Startup Directory";
// ...
void hyperLinkEdit1_OpenLink(object sender, DevExpress.XtraEditors.Controls.OpenLinkEventArgs e) {
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    process.StartInfo.FileName = Application.StartupPath;
    process.StartInfo.Verb = "explore";
    process.StartInfo.WindowStyle = (sender as HyperLinkEdit).Properties.BrowserWindowStyle;
    process.Start();
    e.Handled = true;
}
vb
Me.HyperLinkEdit1.Properties.Caption = "Show Startup Directory"
'...
Private Sub HyperLinkEdit1_OpenLink(ByVal sender As System.Object, _
  ByVal e As DevExpress.XtraEditors.Controls.OpenLinkEventArgs) _
  Handles HyperLinkEdit1.OpenLink
    Dim process As New System.Diagnostics.Process()
    process.StartInfo.FileName = Application.StartupPath
    process.StartInfo.Verb = "explore"
    process.StartInfo.WindowStyle = (TryCast(sender, HyperLinkEdit)).Properties.BrowserWindowStyle
    process.Start()
    e.Handled = True
End Sub