How to enable hyperlinks in EditControl?
You can enable hyperlinks in the EditControl by calculating the specified text positions with the following events.
- MouseMove
- MouseDown
MouseMove: Changes the cursor, when mouse hovers on the required text of the control.
MouseDown: Navigates the specified webpage, when mouse button is pressed on the required text of the control. The CurrentLineText property helps match the input text in the EditControl.
The following code example demonstrates the same.
C#
//Initializes the hyperlink text. private string text = "http://www.google.com"; //Initializes the file path. private string filePath = Path.GetDirectoryName(Application.ExecutablePath) + @"\..\..\hyper.php"; //Loads files in EditControl. this.editControl1.LoadFile(filePath); //Specifies the cursor. private void editControl1_MouseMove(object sender, MouseEventArgs e) { //Gets the mouse point. Point pointVirtual = editControl1.PointToVirtualPosition(new Point(e.X, e.Y)); if (pointVirtual.Y > 0) { //Gets the line. ILexemLine line = editControl1.GetLine(pointVirtual.Y); if (line != null) { ILexem lexem = line.FindLexemByColumn(pointVirtual.X); if (lexem != null) { IConfigLexem configLexem = lexem.Config as IConfigLexem; if (text.Contains(lexem.Text)) this.editControl1.Cursor = Cursors.Hand; else this.editControl1.Cursor = Cursors.Default; } else this.editControl1.Cursor = Cursors.Default; } } } //Navigates to the website. void editControl1_MouseDown(object sender, MouseEventArgs e) { if (this.editControl1.CurrentLineText.Contains(text) && text.Contains(this.editControl1.GetCurrentWord())) { //Opens the hyperlink in the webpage. ProcessStartInfo hyperlink = new ProcessStartInfo("http://www.google.com"); Process.Start(hyperlink); } }
VB
'Initializes the hyperlink text. Private textValue As String = "http://www.google.com" 'Initializes the file path. Private filePath As String = Path.GetDirectoryName(Application.ExecutablePath) & "\..\..\hyper.php" 'Loads files in EditControl. Me.editControl1.LoadFile(filePath) 'Specifies the cursor. Private Sub editControl1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) 'Gets the mouse point. Dim pointVirtual As Point = editControl1.PointToVirtualPosition(New Point(e.X, e.Y)) If pointVirtual.Y > 0 Then 'Gets the line. Dim line As ILexemLine = editControl1.GetLine(pointVirtual.Y) If line IsNot Nothing Then Dim lexem As ILexem = line.FindLexemByColumn(pointVirtual.X) If lexem IsNot Nothing Then Dim configLexem As IConfigLexem = TryCast(lexem.Config, IConfigLexem) If text.Contains(lexem.Text) Then Me.editControl1.Cursor = Cursors.Hand Else Me.editControl1.Cursor = Cursors.Default End If Else Me.editControl1.Cursor = Cursors.Default End If End If End If End Sub 'Navigates to the website. Private Sub editControl1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) If Me.editControl1.CurrentLineText.Contains(textValue) AndAlso textValue.Contains(Me.editControl1.GetCurrentWord()) Then 'Opens the hyperlink in the webpage. Dim hyperlink As New ProcessStartInfo("http://www.google.com") Process.Start(hyperlink) End If End Sub
PointToVirtualPosition helps to get the virtual position of the text. It also helps to converts the points in client coordinates to the virtual position in text.
Figure 1: EditControl shows with hyperlinks in C#
Figure 2: EditControl shows with hyperlinks in VB
Figure 3: Webpage loaded to browser after clicking the Hyperlink in EditControl
Sample Links: