How to detect URLs written or pasted in a RichTextBox in WPF application

血红的双手。 提交于 2019-12-11 08:34:00

问题


Sometimes a user will type a website URL while chatting in a Chat Application. I need to make the RichTextBox detect these URLs automatically, but I do not know how. General TextBox does not detect URLs, so i have used RichTextBox instead. I know RichTextBoxes can detect URLs but I don't know how. Can anybody suggest how I might do this?


回答1:


As schoola pointed out, the WPF RichTextBox does not auto-detect URLs. However this article describes a fairly simple (150 line) implementation of a custom RichTextBox that can be used for this very purpose.




回答2:


The WPF RichTextBox does not support auto-detection of URLs, but the Windows Forms Rich Text Box does. You might use a WindowsFormsHost element in your WPF application like this:

[...]
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 
[...]

<WindowsFormsHost >
    <wf:RichTextBox DetectUrls="True" />
</WindowsFormsHost>



回答3:


Have a look at these articles -

Auto-detecting Hyperlinks in RichTextBox - Part I

Auto-detecting Hyperlinks in RichTextBox - Part II



来源:https://stackoverflow.com/questions/9124429/how-to-detect-urls-written-or-pasted-in-a-richtextbox-in-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!