Updating a RichTextBox with many UI-access operations - c#

I have a sub-classed RichTextBox that I use to display chat information scrubbed from a game. Each chat line is saved into a custom object that stores the text as well as some meta-data. The RTB then uses this meta data to do some formatting. Here's the relevant code section:
public class ChatLine
{ //stub-class with code irrelevant to the question removed
public string Text { get; set; };
public Color Color { get; set; };
public DateTime Timestamp { get; set; };
public string Name { get; set; }; //name of the player where this text originated
}
public partial class CustomRichTextbox : RichTextBox
{
private readonly Object syncRoot = new();
private readonly List<ChatLine> displayedChatLines = new();
internal IReadOnlyCollection<ChatLine> DisplayedChatLines
{
get
{
return displayedChatLines.AsReadOnly();
}
}
public CustomRichTextbox()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
/// <summary>
/// Appends text to the current text of the textbox, ends the line, and returns the first line position that was appended.
/// </summary>
/// <remarks>
/// The second parameter should be supplied if you want to enable name display per line, and supplying the name you don't want displayed.
/// </remarks>
internal int AppendLines(IEnumerable<ChatLine> lines, string name = "")
{
//when the RTB is empty line count will be 0, which is what we should return
//when the RTB is not empty then we need to return line count - 1
//the Math.Max call ensures we take care of both cases
var pos = Math.Max(0, Lines.Length - 1);
lock (syncRoot)
{
base.AppendText(String.Join(String.Empty, lines.Select(FormatLine)));
displayedChatLines.AddRange(lines);
}
//paint the lines after we append (THIS LOOP IS TIME CONSUMING)
for (int i = lines.Count() - 1; i >= 0; i--)
{
var index = pos + i;
var line = displayedChatLines[index];
var start = this.GetFirstCharIndexFromLine(index);
var timestampEnd = this.Find(">", start, RichTextBoxFinds.None);
var textStart = timestampEnd + 2;
var lineLength = this.Lines[index].Length;
//color in timestamp
this.Select(start, timestampEnd - start + 1);
this.SelectionColor = this.ForeColor;
this.SelectionBackColor = this.BackColor;
this.SelectionFont = this.Font;
this.SelectionAlignment = HorizontalAlignment.Left;
if (String.IsNullOrEmpty(name) || name == line.Name)
{ //line doesn't contain name
this.Select(textStart, start + lineLength - textStart + 1);
this.SelectionColor = line.Color;
this.SelectionBackColor = (line.Color == Color.White) ? Color.DarkGray : this.BackColor;
}
else
{ //line contains name
var nameStart = this.Find($" {line.Name}", start, start + lineLength, RichTextBoxFinds.Reverse);
this.Select(textStart, nameStart - textStart);
this.SelectionColor = line.Color;
this.SelectionBackColor = (line.Color == Color.White) ? Color.DarkGray : this.BackColor;
this.Select(nameStart, start + lineLength - nameStart + 1);
this.SelectionColor = Color.DarkGray;
this.SelectionBackColor = this.BackColor;
this.SelectionFont = new Font(this.Font.FontFamily, 8);
this.SelectionAlignment = HorizontalAlignment.Right; //this actually causes the entire line to be right aligned
}
}
this.DeselectAll(); //necessary otherwise restoring scroll position won't work
return pos;
string FormatLine(ChatLine line)
{
return (String.IsNullOrEmpty(name) || name == line.Name) ?
$"<{line.Timestamp: hh:mm:ss tt}> {line.Text}{Environment.NewLine}" :
$"<{line.Timestamp: hh:mm:ss tt}> {line.Text} {line.Name}{Environment.NewLine}";
}
}
}
This works fine...for updates that have few lines. When it needs to update more (say 1000) the UI thread chokes up for a few seconds to do all the painting. I've identified the time-consuming code (noted in the code above) because when I comment that loop out then there's no problem.
Normally I'd just move the time-consuming code to a backgroundworker or use an async/await pattern, but the problem is all the code that's in the time consuming section requires UI-thread access! What do I do when I can't push the time-consuming stuff to another thread?

Related

WebForms RichTextBox Editing Approch?

