Link to file path with space in Richtextbox

倾然丶 夕夏残阳落幕 提交于 2019-12-23 04:47:43

问题


I am using richtextbox in winforms. I have to show some links in the richtextbox which will be set readonly. It works fine for the links with out spaces like

\\efile\DSC_0618.JPG

But when the file has space like

\\2527 threshold.png

it wont work and link will get broke due to space.

i have used the solution mentioned in this link Link to File's path with spaces in RichTextBox?

But the problem is that escape sequence also get displayed in the Richtextbox according to that.

Is there any way i can make it as link without using escape sequence?


回答1:


I've found this link http://www.codeproject.com/cs/miscctrl/RichTextBoxLinks.asp which can help you insert a link of any text into a RichTextBox. There is a special note about how to fetch the LinkText and Link Url here which doesn't seem to be found in the original demo. Here I'll demonstrate the link info fetching in LinkClicked event handler:

//Insert link to test
richTextBoxEx1.InsertLink("StackOverFlow", "http://www.stackoverflow.com");
//LickClicked event handler
private void richTextBoxEx1_LinkClicked(object sender, System.Windows.Forms.LinkClickedEventArgs e)
{
        string[] s = e.LinkText.Split(new string[]{@"#http://"}, StringSplitOptions.None);
        if (s.Length == 2)
        {
            s[1] = "http://" + s[1];
            MessageBox.Show("A link has been clicked.\nThe link text is '" + s[0] + "'\nThe link URL is '" + s[1] + "'");
            System.Diagnostics.Process.Start(s[1]);//Try visiting the link.
        }
}

I think this is the most beautiful solution for you (and others who have the same problem).




回答2:


If you'd like something basic, which has the effect of visually mimicking spaces in file names, simply replace the space characters (' ') with the UNICODE non-breaking space (U+00A0), and then in the link click handler do the reverse. See my answer to a similar question here for the sample code:

https://stackoverflow.com/a/19853473/2967903



来源:https://stackoverflow.com/questions/17719021/link-to-file-path-with-space-in-richtextbox

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