HyperLinkEdit Class
The editor to display and edit hyperlinks and navigate to their targets.
Namespace: DevExpress.XtraEditors
Assembly: DevExpress.XtraEditors.v24.2.dll
Declaration
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:noreply@devexpress.com”)
- a path to a file or directory to open (“c:\Program Files”, “\Server\Public\setup.exe”)
To activate the hyperlink functionality, you can:
- click or double-click the editor’s contents depending upon the RepositoryItemHyperLinkEdit.SingleClick property value
- press the shortcut specified by RepositoryItemHyperLinkEdit.StartKey
- call the HyperLinkEdit.ShowBrowser method without parameters
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.
How to Display Hyperlinks in the DevExpress Data Grid
This example uses RepositoryItemHyperLinkEdit to display hyperlinks in the “Url” column of a grid.
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; }
}
}
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 “noreply@devexpress.com” 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.
using DevExpress.XtraEditors.Controls;
this.hyperLinkEdit2.EditValue = "noreply@devexpress.com";
//...
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();
}
}