Back to Devexpress

HyperLinkEdit Class

windowsforms-devexpress-dot-xtraeditors-c1dc9baa.md

latest10.7 KB
Original Source

HyperLinkEdit Class

The editor to display and edit hyperlinks and navigate to their targets.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
public class HyperLinkEdit :
    ButtonEdit
vb
Public Class HyperLinkEdit
    Inherits ButtonEdit

Remarks

A hyperlink editor presents its contents as a hyperlink. The text displayed in the edit box is underlined. When you activate the hyperlink functionality, the editor executes the command specified by the HyperLinkEdit.Text property.

The text displayed in the edit box may not match the command to execute. The display text is determined by the RepositoryItemHyperLinkEdit.Caption property. If the property represents an empty string, the display text matches the editor’s HyperLinkEdit.Text property. Otherwise, the display text is determined by the RepositoryItemHyperLinkEdit.Caption. Note if the RepositoryItemHyperLinkEdit.Caption is set to a non-empty string, the end-user is not able to modify the editor’s contents.

By default, the RepositoryItemHyperLinkEdit.TextEditStyle property is set to TextEditStyles.DisableTextEditor. This prevents the end-user from modifying and selecting the text. In order to enable editing and text selection, set the property to TextEditStyles.Standard.

The command in the HyperLinkEdit.Text property can represent any valid command recognized by the system. You can also specify the email subject, caption, attachments, etc. For instance, HyperLinkEdit.Text can specify:

  • a URL to open in the default browser (“www.devexpress.com”)

  • a “mailto:” command which activates the default mail client (“mailto:[email protected]”)

  • a path to a file or directory to open (“c:\Program Files”, “\Server\Public\setup.exe”)

  • C#

  • VB.NET

csharp
hyperLinkEdit1.EditValue = "mailto:[email protected]&subject=TestSubject";
vb
hyperLinkEdit1.EditValue = "mailto:[email protected]&subject=TestSubject"

To activate the hyperlink functionality, you can:

The hyperlink editor provides a RepositoryItemHyperLinkEdit.OpenLink event to control hyperlink execution. Using this event, you can perform an action not recognized by the system, modify the command to execute, etc. For instance, if the hyperlink editor contains an e-mail without the “mailto:” prefix, the default hyperlink processing will do nothing. However, you can handle the RepositoryItemHyperLinkEdit.OpenLink event in order to make up a valid command by adding a “mailto:” prefix.

This example uses RepositoryItemHyperLinkEdit to display hyperlinks in the “Url” column of a grid.

csharp
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;

namespace DXApplication4 {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        RepositoryItemHyperLinkEdit hyperLinkEdit;
        public Form1() {
            InitializeComponent();
            InitGridControl();
            InitHyperlinkEditor("Url");
        }
        void InitHyperlinkEditor(string fieldName){
            hyperLinkEdit = new RepositoryItemHyperLinkEdit() {
                Name = "repositoryItemHyperLinkEdit1",
                SingleClick = true,
            };
            gridControl1.RepositoryItems.Add(hyperLinkEdit);
            gridView1.Columns[fieldName].ColumnEdit = hyperLinkEdit;
        }
        void InitGridControl() {
            gridControl1.DataSource = InitData();
            gridControl1.ForceInitialize();
            gridView1.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDown;
            gridView1.Columns["Url"].OptionsColumn.ReadOnly = true;
        }
        List<DataItem> InitData(){
            return new List<DataItem>() {
                new DataItem(){ Url = "https://www.devexpress.com" },
                new DataItem(){ Url = "https://www.devexpress.com/buy" },
                new DataItem(){ Url = "https://www.devexpress.com/try" },
                new DataItem(){ Url = "https://demos.devexpress.com" }
            };
        }
    }
    public class DataItem {
        public string Url { get; set; }
    }
}
vb
Imports DevExpress.Utils
Imports DevExpress.XtraEditors.Repository
Imports System.Collections.Generic

Namespace DXApplication4
    Partial Public Class Form1
        Inherits DevExpress.XtraEditors.XtraForm

        Private hyperLinkEdit As RepositoryItemHyperLinkEdit
        Public Sub New()
            InitializeComponent()
            InitGridControl()
            InitHyperlinkEditor("Url")
        End Sub
        Private Sub InitHyperlinkEditor(ByVal fieldName As String)
            hyperLinkEdit = New RepositoryItemHyperLinkEdit() With {
                .Name = "repositoryItemHyperLinkEdit1",
                .SingleClick = True
            }
            gridControl1.RepositoryItems.Add(hyperLinkEdit)
            gridView1.Columns(fieldName).ColumnEdit = hyperLinkEdit
        End Sub
        Private Sub InitGridControl()
            gridControl1.DataSource = InitData()
            gridControl1.ForceInitialize()
            gridView1.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDown
            gridView1.Columns("Url").OptionsColumn.ReadOnly = True
        End Sub
        Private Function InitData() As List(Of DataItem)
            Return New List(Of DataItem)() From {
                New DataItem() With {.Url = "https://www.devexpress.com"},
                New DataItem() With {.Url = "https://www.devexpress.com/buy"},
                New DataItem() With {.Url = "https://www.devexpress.com/try"},
                New DataItem() With {.Url = "https://demos.devexpress.com"}
            }
        End Function
    End Class
    Public Class DataItem
        Public Property Url() As String
    End Class
End Namespace

Example

Suppose that a hyperlink editor is used to display e-mails. When you activate the hyperlink, the default mail client should be opened for the specified address. The problem is that a hyperlink editor will run the mail client only if the command contains a “mailto:” prefix. So a command such as “[email protected]” is not recognized by the editor by default.

Assuming that the hyperlink editor represents e-mails, we handle the HyperLinkEdit.OpenLink event and check whether the command contains the “mailto:” prefix. If not, we add it to the command. After the event handler is processed, the command will be executed by the editor (the OpenLinkEventArgs.Handled property of the event parameter is false by default) and this will open the mail client.

csharp
using DevExpress.XtraEditors.Controls;

this.hyperLinkEdit2.EditValue = "[email protected]";
//...
private void hyperLinkEdit2_OpenLink(object sender, OpenLinkEventArgs e) {
    const string mailPrefix = "mailto:";
    if(!e.EditValue.ToString().ToLower().StartsWith(mailPrefix)) {
        e.EditValue = mailPrefix + e.EditValue.ToString();
    }
}
vb
Me.HyperLinkEdit2.EditValue = "[email protected]"
'...
Private Sub HyperLinkEdit2_OpenLink(ByVal sender As System.Object, _
  ByVal e As DevExpress.XtraEditors.Controls.OpenLinkEventArgs) _
  Handles HyperLinkEdit2.OpenLink
    Const mailPrefix As String = "mailto:"
    If Not e.EditValue.ToString().ToLower().StartsWith(mailPrefix) Then
        e.EditValue = mailPrefix + e.EditValue.ToString()
    End If
End Sub

Implements

IXtraResizableControl

Inheritance

Show 11 items

Object MarshalByRefObject Component Control DevExpress.XtraEditors.XtraControl ControlBase BaseControl BaseEdit TextEdit ButtonEdit HyperLinkEdit

See Also

HyperLinkEdit Members

HyperlinkLabelControl

DevExpress.XtraEditors Namespace