Quantcast
Viewing all articles
Browse latest Browse all 32062

Create hyperlinks in string inside richtextbox in WPF

   Hi Team,

Below i faces issues in String using RichText box control 

for example :

Hi Welcome website like www.google.com and visit this site lot of things 

when i bind this strings in our RichText box control , unable show mid word like hyberlink (www.google.com)

below code i used 


 public static void Hyperlink(this RichTextBox rtb, Paragraph paragraph, string text)
        {
         
            var unmatchedCharIndices = Enumerable.Range(0, text.Length);
            MatchCollection mactches = regexUrl.Matches(text);
            foreach (Match match in mactches)
            {
                unmatchedCharIndices = unmatchedCharIndices.Except(Enumerable.Range(match.Index, match.Length));
               
                Hyperlink linkURl = null;
                if (match.Success)
                {
                  
                                var unmatchedStrings = unmatchedCharIndices
                   .Select((n, i) => new { n, i })
                   .GroupBy(x => x.n - x.i) //this line will group consecutive nums in the seq
                   .Select(x => text.Substring(x.First().n, x.Count()));
                    foreach (var unmatchedString in unmatchedStrings)
                    {
                     int intMid=   text.IndexOf(match.Value);
                     string str1 = text.Substring(0, intMid);
                        int urlLength=match.Value.Length;
                        int totalLength=text.Length;
                        string str2 = text.Substring(0 , totalLength);  
                   
                        paragraph.Inlines.Add(unmatchedString.ToString());
                    }

                    linkURl = new Hyperlink();
                    Uri serverUri = new Uri("http://" + match.Value.Trim());
                    linkURl.IsEnabled = true;
                    linkURl.NavigateUri = serverUri;
                    linkURl.ToolTip = match.Value.Trim();
                    linkURl.Inlines.Add(match.Value.Trim());


                    //  text.Replace(match.Value, "<a href='#provide'>provide</a>");
                    paragraph.Inlines.Add(linkURl);
                    
                }

            }
                    
               
        
                           
                                
            
            rtb.Document.Blocks.Add(paragraph);
}
please give your suggestion and needful 

thanks 

Viewing all articles
Browse latest Browse all 32062

Trending Articles