I have a RichTextBox with a search box under it. I use the following code for the search functionality:
TabPage activePage = tabs.SelectedTab;
RichTextBox xmlBox = activePage.Controls.Find("xmlBox", true).Single() as RichTextBox;
xmlBox.DeselectAll();
int index = 0;
int len = xmlBox.TextLength;
int lastIndex = xmlBox.Text.LastIndexOf(tbSearch.Text);
while (index < lastIndex)
{
xmlBox.Find(tbSearch.Text, index, len, RichTextBoxFinds.WholeWord);
xmlBox.SelectionBackColor = Color.Yellow;
index = xmlBox.Text.IndexOf(tbSearch.Text, index) + 1;
}
What I want is that lets say a user types the word User. When he types the U I want all the Us to be highlighted, etc. and then if he deletes the r I want only Use to be highlighted. I was thinking that DeselectAll() would do the trick, but this isn`t working. Any other way to do this?
DeselectAll() will simply unselect any current selection. Your code actually changed the BackColor() of previous text so you'd have to undo this...possibly by selecting everything and resetting it to the default color before again highlighting the new search value.
Related
I have a TabControl in my C# form. According to the program step I have to remove a range of the tabs programmaticaly. I do that currently in this way, but with changing conditions I have to remove the tabpages always one by one.
tabControl1.TabPages.Remove(tabPage5);
tabControl1.TabPages.Remove(tabPage6);
tabControl1.TabPages.Remove(tabPage7);
// remove other tabs
I want to be able to remove the tabs in a shorter way, in a loop something like that: Here Instead of the ??? I want to write the tabpage(n) indirectly with a variable. Is that possible?
for(int n=start; n<=stop; n++)
{
tabControl1.TabPages.Remove(???)
}
Is it not possible to define the "tabpagex" as a control and define
the control name dynamicaly?
Sure, you can SEARCH for controls "by name" using Controls.Find(name, true):
int start = 5;
int stop = 8;
for (int n = start; n <= stop; n++)
{
TabPage tb = this.Controls.Find("tabPage" + n, true).FirstOrDefault() as TabPage;
if (tb != null)
{
tabControl1.Controls.Remove(tb);
}
}
I have a variable, lets say it = 5, and then I would like the line number 5 to be highlighted "blue" in my RichTextBox1. is that possible at all?
Or should I use something like a ListBox, DataGridView etc.
This will highlight the text in a given line in a RichTextBox if WordWrap is off:
void highLightALine(RichTextBox rtb, int line, Color hiLight)
{
int i1 = rtb.GetFirstCharIndexFromLine(line);
int i2 = rtb.GetFirstCharIndexFromLine(line + 1);
if (i2 < 0) i2 = rtb.Text.Length;
rtb.SelectionStart = i1;
rtb.SelectionLength = i2 - i1;
rtb.SelectionBackColor = hiLight;
}
Note that if WordWrap is true it will still highlight the line but only as far as it is visible. Its continuation on the next line will not be changed.
Also note that only Text can be highlighted. Trailing empty space can't be highlighted afaik. Here is an example of trying to owner-draw an RTB subclass..
Is there any way to get the index of the first item currently showing, when the list is scrollable?
I'm making a CharMap with some extensions and just found that ListView can't contain 64k items (see code below)
for (var i = char.MinValue; i < char.MaxValue; i++)
{
var c = Convert.ToChar(i);
if (!char.IsControl(c))
lv1.Items.Add(""+c);
}
so decided to load chars when scroll is at some appropriate points (ie first/last 15%) but ListView doesn't give absolute position of the scrollbar.
It does feel a little hackish, but maybe it will do the job:
int getFirstVisibleItem(ListView lv)
{
ListViewHitTestInfo HI;
for (int i = 0; i < Math.Min(lv.ClientSize.Width, lv.ClientSize.Height); i += 3)
{
HI = lv.HitTest(i, i);
if (HI.Item != null) return HI.Item.Index;
}
return -1;
}
This does not directly help with your scrolling issue but should find the first visible Item as you have asked. If your Items have extremely weird (ie non-square) shapes you may want to change the travesal code a little..
For your requirement implement ListView with custom scrollbar. So you have more control over your scroll position. You decide when to what action based on scroll position. This might be helpful Code
I found a good solution for search and highlight text in RichTextbox LINK
And this solution works fine, but I found a very nasty bug, when searching for the last character in any text, such as "Hello World" and if you try to type a letter "d" in search field, program will glitch in endless cycle in here
while ((index = this.Find(findWhat, startSearch, findoptions)) > -1)
{
isfind = true;
this.SelectionBackColor = highlightColor;
startSearch = index + 1;
}
How to fix this bug? Or maybe help me find another solution how to find and highlight text in RichTextBox.
Yes, it is buggy. You have to add an extra check to ensure it doesn't start the search beyond the end of the text. Like this:
int max = this.TextLength;
while (startSearch < max &&
(index = this.Find(findWhat, startSearch, findoptions)) > -1) {
isFind = true;
this.SelectionBackColor = highlightColor;
startSearch = index + 1;
}
I am trying to select lines of text in a ICSharpCode TextEditor. As well as make the textbox go to the specific row. The application is windows form app built in VS 2010 in C#.
The reason I am using the text editor is for the code highlighting and line numbers etc.
I dont really have too much experience using windows forms so any help would be appreciated. The Code I have is as follows:
textEditorControl.Text = "long file string with line breaks";
textEditorControl.VRulerRow = 10; //Example row selection
Here is an example of how to select text with the Text Editor included with SharpDevelop 3.2:
// Two lines of text.
textEditorControl.Text =
"First\r\n" +
"Second\r\n";
// Start of selection - columns and lines are zero based.
int startCol = 0;
int startLine = 1;
TextLocation start = new TextLocation(startCol, startLine);
// End of selection.
int endCol = 6;
int endLine = 1;
TextLocation end = new TextLocation(endCol, endLine);
// Select the second line.
textEditorControl.ActiveTextAreaControl.SelectionManager.SetSelection(start, end);
// Move cursor to end of selection.
textEditorControl.ActiveTextAreaControl.Caret.Position = end;
I am assuming that by "make the textbox go to the specific row" you mean move the cursor to that row. The last line of code in the example above shows you how to do that.