I am using C# WinForm, and I have a RichTextBox that I am trying to make look like a C# script.
Means when using specific words, I want them to be colored. When they edit the word by changing it, I want it to go back to be black.
My approach works, but it really messy and cause bugs when the a scroll option is created and needed to be used to see the code below. (When typing, pretty much the richtextbox jumps up and down without stop)
private void ScriptRichTextBox_TextChanged(object sender, EventArgs e)
{
ScriptTextChange = ScriptRichTextBox.Text;
ScriptColorChange();
}
private void ScriptColorChange()
{
int index = ScriptRichTextBox.SelectionStart;
ScriptRichTextBox.Text = ScriptTextChange; //Only way I found to make the all current text black again, SelectAll() didn't work well.
ScriptRichTextBox.SelectionStart = index;
String[] coloredNames = {"Main", "ClickMouseDown", "ClickMouseUp", "PressKey", "StopMoving", "Delay", "GoRight", "GoLeft", "GoUp", "GoDown", "MousePosition", "LastHorizontalDirection", "LastVerticalDirections", "CurrentDirection", "Directions" };
String[] coloredNames2 = { "cm.", "if", "else", "while", "switch", "case", "break", "return", "new" };
String[] coloredNames3 = { "MyPosition", "MyHp", "MyMp", "OtherPeopleInMap", ".RIGHT", ".LEFT", ".UP", ".DOWN", ".STOP_MOVING" };
foreach (String s in coloredNames)
this.CheckKeyword(s, Color.LightSkyBlue, 0);
foreach (String s in coloredNames2)
this.CheckKeyword(s, Color.Blue, 0);
foreach (String s in coloredNames3)
this.CheckKeyword(s, Color.DarkGreen, 0);
}
private void CheckKeyword(string word, Color color, int startIndex)
{
if (this.ScriptRichTextBox.Text.Contains(word))
{
int index = 0;
int selectStart = this.ScriptRichTextBox.SelectionStart;
while ((index = this.ScriptRichTextBox.Text.IndexOf(word, (index + 1))) != -1)
{
this.ScriptRichTextBox.Select((index + startIndex), word.Length);
this.ScriptRichTextBox.SelectionColor = color;
this.ScriptRichTextBox.Select(selectStart, 0);
this.ScriptRichTextBox.SelectionColor = Color.Black;
}
}
}
I refactored your code a little to hopefully demonstrate a better approach to colouring the text. It is also not optimal to instantiate your string arrays every time you fire the TextChanged event.
Updated:The idea is to build up a word buffer that will be matched with your set of words when typing.
The buffer records each key and if it .IsLetterOrDigit it adds it to the StringBuilder buffer. The buffer has some additional bugs, with recording key press values and not removing recorded chars if you hit backspace etc..
Instead of the word buffer, use RegEx to match any of the words in your reserve word list. Build up the reserve word RegEx so you end up with something like \b(word|word2|word3....)\b This is done in the code in the BuildRegExPattern(..) method.
Once you hit any key other than a letter or number the buffer is checked for content and if the content matches a word then only the text right before the cursor in the ScriptRichTextBox.Text is checked and changed.
Remove the .(dots) from the reserve words as this just complicates the matching criteria. The RegEx in the built up patters will match the words exactly, so if you type something like FARRIGHT or cms the words will not partially change colour.
As an extra I also covered the paste process pressing Ctrl+V because it is a bit of a pain in WinForms and will probably happen quite often.
There are older questions eg. this one that cover the scrolling behaviour, where it shows how to interop by adding the [System.Runtime.InteropServices.DllImport("user32.dll")] attribute, but it can be done without it.
To prevent all the scroll jumping you can make use of the .DefWndProc(msg) method on the form. this question pointed me towards the WM_SETREDRAW property.
There is also this list of other properties that can be set.
The full implementation is this:
public partial class Form1 : Form
{
private readonly string[] _skyBlueStrings;
private readonly string[] _blueStrings;
private readonly string[] _greenStrings;
//for pasting
bool _IsCtrl;
bool _IsV;
//value to fix the colour not setting first character after return key pressed
int _returnIdxFix = 0;
//regex patterns to use
string _LightBlueRegX = "";
string _BlueRegX = "";
string _GreenRegX = "";
//match only words
Regex _rgxAnyWords = new Regex(#"(\w+)");
//colour setup
Color _LightBlueColour = Color.LightSkyBlue;
Color _BlueColour = Color.Blue;
Color _GreenColour = Color.DarkGreen;
Color _DefaultColour = Color.Black;
public Form1()
{
InitializeComponent();
_skyBlueStrings = new string[] { "Main", "ClickMouseDown", "ClickMouseUp", "PressKey", "StopMoving", "Delay", "GoRight", "GoLeft", "GoUp", "GoDown", "MousePosition", "LastHorizontalDirection", "LastVerticalDirections", "CurrentDirection", "Directions" };
_blueStrings = new string[] { "cm", "if", "else", "while", "switch", "case", "break", "return", "new" };
_greenStrings = new string[] { "MyPosition", "MyHp", "MyMp", "OtherPeopleInMap", "RIGHT", "LEFT", "UP", "DOWN", "STOP_MOVING" };
_LightBlueRegX = BuildRegExPattern(_skyBlueStrings);
_BlueRegX = BuildRegExPattern(_blueStrings);
_GreenRegX = BuildRegExPattern(_greenStrings);
}
string BuildRegExPattern(string[] keyworkArray)
{
StringBuilder _regExPatern = new StringBuilder();
_regExPatern.Append(#"\b(");//beginning of word
_regExPatern.Append(string.Join("|", keyworkArray));//all reserve words
_regExPatern.Append(#")\b");//end of word
return _regExPatern.ToString();
}
private void ProcessAllText()
{
BeginRtbUpdate();
FormatKeywords(_LightBlueRegX, _LightBlueColour);
FormatKeywords(_BlueRegX, _BlueColour);
FormatKeywords(_GreenRegX, _GreenColour);
//internal function to process words and set their colours
void FormatKeywords(string regExPattern, Color wordColour)
{
var matchStrings = Regex.Matches(ScriptRichTextBox.Text, regExPattern);
foreach (Match match in matchStrings)
{
FormatKeyword(keyword: match.Value, wordIndex: match.Index, wordColour: wordColour);
}
}
EndRtbUpdate();
ScriptRichTextBox.Select(ScriptRichTextBox.Text.Length, 0);
ScriptRichTextBox.Invalidate();
}
void ProcessWordAtIndex(string fullText, int cursorIdx)
{
MatchCollection anyWordMatches = _rgxAnyWords.Matches(fullText);
if (anyWordMatches.Count == 0)
{ return; } // no words found
var allWords = anyWordMatches.OfType<Match>().ToList();
//get the word just before cursor
var wordAtCursor = allWords.FirstOrDefault(w => (cursorIdx - _returnIdxFix) == (w.Index + w.Length));
if (wordAtCursor is null || string.IsNullOrWhiteSpace(wordAtCursor.Value))
{ return; }//no word at cursor or the match was blank
Color wordColour = CalculateWordColour(wordAtCursor.Value);
FormatKeyword(wordAtCursor.Value, wordAtCursor.Index, wordColour);
}
private Color CalculateWordColour(string word)
{
if (_skyBlueStrings.Contains(word))
{ return _LightBlueColour; }
if (_blueStrings.Contains(word))
{ return _BlueColour; }
if (_greenStrings.Contains(word))
{ return _GreenColour; }
return _DefaultColour;
}
private void FormatKeyword(string keyword, int wordIndex, Color wordColour)
{
ScriptRichTextBox.Select((wordIndex - _returnIdxFix), keyword.Length);
ScriptRichTextBox.SelectionColor = wordColour;
ScriptRichTextBox.Select(wordIndex + keyword.Length, 0);
ScriptRichTextBox.SelectionColor = _DefaultColour;
}
#region RichTextBox BeginUpdate and EndUpdate Methods
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
//wait until the rtb is visible, otherwise you get some weird behaviour.
if (ScriptRichTextBox.Visible && ScriptRichTextBox.IsHandleCreated)
{
if (m.LParam == ScriptRichTextBox.Handle)
{
rtBox_lParam = m.LParam;
rtBox_wParam = m.WParam;
}
}
}
IntPtr rtBox_wParam = IntPtr.Zero;
IntPtr rtBox_lParam = IntPtr.Zero;
const int WM_SETREDRAW = 0x0b;
const int EM_HIDESELECTION = 0x43f;
void BeginRtbUpdate()
{
Message msg_WM_SETREDRAW = Message.Create(ScriptRichTextBox.Handle, WM_SETREDRAW, (IntPtr)0, rtBox_lParam);
this.DefWndProc(ref msg_WM_SETREDRAW);
}
public void EndRtbUpdate()
{
Message msg_WM_SETREDRAW = Message.Create(ScriptRichTextBox.Handle, WM_SETREDRAW, rtBox_wParam, rtBox_lParam);
this.DefWndProc(ref msg_WM_SETREDRAW);
//redraw the RichTextBox
ScriptRichTextBox.Invalidate();
}
#endregion
private void ScriptRichTextBox_TextChanged(object sender, EventArgs e)
{
//only run all text if it was pasted NOT ON EVERY TEXT CHANGE!
if (_IsCtrl && _IsV)
{
_IsCtrl = false;
ProcessAllText();
}
}
protected void ScriptRichTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsLetterOrDigit(e.KeyChar))
{
//if the key was enter the cursor position is 1 position off
_returnIdxFix = (e.KeyChar == '\r') ? 1 : 0;
ProcessWordAtIndex(ScriptRichTextBox.Text, ScriptRichTextBox.SelectionStart);
}
}
private void ScriptRichTextBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.ControlKey)
{
_IsCtrl = true;
}
if (e.KeyCode == Keys.V)
{
_IsV = true;
}
}
private void ScriptRichTextBox_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.ControlKey)
{
_IsCtrl = false;
}
if (e.KeyCode == Keys.V)
{
_IsV = false;
}
}
}
It looks like this when you paste some "code" with keywords:
and typing looks like this:
Ok after 2 days of not finding something that actually works good or has annoying bugs. I managed to find a solution myself after a big struggle of trying to make it work. The big idea is people try to edit all the RichTextBox words at once, which cause bugs. Why to edit all of the rich text box when you can do your checks on the current word only to get the same result. Which is what I did, I checked if any of my array strings is in the current word, and colored all of them.
private void ScriptRichTextBox_TextChanged(object sender, EventArgs e)
{
FindStringsInCurrentWord();
}
private void FindStringsInCurrentWord()
{
RichTextBox script = ScriptRichTextBox;
String finalWord, forwards, backwards;
int saveLastSelectionStart = script.SelectionStart;
int index = script.SelectionStart;
String[] coloredNames = { "Main", "ClickMouseDown", "ClickMouseUp", "PressKey", "StopMoving", "Delay", "GoRight", "GoLeft", "GoUp", "GoDown", "MousePosition", "LastHorizontalDirection", "LastVerticalDirections", "CurrentDirection", "Directions" };
String[] coloredNames2 = { "cm.", "if", "else", "while", "switch", "case", "break", "return", "new" };
String[] coloredNames3 = { "MyPosition", "MyHp", "MyMp", "OtherPeopleInMap", ".RIGHT", ".LEFT", ".UP", ".DOWN", ".STOP_MOVING" };
String[] arr2 = coloredNames.Union(coloredNames2).ToArray();
Array arrAll = arr2.Union(coloredNames3).ToArray(); //Gets all arrays together
Array[] wordsArray = { coloredNames, coloredNames2, coloredNames3 }; //All found strings in the word
List<String> wordsFoundList = new List<String>();
int foundChangedColor = 0;
int wordsFound = 0;
char current = (char)script.GetCharFromPosition(script.GetPositionFromCharIndex(index)); //Where the editor thingy is
//Check forward text where he uses space and save text
while (!System.Char.IsWhiteSpace(current) && index < script.Text.Length)
{
index++;
current = (char)script.GetCharFromPosition(script.GetPositionFromCharIndex(index));
}
int lengthForward = index - saveLastSelectionStart;
script.Select(script.SelectionStart, lengthForward);
forwards = script.SelectedText;
//Debug.WriteLine("Forwards: " + forwards);
script.SelectionStart = saveLastSelectionStart;
this.ScriptRichTextBox.Select(script.SelectionStart, 0);
index = script.SelectionStart;
current = (char)script.GetCharFromPosition(script.GetPositionFromCharIndex(index));
int length = 0;
//Check backwords where he uses space and save text
while ((!System.Char.IsWhiteSpace(current) || length == 0) && index > 0 && index <= script.Text.Length)
{
index--;
length++;
current = (char)script.GetCharFromPosition(script.GetPositionFromCharIndex(index));
}
script.SelectionStart -= length;
script.Select(script.SelectionStart + 1, length - 1);
backwards = script.SelectedText;
//Debug.WriteLine("Backwards: " + backwards);
script.SelectionStart = saveLastSelectionStart;
this.ScriptRichTextBox.Select(saveLastSelectionStart, 0);
this.ScriptRichTextBox.SelectionColor = Color.Black;
finalWord = backwards + forwards; //Our all word!
//Debug.WriteLine("WORD:" + finalWord);
//Setting all of the word black, after it coloring the right places
script.Select(index + 1, length + lengthForward);
script.SelectionColor = Color.Black;
foreach (string word in arrAll)
{
if (finalWord.IndexOf(word) != -1)
{
wordsFound++;
wordsFoundList.Add(word);
script.Select(index + 1 + finalWord.IndexOf(word), word.Length);
if (coloredNames.Any(word.Contains))
{
script.SelectionColor = Color.LightSkyBlue;
foundChangedColor++;
}
else if (coloredNames2.Any(word.Contains))
{
script.SelectionColor = Color.Blue;
foundChangedColor++;
}
else if (coloredNames3.Any(word.Contains))
{
script.SelectionColor = Color.DarkGreen;
foundChangedColor++;
}
//Debug.WriteLine("Word to edit: " + script.SelectedText);
this.ScriptRichTextBox.Select(saveLastSelectionStart, 0);
this.ScriptRichTextBox.SelectionColor = Color.Black;
}
}
//No strings found, color it black
if (wordsFound == 0)
{
script.Select(index + 1, length + lengthForward);
script.SelectionColor = Color.Black;
//Debug.WriteLine("WORD??: " + script.SelectedText);
this.ScriptRichTextBox.Select(saveLastSelectionStart, 0);
this.ScriptRichTextBox.SelectionColor = Color.Black;
}
}

