Inserting custom made links into RadTextBoxControl

1 Answer 189 Views
TextBoxControl
Michael
Top achievements
Rank 1
Veteran
Michael asked on 24 Oct 2022, 04:55 AM

Hello,

I have a small terminal type app that displays some data in a RadTextBoxControl as it arrives. Events are fired and data is displayed on the screen by way of the RadTextBoxControl. That part works great.  

When certain strings arrive, I'd like to replace it with a hyperlink or button of sorts. I found the example for Creating Custom Blocks (https://docs.telerik.com/devtools/winforms/controls/editors/textboxcontrol/creating-custom-blocks) and this looked really promising. But, the issue I had there is that this event fires on on lots of different characters. Meaning, you can't use it to parse out a line of text and then replace it with a custom object. You parse each block as it comes in.

Ideally, I want certain types of text, like "<link cmd=".cm*">Run custom command</link>" to be replaced with some text that looks like "Run custom command" but in a way that it acts like a hyperlink where I can register for its click event and do whatever I need to do. This is proving to be pretty tricky for me but I have a feeling Telerik controls probably have a way :)

Thanks,

Michael

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 26 Oct 2022, 01:46 PM

Hello Michael,

Thank you for your interest in our RadTextBoxControl.

I have checked your scenario and you are right that using the custom blocks approach will not work in this case. The syntax for the hyperlink will be iterated char by char and using the CreateTextBlock event won't do the trick. I have tried different approaches but wasn't able to make them work. To think of a suitable solution, may I ask you to share how the control Text property is populated on your side? The hyperlink syntax is embedded in the string and the RadTextBoxControl is just showing the result or the user is entering the link. You can share any details regarding your scenario so that I could try to find a more suitable control for your case.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Michael
Top achievements
Rank 1
Veteran
commented on 27 Oct 2022, 04:07 PM

Hi Dinko,

Thank you for taking the time to look at this.  To create a little sample app, create a test project, add a radform and radtextbox, add a button if you like. I used .net core project, so in the .net6-windows folder, create a test file called test.cmd. Fill it with this:

@echo off
@echo This line has no links
@echo This line has no links
@echo "This line some links <link cmd=SomeCommand>Click me</link>"
@echo this line has no links.
@echo "This line some links <link cmd=Another command>Click me</link>"

Then, in your form's code window, use the following code:


using System;
using System.Diagnostics;

namespace LinkTest
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
        }

        private void RunProcess()
        {
            RichTextBox.Text = String.Empty;

            Process proc = new Process();

            proc.StartInfo.FileName = "test.cmd";

            proc.StartInfo.UseShellExecute = false;

            proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;

            proc.StartInfo.CreateNoWindow = true;

            proc.StartInfo.RedirectStandardOutput = true;

            proc.OutputDataReceived += OnDataReceived;

            proc.Start();

            proc.BeginOutputReadLine();

            proc.WaitForExit();

        }

        private void OnDataReceived(object sender, DataReceivedEventArgs e)
        {
            var line = e.Data;
            if (line != null)
            {
                RichTextBox.Insert(line + Environment.NewLine);
            }

        }

        private void RunButton_Click(object sender, EventArgs e)
        {
            RunProcess();
        }
    }
}

 

My goal was to parse out the <link> tags and replace them with real hyperlinks that I can handle in code behind.

Hope that helps clarify it. Thanks again for looking at this.

Michael

Dinko | Tech Support Engineer
Telerik team
commented on 01 Nov 2022, 08:34 AM

Thank you for the additional details.

What I can suggest covering your scenario is to use our RadRichTextEditor control. The control display and edits rich-text content, including formatted text arranged in pages, paragraphs, spans (runs), tables, and as in your case hyperlinks. You can check the following articles which I think will help you start with the control:

For your convenience, I have created a sample project to demonstrate what I have in mind. I have slightly modified the text file so that it includes the actual URL address.

@echo off
@echo This line has no links
@echo This line has no links
@echo "This line some links <link cmd=www.telerik.com>Click me</link>"
@echo this line has no links.
@echo "This line some links <link cmd=www.google.com>Click me</link>"

Here is the result from my side:

Michael
Top achievements
Rank 1
Veteran
commented on 16 Dec 2022, 02:03 PM

Hi Dinko,

I was able to get this to work partially. If you add any additional text immediately after inserting a hyperlink, the color of the font will be changed to black. I don't know why it does this and I couldn't find any work around. In my app, my rich text editor is a dark grey color (visual studio theme) with white text. I gave up on trying to implement the links for now. 

Thank you again for taking the time to help me on this. Your time is appreciated.

Regards,

Michael

Dinko | Tech Support Engineer
Telerik team
commented on 16 Dec 2022, 03:07 PM

Hello Michael,

I have tested your approach but the color of the hyperlink is not changed to black. I have tested it on my side but wasn't able to reproduce it.

If you send me a sample project which demonstrates the described behavior I could take a look on my side. 

Michael
Top achievements
Rank 1
Veteran
commented on 16 Dec 2022, 09:08 PM

The text after the link would be black, like it is in your picture. Change the background of your texbox to a dark color, or use the visual studio theme like i am, then you should see "some text here" is black. Hope that helps.
Dinko | Tech Support Engineer
Telerik team
commented on 21 Dec 2022, 09:32 AM

I am still confused about what is not working. Any string different from the hyperlink and placed afterward will have the same font. I have used the VisualStudio2012Dark theme and the result is expected, at least in my mind. 

Here is the code which I used to add more text. Maybe you are using a different approach.

this.radRichTextEditor1.Insert(text);
this.radRichTextEditor1.InsertHyperlink(info, "Click me");
this.radRichTextEditor1.Insert(" "+"Some Text Here");

The code is from the attached project. Can you send me pictures of what you are expecting and what is the result of your application? You could modify the project if you are using a different approach.

 

Tags
TextBoxControl
Asked by
Michael
Top achievements
Rank 1
Veteran
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or