Results 1 to 10 of 10

Thread: AutoDetecting Link in WPF

  1. #1

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    AutoDetecting Link in WPF

    Hello to everyone, I'm having a lot of problems while trying to achieve the following:

    I'm developing a Twitter client, and I'd like the update status text field to detect when ever the user wrote a link (for instance if the user entered google.com) insert an hyperlink right away.

    I've been working really hard on this, and so far I get the links inserted into a RichtextBox, but whenever a link is located twice in the RichtextBox the link is inserted again between the first link.

    Since I'm using a string finder system to a string in the RichtextBox and then check if it is a link or not, whenever I get two same link sin the RichtextBox the first link is located by the search.

    I would appreciate if any body could help me, I wouldn't like to make the users manually insert links into the twitter status update field.
    C# and WPF developer
    My Website:
    http://singlebits.com/

  2. #2
    Member
    Join Date
    Jul 2010
    Posts
    63

    Re: AutoDetecting Link in WPF

    Bump...
    Does anybody have idea how to make it (i have same problem) ?
    I making chat app and when Richtextbox update text, and if text have link, its not underlined or anything... its look like classic TEXT ...
    Any idea ???
    Thanks in advice

  3. #3

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: AutoDetecting Link in WPF

    Quote Originally Posted by stefanACM View Post
    Bump...
    Does anybody have idea how to make it (i have same problem) ?
    I making chat app and when Richtextbox update text, and if text have link, its not underlined or anything... its look like classic TEXT ...
    Any idea ???
    Thanks in advice
    I have found a solution by myself already, I will post it later today.

    It's simply creating a custom control inherited from RichTextbox and watch text changes event. Matching, comparison,etc..
    C# and WPF developer
    My Website:
    http://singlebits.com/

  4. #4
    Member
    Join Date
    Jul 2010
    Posts
    63

    Re: AutoDetecting Link in WPF

    Ok please
    Thanks

  5. #5

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: AutoDetecting Link in WPF

    Do you already have a link detection and insertion code ? or it's just the highlighting that's causing problems ?
    C# and WPF developer
    My Website:
    http://singlebits.com/

  6. #6
    Member
    Join Date
    Jul 2010
    Posts
    63

    Re: AutoDetecting Link in WPF

    I dont have anything...
    When somebody send message on chat, server sending it to all users, and client app append it to RichTextbox...but...it is clearly look like normal text...no underline, no diferent color, no highlight....
    So apsolutly no link detection :|

  7. #7

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: AutoDetecting Link in WPF

    Take a look at this code:

    csharp Code:
    1. private Paragraph ParseLinks (string Source)
    2.         {
    3.             Paragraph Input = new Paragraph(new Run(Source));
    4.  
    5.             char[] Sep = new char[] { ' ' };      
    6.             string[] Words = new TextRange(Input.ContentStart, Input.ContentEnd).Text.Split(Sep);
    7.  
    8.             Input.Inlines.Clear();
    9.            
    10.             for (int i = 0; i < Words.Length; i++)
    11.             {
    12.                 if (Words[i].StartsWith("http://") == true ||
    13.                     Words[i].StartsWith("www.") == true ||
    14.                     Words[i].StartsWith("@") == true)
    15.                 {
    16.                     #region Http
    17.                     if (Words[i].StartsWith("http://"))
    18.                     {
    19.                         if (i != 0)
    20.                         {
    21.                             //Add Space
    22.                             Input.Inlines.Add(new Run(" "));
    23.                         }
    24.                         Hyperlink m = new Hyperlink(new Run(Words[i]));
    25.                         m.NavigateUri = new Uri(Words[i]);
    26.                         m.Click += new RoutedEventHandler(m_Click);
    27.                         Input.Inlines.Add(m);
    28.                     }
    29.                     #endregion
    30.                     #region www
    31.                     if (Words[i].StartsWith("www."))
    32.                     {
    33.                         if (i != 0)
    34.                         {
    35.                             //Add Space
    36.                             Input.Inlines.Add(new Run(" "));
    37.                         }
    38.                         Hyperlink m = new Hyperlink(new Run("http://" + Words[i]));
    39.                         m.NavigateUri = new Uri("http://" + Words[i]);
    40.                         m.Click += new RoutedEventHandler(m_Click);
    41.                         Input.Inlines.Add(m);
    42.                     }
    43.                     #endregion
    44.                     #region @
    45.                     if (Words[i].StartsWith("@"))
    46.                     {
    47.                         if (i != 0)
    48.                         {
    49.                             //Add Space
    50.                             Input.Inlines.Add(new Run(" "));
    51.                         }
    52.                         Hyperlink m = new Hyperlink(new Run(Words[i]));
    53.                         m.NavigateUri = new Uri("http://www.twitter.com/" + Words[i]);
    54.                         m.Click += new RoutedEventHandler(m_Click);
    55.                         Input.Inlines.Add(m);
    56.                     }
    57.                     #endregion
    58.                 }
    59.                 else
    60.                 {
    61.                     if (i != 0)
    62.                     {
    63.                         //Add Space
    64.                         Input.Inlines.Add(new Run(" "));
    65.                     }
    66.                     Input.Inlines.Add(new Run(Words[i]));
    67.                 }
    68.             }
    69.             return Input;
    70.         }

    What it doest it detects any given link, or twitter username refs in a source string.

    This code is not polished and could have significant improvements.

    What you have to do is whenever you set the text for your Richtextbox you use this method to insert the in-lines of the Richtextbox .
    C# and WPF developer
    My Website:
    http://singlebits.com/

  8. #8
    Member
    Join Date
    Jul 2010
    Posts
    63

    Re: AutoDetecting Link in WPF

    Wait,
    This function adding HYPERLINK on every link in paragraph...
    But can you explain me how to do that function everytime when Richtextbox is updated... I know i am using TextChanged event, but i am a bit confused in this WPF how to read complete Richtextbox and than back text edited with this function.
    Thanks so much

  9. #9

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: AutoDetecting Link in WPF

    http://msdn.microsoft.com/en-us/libr...wdocument.aspx

    There is all the documentation needed to get started with managin WPF Flowdocuments, which are what richboxtextbox controls use.

    Good Luck
    C# and WPF developer
    My Website:
    http://singlebits.com/

  10. #10

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: AutoDetecting Link in WPF

    I forgot to say this,

    I imagine you are inserting each conversation line in a richtextbox, if that's the case you would have to use binding, but you cannon't bind directly to a richtextbox.

    For this I remember doing something about it, but I lost the files on which I had an example, try looking at this:

    http://www.fortes.com/2007/bindablerun

    http://lmgtfy.com/?q=wpf+bind+flowdocument
    C# and WPF developer
    My Website:
    http://singlebits.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width