Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Panels, Labels, GroupBox > How to know which link is "clicked" in the RadLabel_Click Event if it has Multiple HTML A REF tags in the RadLabel Text

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

Feed from this thread
  • Donald avatar

    Posted on Jun 7, 2011 (permalink)

    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.. 

    Attached files

    Reply

  • Peter Peter admin's avatar

    Posted on Jun 10, 2011 (permalink)

    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.

    Reply

  • Donald avatar

    Posted on Jun 13, 2011 (permalink)

    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

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Panels, Labels, GroupBox > How to know which link is "clicked" in the RadLabel_Click Event if it has Multiple HTML A REF tags in the RadLabel Text
Related resources for "How to know which link is "clicked" in the RadLabel_Click Event if it has Multiple HTML A REF tags in the RadLabel Text"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]