How to know which link is "clicked" in the RadLabel_Click Event if it has Multiple HTML A REF tags in the RadLabel Text

7 posts, 0 answers
  1. Donald
    Donald avatar
    7 posts
    Member since:
    Jun 2011

    Posted 07 Jun 2011 Link to this post

    Hi,
    I did look at other posts involving creating HTML links in the RadLabel control but they all are navigating to some web page.

    I don't know if this is possible but i wanted to "detect" which link is clicked in the RadLabel_Click event and then load some Winforms based on the link.

    To make this simple lets say for example, the RadLabel has some HTML text with say 3 link tags.

     

     

     

    RadLabel1.Text = "<html><span><a href='??'>Link 1</a>, <a href='??'>Link 2</a>, <a href='??'>Link 3</a</span></html>"

    When user click on "link 1", i would show message box "Link 1" or load Form1, click on Link 2, show message box "Link 2" or load  Form2 and so on. I put "??" in the href as am not sure what will go there yet.

    Is this possible? I know it can be done with the LinkControl but i really like the HTML formatting available with a RadLabel.

    Thanks
    Don

    Edit:
    I do see a "Other" option in the hyperlink type option.(image attached)
    Please could you tell me how to use this option? Can i use this option to load winforms?
    Thanks.. 

  2. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 10 Jun 2011 Link to this post

    Hi Donald,

    Thank you for the question.

    Our Html-like feature does not support this functionality out of the box and you can detect the link that the user have pressed after the press action.

    Please refer to the code snippet which demonstrates how to detect the pressed link:

    this.radLabel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.radLabel1_MouseDown);
     
     private FormattedText IsMouseOverBlock(FormattedTextBlock textBlock, MouseEventArgs e)
            {                 
                Point elementAtPoint = this.radLabel1.LabelElement.PointFromControl(e.Location);
                int linesCount = textBlock.Lines.Count;
                for (int i = 0; i < linesCount; ++i)
                {
                    TextLine textLine = textBlock.Lines[i];
                    int textLineCount = textLine.List.Count;
                    for (int j = 0; j < textLineCount; ++j)
                    {
                        FormattedText formattedText = textLine.List[j];
                        if (!string.IsNullOrEmpty(formattedText.Link) && formattedText.DrawingRectangle.Contains(elementAtPoint))
                        {
                            return formattedText;//found link under mouse
                        }
                    }
                }
     
                return null;//notfound
            }
     
            private void radLabel1_MouseDown(object sender, MouseEventArgs e)
            {
                FieldInfo pi = this.radLabel1.LabelElement.Children[2].Children[1].GetType().GetField("textPrimitiveImpl", BindingFlags.Instance | BindingFlags.SetProperty |BindingFlags.NonPublic);
                TextPrimitiveHtmlImpl text = (TextPrimitiveHtmlImpl)pi.GetValue(this.radLabel13.LabelElement.Children[2].Children[1]);
                  
     
                FormattedTextBlock textBlock = text.TextBlock;
                FormattedText clickedLink = IsMouseOverBlock(textBlock, e);
                if (clickedLink != null)
                {
                    MessageBox.Show(clickedLink.Text + " pressed");
                }
            }

    I hope this helps.

    Regards,
    Peter
    the Telerik team
    Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Donald
    Donald avatar
    7 posts
    Member since:
    Jun 2011

    Posted 13 Jun 2011 Link to this post

    Thanks Peter for the code snippet. I was able to detect which link was clicked.
    I can now use the HTML tags to create a "linkable" RadLabel.
    Thanks again,
    don
  5. Thomas
    Thomas avatar
    15 posts
    Member since:
    Oct 2012

    Posted 22 May 2014 Link to this post

    Hi,
    I know this is an old post, but I was just wondering if the approach described by Peter, still is the way to get this functionality?
    Or is there by now, a simpler/more readalbe way to do this ?

    /Thomas
  6. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 26 May 2014 Link to this post

    Hello Thomas,

    Thank you for writing.

    The approach, introduced by Peter for detecting which link is clicked in a HTML-like text formatted RadLabel is still the appropriate way to achieve this requirement. HTML-like Text Formatting is just an advanced text styling mechanism, which can be applied to all Telerik controls for WinForms and their elements and it does not support the mentioned functionality out of the box. If we have more requests for this we will consider it in the future improvement of the HTML-like Text Formatting functionality.

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  7. Stephen McDaniel
    Stephen McDaniel avatar
    35 posts
    Member since:
    Mar 2008

    Posted 08 Jul 2015 in reply to Dess Link to this post

    I would also like there to be some built-in way to do this.  I can somewhat use the code above to detect clicks but it doesn't seem ideal. For example, I can't find a clean way to stop the built-in click behavior.  I want to cancel the default action (of doing a Process.Start on the link text) and instead do something else.  I've found that I can clear out the LinkText after the control has drawn and then the normal Process.Start won't be called.  But then when I clear out the LinkText, then the cursor doesn't turn to a hand when I hover over the link.

     Is there some way to be able to cancel the default action but still have everything behave normally?  Or better yet, it would be great if this became standard functionality.

  8. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 13 Jul 2015 Link to this post

    Hi Stephen,

    Thank you for writing.

    The Html-like Text Formatting is basically an advanced styling mechanism which would allow you to easily format your labels using Html tags. As seen from the answers of my colleagues having multiple links in one label and differentiating them in a click event is not supported out of the box. 

    This functionality, however, can be accomplished. Considering your question, I am not completely sure that I understand it properly. Removing the link text in <a> tag results in an empty link hence the cursor does not turn into a hand. If you would like to override the default implementation of some events you can create a custom RadLabel control and override its input methods, e.g.:
    public partial class MyRadLabel : RadLabel
    {
        public MyRadLabel()
        {
            InitializeComponent();
        }
     
        protected override void OnMouseDown(MouseEventArgs e)
        {
            //base.OnMouseDown(e);
        }
     
     
        protected override void OnClick(EventArgs e)
        {
            //base.OnClick(e);
        }
    }

    Not calling the base implementation will prevent the built-in functionality from executing.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready