Example E2096
Visible to All Users

XAF - How to show a hyper link (URL, email, etc.) for a business class property

This example shows custom Property Editors that provide access to object fields with an email address or URL as clickable text.

To validate a URL, these editors use a combined RexEx mask. The default regular expression is:

Code
(((http|https|ftp)\://)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*)|([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})

You can use it as is or modify it per your specific needs. Search for Regular Expression in MSDN for more information on how to do this.
A double click opens a URL in DetailView of the WinForms project for end-user convenience.

These editors are created for learning purposes only. You can extend them or create your own editors to meet your business needs.

image

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

EFCore/HyperLinkEditorEF/HyperLinkEditorEF.Win/Editors/WinHyperLinkStringPropertyEditor.cs
C#
using System; using DevExpress.ExpressApp; using DevExpress.XtraEditors; using DevExpress.ExpressApp.Model; using DevExpress.XtraEditors.Mask; using DevExpress.ExpressApp.Editors; using System.Text.RegularExpressions; using DevExpress.XtraEditors.Controls; using DevExpress.ExpressApp.Win.Editors; using DevExpress.XtraEditors.Repository; namespace HyperLinkPropertyEditor.Win { [PropertyEditor(typeof(System.String), false)] public class WinHyperLinkStringPropertyEditor : StringPropertyEditor { public const string UrlEmailMask = @"(((http|https|ftp)\://)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*)|([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})"; private HyperLinkEdit hyperlinkEditCore; public WinHyperLinkStringPropertyEditor(Type objectType, IModelMemberViewItem info) : base(objectType, info) { } public new HyperLinkEdit Control { get { return hyperlinkEditCore; } } protected override RepositoryItem CreateRepositoryItem() { return new RepositoryItemHyperLinkEdit(); } protected override object CreateControlCore() { hyperlinkEditCore = new HyperLinkEdit(); return hyperlinkEditCore; } protected override void SetupRepositoryItem(RepositoryItem item) { base.SetupRepositoryItem(item); RepositoryItemHyperLinkEdit hyperLinkProperties = (RepositoryItemHyperLinkEdit)item; hyperLinkProperties.SingleClick = View is ListView; hyperLinkProperties.TextEditStyle = TextEditStyles.Standard; hyperLinkProperties.OpenLink += hyperLinkProperties_OpenLink; EditMaskType = EditMaskType.RegEx; hyperLinkProperties.Mask.MaskType = MaskType.RegEx; hyperLinkProperties.Mask.EditMask = UrlEmailMask; } private void hyperLinkProperties_OpenLink(object sender, OpenLinkEventArgs e) { e.EditValue = GetResolvedUrl(e.EditValue); } public static string GetResolvedUrl(object value) { string url = Convert.ToString(value); if (!string.IsNullOrEmpty(url)) { if (url.Contains("@") && IsValidUrl(url)) return string.Format("mailto:{0}", url); if (!url.Contains("://")) url = string.Format("http://{0}", url); if (IsValidUrl(url)) return url; } return string.Empty; } private static bool IsValidUrl(string url) { return Regex.IsMatch(url, UrlEmailMask); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.