AvalonEdit show selection over other formatting

I'm currently using the following code as a LineTransformer with an AvalonEdit TextEditor. I want to be able to highlight the current single search result with a selection, however the selection is barely visible because the formatting of the DocumentColorizingTransformer has precedence over showing highlighted text. How do I get the highlighted selection to show instead of or before the formatting?
public class ColorizeSearchResults : DocumentColorizingTransformer {
public ColorizeSearchResults() : base() {
SearchTerm = "";
MatchCase = false;
}
public string SearchTerm { get; set; }
public bool MatchCase { get; set; }
protected override void ColorizeLine(DocumentLine line) {
if (SearchTerm.Length == 0)
return;
int lineStartOffset = line.Offset;
string text = CurrentContext.Document.GetText(line);
int count = 0;
int start = 0;
int index;
while ((index = text.IndexOf(SearchTerm, start, MatchCase ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase)) >= 0) {
base.ChangeLinePart(
lineStartOffset + index,
lineStartOffset + index + SearchTerm.Length,
(VisualLineElement element) => {
element.TextRunProperties.SetForegroundBrush(Brushes.White);
element.TextRunProperties.SetBackgroundBrush(Brushes.Magenta);
});
start = index + 1;
count++;
}
}
}
Example of formatting showing over selection
See http://avalonedit.net/documentation/html/c06e9832-9ef0-4d65-ac2e-11f7ce9c7774.htm for AvalonEdit render flow.
The selection layer is rendered before text. So if the text has background, it overrides the selection background. Fortunately we can set the background to Brush.Transparent (or a mix of the Selection.Brush and your own color).
Solution: I've modified the SelectionColorizer code to reset selection background to transparent:
class SelectionColorizerWithBackground : ColorizingTransformer
{
ICSharpCode.AvalonEdit.Editing.TextArea _textArea;
public SelectionColorizerWithBackground(
ICSharpCode.AvalonEdit.Editing.TextArea textArea)
{
if (textArea == null)
throw new ArgumentNullException("textArea");
this._textArea = textArea;
}
protected override void Colorize(ITextRunConstructionContext context)
{
int lineStartOffset = context.VisualLine.FirstDocumentLine.Offset;
int lineEndOffset = context.VisualLine.LastDocumentLine.Offset +
context.VisualLine.LastDocumentLine.TotalLength;
foreach (var segment in _textArea.Selection.Segments)
{
int segmentStart = segment.StartOffset;
if (segmentStart >= lineEndOffset)
continue;
int segmentEnd = segment.EndOffset;
if (segmentEnd <= lineStartOffset)
continue;
int startColumn;
if (segmentStart < lineStartOffset)
startColumn = 0;
else
startColumn = context.VisualLine.ValidateVisualColumn(
segment.StartOffset, segment.StartVisualColumn,
_textArea.Selection.EnableVirtualSpace);
int endColumn;
if (segmentEnd > lineEndOffset)
endColumn =
_textArea.Selection.EnableVirtualSpace
? int.MaxValue
: context.VisualLine
.VisualLengthWithEndOfLineMarker;
else
endColumn = context.VisualLine.ValidateVisualColumn(
segment.EndOffset, segment.EndVisualColumn,
_textArea.Selection.EnableVirtualSpace);
ChangeVisualElements(
startColumn, endColumn,
element => {
element.TextRunProperties.SetBackgroundBrush(
System.Windows.Media.Brushes.Transparent);
if (_textArea.SelectionForeground != null)
{
element.TextRunProperties.SetForegroundBrush(
_textArea.SelectionForeground);
}
});
}
}
}
To use the code you are supposed to do the following:
var lineTransformers = textEditor.TextArea.TextView.LineTransformers;
// Remove the original SelectionColorizer.
// Note: if you have syntax highlighting you need to do something else
// to avoid clearing other colorizers. If too complicated you can skip
// this step but to suffer a 2x performance penalty.
lineTransformers.Clear();
lineTransformers.Add(new ColorizeSearchResults());
lineTransformers.Add(
new SelectionColorizerWithBackground(textEditor.TextArea));
After I've tried my solutions extensively, I'd like to add a few points:
While my other solution above appears to work, you'll have some subpixel artefacts when the rectangles are supposed to be tiled. If that is unacceptable you can implement an IBackgroundRenderer. (That happens to be my chosen solution.) If you want some code you may request here, but I doubt whether it will be useful.
BTW, since your question is about search result, most likely you can use https://github.com/icsharpcode/AvalonEdit/blob/697ff0d38c95c9e5a536fbc05ae2307ec9ef2a63/ICSharpCode.AvalonEdit/Search/SearchResultBackgroundRenderer.cs unmodified (or modify it if you don't want the rounded borders).
You may use element.BackgroundBrush = Brushes.Magenta; instead of element.TextRunProperties.SetBackgroundBrush(Brushes.Magenta);. AvalonEdit appears to draw the background with a rectangle with 3px radius.
There is also a RichTextColorizer starting from AvalonEdit 5.01. I don't know how to use it though because it is not referenced in other files. And the (most likely unwanted) rounded rectangles in the previous paragraph are likely to be present.
So here's my final product based almost entirely off of the existing AvalonEdit SearchResultBackgroundRenderer.
This works a little different than my post's colorizer as you have to modify the search results manually instead of it doing it for you. But that may also save some computation time.
If your search doesn't use Regex, then you can easily modify SearchResult to instead just pass in a start offset and length for the constructor.
/// <summary>A search result storing a match and text segment.</summary>
public class SearchResult : TextSegment {
/// <summary>The regex match for the search result.</summary>
public Match Match { get; }
/// <summary>Constructs the search result from the match.</summary>
public SearchResult(Match match) {
this.StartOffset = match.Index;
this.Length = match.Length;
this.Match = match;
}
}
/// <summary>Colorizes search results behind the selection.</summary>
public class ColorizeSearchResultsBackgroundRenderer : IBackgroundRenderer {
/// <summary>The search results to be modified.</summary>
TextSegmentCollection<SearchResult> currentResults = new TextSegmentCollection<SearchResult>();
/// <summary>Constructs the search result colorizer.</summary>
public ColorizeSearchResultsBackgroundRenderer() {
Background = new SolidColorBrush(Color.FromRgb(246, 185, 77));
Background.Freeze();
}
/// <summary>Gets the layer on which this background renderer should draw.</summary>
public KnownLayer Layer {
get {
// draw behind selection
return KnownLayer.Selection;
}
}
/// <summary>Causes the background renderer to draw.</summary>
public void Draw(TextView textView, DrawingContext drawingContext) {
if (textView == null)
throw new ArgumentNullException("textView");
if (drawingContext == null)
throw new ArgumentNullException("drawingContext");
if (currentResults == null || !textView.VisualLinesValid)
return;
var visualLines = textView.VisualLines;
if (visualLines.Count == 0)
return;
int viewStart = visualLines.First().FirstDocumentLine.Offset;
int viewEnd = visualLines.Last().LastDocumentLine.EndOffset;
foreach (SearchResult result in currentResults.FindOverlappingSegments(viewStart, viewEnd - viewStart)) {
BackgroundGeometryBuilder geoBuilder = new BackgroundGeometryBuilder();
geoBuilder.AlignToWholePixels = true;
geoBuilder.BorderThickness = 0;
geoBuilder.CornerRadius = 0;
geoBuilder.AddSegment(textView, result);
Geometry geometry = geoBuilder.CreateGeometry();
if (geometry != null) {
drawingContext.DrawGeometry(Background, null, geometry);
}
}
}
/// <summary>Gets the search results for modification.</summary>
public TextSegmentCollection<SearchResult> CurrentResults {
get { return currentResults; }
}
/// <summary>Gets or sets the background brush for the search results.</summary>
public Brush Background { get; set; }
}
In order to make use of the background renderer:
var searchColorizor = new ColorizeSearchResultsBackgroundRenderer();
textEditor.TextArea.TextView.BackgroundRenderers.Add(searchColorizor);

