Hi i want create simple editor with richtextbox in C# windows form. in my project users should can add hyperlink for file in the richtextbox. but I have 2 problem that one of the is very weird and strange.
1. my user can type both ansi and utf-8 character, but when the user add hyper link in link text is in utf8, text turn to something like this "????"
2. when user add hyperlink, When the mouse goes over the hyperlink, the pointer turns into a hand; But when we click, the link click event does not firing and worked. the weird point is: when the user click on enter, go to the next line and type at least the same length of the above line. After this, by clicking on the hyperlink, the event will be executed and the file will be opened.
what Couse this problems and how to solve them?
What I have tried:
my code for add hyperlink button is:
private void AddLinkBtn_Click(object sender, EventArgs e)
{
string url = null;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
url = openFileDialog.FileName;
}
if (!string.IsNullOrEmpty(url))
{
string linkText = TextBoxDefinition.SelectedText;
byte[] bytes = Encoding.UTF8.GetBytes(linkText);
linkText = Encoding.UTF8.GetString(bytes);
string hyperlinkRtf = @"{\rtf1\ansi{\field{\*\fldinst HYPERLINK ""file://" + url + @"""}{\fldrslt " + linkText + @"}}}";
TextBoxDefinition.SelectedRtf = hyperlinkRtf;
}
}
and the link click event is :
private void TextBoxDefinition_LinkClicked(object sender, LinkClickedEventArgs e)
{
if(e.LinkText.StartsWith("file://"))
{
string filePath = e.LinkText.Substring(7);
Process.Start(filePath);
}
else
{
Process.Start(e.LinkText);
}
}