I want to make a clickable text through code in c# (its not a URL just ordinary text) so when i click that text it do linkclicked event.
It is for a dictionary so when I search something and in the description there is a word same from the storage I can click it to search that word.
for (int j = 0; j <= jml[i]; j++)
{
richTextBox4.AppendText(j + 1 + ". ");
string[] desk = sk[i, j].Split(' ');
for (int l = 0; l < desk.Count(); l++)
{
for (int m = 0; m < kata.Count(); m++)
{
if (desk[l] == kata[m])
{
richTextBox4.SelectionColor = Color.Blue;
desk[l] = LinkArea;
}
}
richTextBox4.AppendText(desk[l] + " ");
richTextBox4.SelectionColor = Color.Black;
}
richTextBox4.AppendText("\n");
}
my code for the moment(for the hyperlink type text just the color that work)
There is no built-in way but if you know what you want it is not hard to build.
Step one is to decide just how the clickable text is defined, read: What are the separators?
If you want single words it is rather easy. One way is using a regEx to identify words in a RTB. If you don't like the built-in way to define a word, I can post a code with user-defined separators.. But maybe you are happy with what have already.
If you need whole phrases (including spaces) to be clickable as one whole entity then you will have to be a little more inventive:
either you can add special characters around the phrases
or you can replace the spaces by non-breakable spaces
Or you could write a complete map into your text, but that probably defies your purpose.
Anything but simple words will mean that you will need to apply some changes to your text.
The result should be a function that can find the start and the end of the clickable text and return the clicked word/phrase.
Step Two is to look up the text in a Dictionary<string, string> and do what you want to do with the value, e.g. show the translation in a Label:
if (dictEnGE.ContainsKey(theClickedWord))
label.Text = dictEnGE[theClickedWord];
For this to work you must first prepare the dictionary by adding the key-value pairs: The keys are the clickable texts and the values are whatever you need to find:
Dictionary<string, string> dictEnGE = new Dictionary<string, string>();
dictEnGE.Add("house", "Haus");
dictEnGE.Add("man", "Mann");
dictEnGE.Add("mouse", "Maus");
//..
So the whole solution is: Prepare a dictionary, code the Mouseclick event to find the clicked text, look up the text in the dictioanry, do your thing with the value you find..
Update: Here is a function to get the clicked word:
string getWordAtIndex(RichTextBox RTB, int index)
{
string wordSeparators = " .,;-!?\r\n\"";
int cp0 = index;
int cp2 = RTB.Find(wordSeparators.ToCharArray(), index);
for (int c = index; c > 0; c--)
{ if (wordSeparators.Contains(RTB.Text[c])) { cp0 = c + 1; break; } }
int l = cp2 - cp0;
if (l > 0) return RTB.Text.Substring(cp0, l); else return "";
}
and here is how you could use it:
private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
string word = getWordAtIndex(richTextBox1, richTextBox1.SelectionStart);
if (dictEnGE.ContainsKey(word)) aLabel.Text = dictEnGE[word];
}
Related
I am trying to set the position of caret in richtextbox based on index position of a word. Even though I am able to change the caret position, the caret does not move to the correct location.
Here is my sample code:
private void Button_Click(object sender, RoutedEventArgs e)
{
RTB_Main.Document.Blocks.Clear();
for (int i = 0; i < 10; i++)
{
Paragraph para = new Paragraph(new Run(i + ""));
RTB_Main.Document.Blocks.Add(para);
}
TextRange richText = new TextRange(RTB_Main.Document.ContentStart, RTB_Main.Document.ContentEnd);
string searchText = tb_Search.Text; // 1 to 9
int position = Regex.Match(richText.Text, searchText).Index;
RTB_Main.CaretPosition = RTB_Main.Document.ContentStart;
RTB_Main.CaretPosition = RTB_Main.CaretPosition.GetPositionAtOffset(position);
RTB_Main.Focus();
}
What is wrong with this approach?
Also, Please let me know if there is a better way to set the caret position to an index?
The problem in my case was caused by new line characters \r\n. I just replaced these with another characters and it worked for me. Note that I am replacing them with not 2 characters but 4.
private void Button_Click(object sender, RoutedEventArgs e)
{
RTB_Main.Document.Blocks.Clear();
for (int i = 0; i < 10; i++)
{
Paragraph para = new Paragraph(new Run(i + ""));
RTB_Main.Document.Blocks.Add(para);
}
TextRange richText = new TextRange(RTB_Main.Document.ContentStart, RTB_Main.Document.ContentEnd);
string searchText = tb_Search.Text; // 1 to 9
string tmpStr = richText.Text.Replace("\r\n", "....");
int position = Regex.Match(tmpStr, searchText).Index;
RTB_Main.CaretPosition = RTB_Main.Document.ContentStart;
RTB_Main.CaretPosition = RTB_Main.CaretPosition.GetPositionAtOffset(position);
RTB_Main.Focus();
}
As Maciek noted, there are invisible formatting items that affects the count. My code adds a feedback loop because we are able to ask what the true caret position is. It feels hacky but I could not find anything better.
public static void SetCaretPositionOfRichTextBoxToCharIndex(
System.Windows.Controls.RichTextBox box, int charIndex)
{
// RichTextBox contains many formattings, and they, although invisible, count
// when setting CaretPosition. Calling GetPositionAtOffset with charIndex from
// DocumentStart can be less than the necessary CaretPosition. This code
// therefore has a feedback loop to see how much more offset is necessary.
box.CaretPosition = box.CaretPosition.DocumentStart;
int attemptedCharIndex = 0;
int fixerInc = 0;
while (attemptedCharIndex < charIndex)
{
box.CaretPosition = box.CaretPosition.GetPositionAtOffset(charIndex - attemptedCharIndex + fixerInc);
int temp = new TextRange(box.Document.ContentStart, box.CaretPosition).Text.Length;
if (attemptedCharIndex == temp)
{
fixerInc++;
}
else
{
fixerInc = 0
}
attemptedCharIndex = temp;
}
}
I have a problem while trying to replace all text matching a particular word in a rich text box. This is the code i use
public static void ReplaceAll(RichTextBox myRtb, string word, string replacer)
{
int index = 0;
while (index < myRtb.Text.LastIndexOf(word))
{
int location = myRtb.Find(word, index, RichTextBoxFinds.None);
myRtb.Select(location, word.Length);
myRtb.SelectedText = replacer;
index++;
}
MessageBox.Show(index.ToString());
}
private void btnReplaceAll_Click(object sender, EventArgs e)
{
Form1 text = (Form1)Application.OpenForms["Form1"];
ReplaceAll(text.Current, txtFind2.Text, txtReplace.Text);
}
This works well but i have noticed a little malfunction when i try to replace a letter with itself and another letter.
For example i want to replace all the e in Welcome to Nigeria with ea.
This is what i get Weaalcomeaaaaaaa to Nigeaaaaaaaaaaaaaaria.
And the message box shows 23 when there are only three e. Pls what am i doing wrong and how can i correct it
Simply do this:
yourRichTextBox.Text = yourRichTextBox.Text.Replace("e","ea");
If you want to report the number of matches (which are replaced), you can try using Regex like this:
MessageBox.Show(Regex.Matches(yourRichTextBox.Text, "e").Count.ToString());
UPDATE
Of course, using the method above has expensive cost in memory, you can use some loop in combination with Regex to achieve some kind of advanced replacing engine like this:
public void ReplaceAll(RichTextBox myRtb, string word, string replacement){
int i = 0;
int n = 0;
int a = replacement.Length - word.Length;
foreach(Match m in Regex.Matches(myRtb.Text, word)){
myRtb.Select(m.Index + i, word.Length);
i += a;
myRtb.SelectedText = replacement;
n++;
}
MessageBox.Show("Replaced " + n + " matches!");
}
im trying to make some kind of search function in my program which is just highlighting all keywords found on textbox
the keyword box is on t2.text and the text is from bx2.text
firstly i tried out this method from here Highlight key words in a given search text
the regex is working but the highlight not
any wrong here?
private void searchs()
{
var spattern = t2.Text;
foreach(var s in bx2.Text)
{
var zxc = s.ToString();
if (System.Text.RegularExpressions.Regex.IsMatch(zxc, spattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
bx2.Text.Replace(bx2.Text, #"<span class=""search-highlight"">$0</span>");
}
}
}
The linked question uses HTML formatting, which you can't use in a regular textbox.
You need either a RichText box. In there you can format text using the control's methods and properties (not using HTML).
Or use a browser control rather than a textbox to use the HTML formatting.
private void findButton_Click(object sender, EventArgs e)
{
int count = 0;
string keyword = keywordTextbox.Text.Trim();//getting given searching string
int startPosition = 0; //initializing starting position to search
int endPosition = 0;
int endArticle = articleRichTextbox.Text.Length;//finding total number of character into the article
for (int i = 0; i<endArticle ; i =startPosition )//creating a loop until ending the article
{
if (i == -1) //if the loop goes to end of the article then stop
{
break;
}
startPosition = articleRichTextbox.Find(keyword, startPosition, endArticle, RichTextBoxFinds.WholeWord);//using find method get the begining position when find the searching string
if (startPosition >= 0) //if match the string //if don't match the string then it return -1
{
count++;//conunting the number of occuerence or match the search string
articleRichTextbox.SelectionColor = Color.Blue;//coloring the matching string in the article
endPosition = keywordTextbox.Text.Length;
startPosition = startPosition + endPosition;//place the starting position at the next word of previously matching string to continue searching.
}
}
if (count == 0)//if the givn search string don't match at any time
{
MessageBox.Show("No Match Found!!!");
}
numberofaccurTextbox.Text = count.ToString();//show the number of occurence into the text box
}
I am working with windows form. In my project i need to color the last word of rich text box. When someone write on the given text box of the application i need the last word that is just written on the richtextbox to be colored red or whatever.
I found a way to extract the last word from the following link
http://msdn.microsoft.com/en-us/library/system.windows.documents.textselection.select%28v=vs.95%29.aspx
But i need more handy code to extract the last word if its possible. Please help.
Well, if you really are just looking to get the last word, you could do something like this...Assuming of course, that you make a string equal to the text of your rich text box.
string str="hello, how are you doing?";
if (str.Length >0)
{
int index=str.LastIndexOf(" ") + 1;
str = str.Substring(index));
}
Then just return the string, and do what you need to do with it.
Here is the sample code
*> char[] arr = new char[50];
int i = 0;
private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space) {
string str = new string(arr);
MessageBox.Show(str);
Array.Clear(arr, 0, arr.Length);
i = 0;
}
else if (e.KeyCode == Keys.Back)
{
i--;
if (i < 0)
{
i = 0;
}
arr[i] = ' ';
}
else
{
arr[i] = (char)e.KeyValue;
i++;
}
}*
This is how you will be able to extract the latest word. Now color yourself the word you like.
As I remember rich text box can render text as HTML.
Just wrap your last word in font tag and that's it.
So, I have a listbox with x number of items. On top of the listbox I have a TextBox (this is the search field). I try do develop an algorithm that removes items from the listbox, if it doesn't contain the searchword (variable keyword in the code). This is supposed to happen for each key the user types (on-the-fly). So, the code:
private void _keywordTextBox_TextChanged(object sender, EventArgs e)
{
string keyword = _keywordTextBox.Text;
if (keyword == searchtext || isSpace) // do nothing if space is typed - searchtext is a templatetext in the textbox ("type here to search...")
return; // ignore
else if (keyword == "")
{
listBox.Items.Clear();
foreach (string s in originalList)
listBox.Items.Add(s);
}
else
{
List<string> selection = new List<string>();
foreach (string s in originalList) // originalList is the listbox at startup
selection.Add(s);
listBox.BeginUpdate();
string[] keywordSplit = keyword.Split(' ');
try
{
for (int i = originalList.Count - 1; i >= 0; i--)
{
string[] selectionSplit = selection[i].Split(' ');
int l = 0; // number of hits
for (int j = 0; j < selectionSplit.Length; j++)
{
for (int k = 0; k < keywordSplit.Length; k++)
{
if (selectionSplit[j].ToLower().Contains(keywordSplit[k].ToLower()))
{
l++;
break;
}
}
}
if (l < keywordSplit.Length) // Not hit on all keywords
selection.RemoveAt(i);
}
}
finally
{
listBox.Items.Clear();
foreach (string s in selection) // Add selection in listbox
listBox.Items.Add(s);
if (listBox.Items.Count > 0)
listBox.SetSelected(0, true); // Select first item in listbox
listBox.EndUpdate();
}
}
}
The problem is hard to describe, other than it doesn't work as intended. The behavour is, as far as I can see, sporadic.
If I search for "ck flow", I should get a hit for stackoverflow. More importantly, it should also work if I deletes chars (delete key of backspace). Anybody?
Edit: more details:
The listbox should shrink and grow on each keystroke, based on what the user searches for. The listbox should keep every item that matches the keyword typed in by the user, and filter away that doesn't match.
Or you could try to work out a Regular Expression:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string keyword = textBox1.Text;
if (string.IsNullOrEmpty(keyword.Trim()))
{
listBox1.Items.Clear();
listBox1.Items.AddRange(_originalList.ToArray());
}
else
{
Regex regex = new Regex(GetRegexPatternFromKeyword(keyword));
List<string> selection =
_originalList.Where(s => regex.IsMatch(s)).ToList();
listBox1.Items.Clear();
listBox1.Items.AddRange(selection.ToArray());
}
}
private static string GetRegexPatternFromKeyword(string keyword)
{
string[] words =
keyword.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(word => "(?=.*" + word.Replace(")", #"\)") + ")").ToArray();
return string.Join("", words);
}
disclaimer: there could be some cases where an input would 'destroy' the regex pattern
Your code increases l too often. For instance;
the text 'aaa aaa aaa' with searchword 'aaa bbb' will give an l of 3 because every time you find 'aaa' you increase l. So this will be a match even though 'bbb' is never found.
You can fix this (among others) by deleting found parts of keywordsplit and recreating keywordsplit anew before every search of a new selectionline.
l++;
break;
becomes
l++
keywordSplit.RemoveAt[k];
break;
and move
string[] keywordSplit = keyword.Split(' ');
to just before you start the k loop
Altough I feel there might be better ways to achieve what you want with a bit cleaner and faster code it should work.
I got it to work. #IvoTops helped me in the right direction. Just loop trough all the keyword first, and then the selections.
for (int j = 0; j < keywordSplit.Length; j++)
{
for (int k = 0; k < selectionSplit.Length; k++)
{
if (selectionSplit[k].ToLower().Contains(keywordSplit[j].ToLower()))
{
l++;
break;
}
}
}
Seems to work ok now.