-
Jul 24th, 2009, 07:39 PM
#1
Thread Starter
Hyperactive Member
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.
-
Dec 26th, 2010, 01:06 PM
#2
Member
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
-
Dec 26th, 2010, 03:34 PM
#3
Thread Starter
Hyperactive Member
Re: AutoDetecting Link in WPF
Originally Posted by stefanACM
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..
-
Dec 26th, 2010, 06:24 PM
#4
Member
Re: AutoDetecting Link in WPF
Ok please
Thanks
-
Dec 26th, 2010, 08:28 PM
#5
Thread Starter
Hyperactive Member
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 ?
-
Dec 27th, 2010, 09:49 AM
#6
Member
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 :|
-
Dec 27th, 2010, 10:21 AM
#7
Thread Starter
Hyperactive Member
Re: AutoDetecting Link in WPF
Take a look at this code:
csharp Code:
private Paragraph ParseLinks (string Source)
{
Paragraph Input = new Paragraph(new Run(Source));
char[] Sep = new char[] { ' ' };
string[] Words = new TextRange(Input.ContentStart, Input.ContentEnd).Text.Split(Sep);
Input.Inlines.Clear();
for (int i = 0; i < Words.Length; i++)
{
if (Words[i].StartsWith("http://") == true ||
Words[i].StartsWith("www.") == true ||
Words[i].StartsWith("@") == true)
{
#region Http
if (Words[i].StartsWith("http://"))
{
if (i != 0)
{
//Add Space
Input.Inlines.Add(new Run(" "));
}
Hyperlink m = new Hyperlink(new Run(Words[i]));
m.NavigateUri = new Uri(Words[i]);
m.Click += new RoutedEventHandler(m_Click);
Input.Inlines.Add(m);
}
#endregion
#region www
if (Words[i].StartsWith("www."))
{
if (i != 0)
{
//Add Space
Input.Inlines.Add(new Run(" "));
}
Hyperlink m = new Hyperlink(new Run("http://" + Words[i]));
m.NavigateUri = new Uri("http://" + Words[i]);
m.Click += new RoutedEventHandler(m_Click);
Input.Inlines.Add(m);
}
#endregion
#region @
if (Words[i].StartsWith("@"))
{
if (i != 0)
{
//Add Space
Input.Inlines.Add(new Run(" "));
}
Hyperlink m = new Hyperlink(new Run(Words[i]));
m.NavigateUri = new Uri("http://www.twitter.com/" + Words[i]);
m.Click += new RoutedEventHandler(m_Click);
Input.Inlines.Add(m);
}
#endregion
}
else
{
if (i != 0)
{
//Add Space
Input.Inlines.Add(new Run(" "));
}
Input.Inlines.Add(new Run(Words[i]));
}
}
return Input;
}
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 .
-
Dec 27th, 2010, 12:00 PM
#8
Member
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
-
Dec 27th, 2010, 02:35 PM
#9
Thread Starter
Hyperactive Member
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
-
Dec 27th, 2010, 02:40 PM
#10
Thread Starter
Hyperactive Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|