Align in RichtextBox

Using this:
richTextBox1.AppendText("EMPID: " + "\t\t" + "4001");
richTextBox1.AppendText(System.Environment.NewLine);
richTextBox1.AppendText(System.Environment.NewLine);
richTextBox1.AppendText("EmployeeName: " + "\t\t" + "Taborjakol");
I got this:
How I will perfectly Align it to this:
What you can do is change the tab positions of the RichTextBoxControl.
richTextBox1.SelectionTabs = new int[] { 90, 180, 270, 360 };
richTextBox1.AppendText("EMPID: " + "\t\t" + "4001");
richTextBox1.AppendText(System.Environment.NewLine);
richTextBox1.AppendText(System.Environment.NewLine);
richTextBox1.AppendText("EmployeeName: " + "\t\t" + "Taborjakol");
The SelectionsTab property re-defines the spaces that are used for each tab in the RichTextBox control. You need to experiment with the tab settings to get the best result for your text.
Replace your last line of code with
richTextBox1.AppendText("EmployeeName: " + "\t" + "Taborjakol");
If you dont have to use a richtextbox, you should definetly have a look at the gridview or if you use third party tools like Telerik, DevCraft, ComponentOne and others most of them will have a control called Property Grid that has a layout you might be interested in.
If there is no other way around in using a richtextbox, you have to do the following:
Get a fixed-width font or called monospaced font http://en.wikipedia.org/wiki/Monospaced_font
Evalute the amout of characters which share the same width as a tab (I dont know the number of character have to figure it out yourself with testing)
Get the max length of your text at the left side (your "columnnames" I guess - like "EmployeeName")
Do some math - max length + one tab = x characters
now fill the remaining text at your left side with the neccessary tabs (can be anything from 1 to x) to get the same amout of characters as calculated in 4.
But again a richtextbox isnt the ideal control for this kind of scenario.
Edit:
Here some Code:
public partial class Form1 : Form
{
private const int FetchTestData = 50;
private const int TabCharLength = 5;
public Form1()
{
InitializeComponent();
//With this Fontsettings - 5 chars = 1 Tab - this changes with different fonts
this.richTextBox1.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
var type = typeof(TestData);
var list = GetTestData();
var maxProperty = GetMaxProperty(type);
maxProperty = FillToNext(maxProperty);
var properties = GetProperties(type);
for (var i = 0; i < FetchTestData; i++)
{
var data = list[i];
foreach (var propertyInfo in properties)
{
richTextBox1.AppendText(propertyInfo.Name);
var tabs = GetNumberOfTabs(maxProperty, propertyInfo.Name.Length);
for (var j = 0; j < tabs; j++)
richTextBox1.AppendText("\t");
richTextBox1.AppendText(Convert.ToString(propertyInfo.GetValue(data)));
richTextBox1.AppendText(Environment.NewLine);
}
if (i >= FetchTestData - 1)
continue;
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.AppendText("---------- NEXT DATA ----------");
richTextBox1.AppendText(Environment.NewLine);
}
}
private int GetNumberOfTabs(int maxLength, int textLength)
{
if ((maxLength % TabCharLength) != 0)
maxLength = FillToNext(maxLength);
var difLength = maxLength - textLength;
return (int)(Math.Floor(Convert.ToDouble(difLength / TabCharLength)) + 1);
}
private int FillToNext(int maxLength)
{
return maxLength + (5 - (maxLength % TabCharLength));
}
private PropertyInfo[] GetProperties(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
private int GetMaxProperty(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
return (from x in GetProperties(type)
select x.Name.Length).Max();
}
private List<TestData> GetTestData()
{
var returnValue = new List<TestData>();
for (int i = 0; i < FetchTestData; i++)
{
returnValue.Add(new TestData()
{
ID = i,
Name = "NameValue " + i,
Description = "DescriptionValue " + i,
PropertyA = "PropertyAValue " + i,
PropertyB = "PropertyBValue " + i,
SomeReallyLongPropertyName = "RandomStuff... " + i
});
}
return returnValue;
}
}
public class TestData
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string PropertyA { get; set; }
public string PropertyB { get; set; }
public string SomeReallyLongPropertyName { get; set; }
}
As stated you firstly would want to set the font to a mono-space font, this means that each letter will be the same size.
So you will see something like this:
EEEEEEEEEEE
Rather than
EeEeeiiiiiiiiiiiiiiiiii
Secondly, you could align the text which may improve it slightly:
richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
However unless you have to, I would suggest using a different control:
Gridview
Listbox
These are both easy to format, gridview has tabs, and the listbox you can use string.Format when entering the values.

how to display icon in a cell of a grid view c#

I want to Display an icon in the particular cell.
I am using this code. I have commented where i want to display the icon:
string lastScripDate = this.GetLastScripDate(sFolderPath, pb);
string sQuery = "select * from database";
DataTable table = new DataTable();
table = this.Retrieve_Data(sQuery, "database");
pb.Maximum = table.Rows.Count;
pb.Value = 0;
double num = 0.0;
double num2 = 0.0;
double num3 = 0.0;
double num4 = 0.0;
if (table.Rows.Count > 0)
{
for (int i = 0; i < table.Rows.Count; i++)
{
Application.DoEvents();
dgv.Rows.Add();
dgv.Rows[i].Cells["Sname"].Value = table.Rows[i]["Symbol"].ToString();
dgv.Rows[i].Cells["clsprice"].Value = table.Rows[i]["SCRIP"].ToString();
string str3 = table.Rows[i]["TradeType"].ToString();
dgv.Rows[i].Cells["trd"].Value = str3;
string str4 = str3;
if (str4 != null)
{
if (!(str4 == "p"))
{
if (str4 == "q")
{
goto Label_01DD;
}
}
else
{
dgv.Rows[i].Cells["trd"].Style.ForeColor = Color.Green;///////I Want to Display Icon in this Cell(Up.Ico)
}
}
goto Label_020B;
Label_01DD:
dgv.Rows[i].Cells["Trade"].Style.ForeColor = Color.Red;///////////////An Want to Display Icon in this Cell also(Down.Ico
Label_020B:
dgv.Rows[i].Cells["date"].Value = table.Rows[i]["TDate"].ToString();
num = Convert.ToDouble(table.Rows[i]["CPrice"]);
dgv.Rows[i].Cells["tp"].Value = string.Format("{0:0.00}", num);
num2 = Convert.ToDouble(table.Rows[i]["Price"]);
dgv.Rows[i].Cells["tp"].Value = string.Format("{0:0.00}", num2);
num3 = Convert.ToDouble(table.Rows[i]["Loss"]);
dgv.Rows[i].Cells["sl"].Value = string.Format("{0:0.00}", num3);
try
{
num4 = Convert.ToDouble(table.Rows[i]["Ratio"]);
dgv.Rows[i].Cells["Ratio"].Value = string.Format("{0:0.00}", num4);
}
catch
{ }
pb.Value++;
}
if (pb.Value == pb.Maximum)
{
pb.Value = pb.Minimum;
}
return true;
}
return false;
One way to acheive this would be to add DataGridViewImageColumn and then set icon to that cell. Here is a sample
dataGridView1.Columns.Add(new DataGridViewImageColumn());
dataGridView1.Rows.Add(2);
DataGridViewImageCell cell = (DataGridViewImageCell)dataGridView1.Rows[0].Cells[0];
Icon ic = new Icon("icon.ico");
cell.Value = ic;
1 - create some icons inside a property class
Example
public static class EIcons
{
/// <summary>
/// NULL
/// </summary>
public static readonly Icon NOICON = new Icon("Icon/null.ico");
/// <summary>
/// NEW
/// </summary>
public static readonly Icon NEW = new Icon("Icon/new.ico");
}
2 - Create a property for your datagrid, that contains strings, or int, and also ICONS objects.
The constructor must always initialize your icon to something consistent.
public class GridProperty01
{
/// <summary>
/// CONSTRUCTOR - factory initialization
/// </summary>
public GridProperty01()
{
ID = 0;
NewValue = string.Empty;
Alert = EIcons.NOICON; // solid icon initialization. Create transparent icon if you wish...
}
public int ID { get; set; }
public string NewValue { get; set; }
public Icon Alert { get; set; } // for icon
}
3 - Create a list of data and assign to your icon property the icon you need. At last bind the list to your datagrid, and refresh.
All this code must be put inside a button of SEARCH for example, or inside a method that update the gridview..
// GET - data for datagridview
List<GridProperty01> tempList = new List<GridProperty01>();
for (int jj = 0; jj < 3; ++jj) {
// INIT
GridProperty01 tempObj = new GridProperty01();
// GET - data
tempObj.ID = jj; // your code...
tempObj.NewValue = "xxx"; // your code...
tempObj.Alert = EIcons.NEW; // an icon...
// SET - insert row to list
tempList.Add(tempObj);
}
// SET - assign list to your gridview
dataGridView01.DataSource = tempList;
// SET - refresh
dataGridView01.Update();
dataGridView01.Refresh();
I suppose that's enough.
In addition, if you plan to SORT the Icon column when the column header has been clicked, create an Icon Text property, that contains different "text" accordingly to your Icon property. At the sort use this string to sort the rows of the datagrid. In this case, you should have:
// example code..
Alert = EIcons.NEW;
AlertText = "new";
Alert = EIcons.NOICON;
AlertText = "noicons";
you can make it by using DataGridViewImageColumn

Only top row of DataGridView updating?

I have a DataGridView that I'm populating from a list. The function that edits this list is called LoadCollectionData()'. Extra rows get added to the list just fine, and the relevant data pertaining to that row populates when the row is added.
The problem is that later on when other data is being changed that'd alter what's displayed on the datagrid, only the top row continues to update, all of the others remain the same.
Here's the code for the method:
public bool haschanged = false;
public class KeywordDensity
{
public bool included { get; set; }
public string keyword { get; set; }
public string occurences { get; set; }
public string density { get; set; }
}
public int WordCount(string txtToCount)
{
string pattern = "\\w+";
Regex regex = new Regex(pattern);
int CountedWords = regex.Matches(txtToCount).Count;
return CountedWords;
}
public int KeywordCount(string txtToCount, string pattern)
{
Regex regex = new Regex(pattern);
int CountedWords = regex.Matches(txtToCount).Count;
return CountedWords;
}
public List<KeywordDensity> LoadCollectionData()
{
string thearticle = txtArticle.Text.ToLower();
string keywordslower = txtKeywords.Text.ToLower();
string[] keywordsarray = keywordslower.Split('\r');
List<KeywordDensity> lsikeywords = new List<KeywordDensity>();
bool isincluded = false;
double keywordcount = 0;
double wordcount = WordCount(thearticle);
double thedensity = 0;
foreach (string s in keywordsarray)
{
if (s != "")
{
keywordcount = KeywordCount(thearticle, s);
thedensity = keywordcount / wordcount;
thedensity = Math.Round(thedensity, 4) * 100;
if (thearticle.Contains(s))
{
isincluded = true;
}
else
{
isincluded = false;
}
lsikeywords.Add(new KeywordDensity()
{
included = isincluded,
keyword = s,
occurences = keywordcount.ToString(),
density = thedensity.ToString() + "%"
});
}
}
return lsikeywords;
}
private void txtArticle_TextChanged(object sender, EventArgs e)
{
if (haschanged == false)
haschanged = true;
lblWordCountNum.Text = WordCount(txtArticle.Text).ToString();
dataGrid.DataSource = LoadCollectionData();
}
private void dataGrid_MouseUp(object sender, MouseEventArgs e)
{
int cursorpos = 0;
string copied = "";
if (dataGrid.CurrentCellAddress.X == 1) //Only grab content if the "Keyword" column has been clicked on
copied = " " + dataGrid.CurrentCell.Value.ToString() + " ";
cursorpos = txtArticle.SelectionStart;
txtArticle.Text = txtArticle.Text.Insert(cursorpos, copied);
}
What's even more odd, is that when I click on any of the rows, then they immediately update. However, unless the row is clicked on (unless it's the top one) it doesn't update.
Because of this, I suspect there may be some property I need to set on the dataGrid itself, or I need to somehow tell each row to refresh through code.
What's the dealio?
EDIT: It appears that the only reason that the cell that's clicked on updates is because I actively grab content from the cell. I commented out the code below and it stopped updating even when clicked on. It then would only update the top row's values and that's it.
Code:
//Moved above in EDIT 3
EDIT 2: Here's the class declaration for KeywordDensity:
//Moved above in EDIT 3
EDIT 3: Posted whole schebang.
I modified the code slightly, try this code.
string[] keywordsarray = keywordslower.Split
(new char[] {'\r','\n' }, StringSplitOptions.RemoveEmptyEntries);
You may need to Invalidate() the control to trigger a repaint.
call the DataBind() method of the datagrid. That should do.
Update
There's a ResetBindings() in that case.

Categories

Resources