In a WPF application, I want to build a "Find in Files" output pane, in which I can stream large quantity of text, without re-allocating memory at each line, like the TextBox would do.
The WPF TextBox has a single Text property which stores a contiguous string. Each time, I want to add content, I need to do textBox.Text += "New Text", which is bad.
Ideally, that control would be virtual and require a minimum of resources, just for the visible lines.
I thought about using a standard ListBox with a VirtualizingStackPanel, but it does not allow Text Selection across lines.
(At each new line added, I want the control to update)
Any suggestion?
If you do not expect much more than ten-thousands of search results in your application, a TextBlock control or readonly multiline TextBox will suffice by far.
The TextBox class has an AppendText() method which should be fast enough for you.
If you need text highlighting / formatting then maybe you want to use RichTextBox.
If you have really large content, then unfortunately all the WPF textbox and similar controls are very slow. See this question. You could use AvalonEdit as a replacement.
Have you considered or tried the RichTextBox control?
A StringBuilder, just append the text to the String builder and instead of doing
textBox.Text += moreText;
do
myStringBuilder.Append(moreText);
textBox.Text = myStringBuilder.ToString();
This should take care of the Schlemiel the Painter's algorithm.
Of course, the string builder should have to be a member of your class so it exists through your object's life span.
Related
I am using Visual Studio 2019 in order to create a Windows Form Application. I need some titles in my application, which means these strings will not be modified by the user.
For now, I created textboxes for these titles and made these textboxes "read-only". However, this does not satisfy my aesthetical expectations.
Therefore, I wonder if there is a way to add a string without adding a textbox, to the form. Is there a way?
Thanks in advance :)
Consider using a Label control rather than a TextBox.
The only time I would use a TextBox as a label is if I want the user to be able to copy the info, and I make it borderless, readonly and have the same colour as the background of the form. It's not superb UX though as there isn't anything that screams "you can highlight and copy this text" other than an I beam cursor, which is pretty much "mystery meat navigation" - better off putting a copy button next to it if you expect the user to copy info often
Why not use Label for your titles?
Since label, by default, cannot be modified by the user, thats what you want. Textbox is used for the user input, not for the titles.
Use Label control that is the right control to use for your requirement
In my Universal Windows App i want to log some events and writing them in a TextBox Element. But i couldn't find a method like named "appendText". And below method makes flickering on screen
txtbox.Text += log;
What is the best method to appendText for TextBox or TextBlock or RichTextEdit elements?
if you want to display some logging information, it might be read only information so using a TextBlock is already an optimization. Then, depending on your needs, a multi-item control such as a ListBox, ListView or GridView might be even better for logs.
If you have more than hundreds of lines, think about using virtualization.
https://msdn.microsoft.com/en-us/library/windows/apps/mt574120.aspx
I'm looking for a way to highlight (not select) words based on search terms inside a Windows.UI.Xaml.Controls.TextBox control.
There doesn't seem to any way to override the text rendering behaviour? Can this be done with the textbox control?
Edit:
I was originally trying to use the RichEditBox but was having a problem with being able to paste in formatted text which I was trying to prevent (the only event I can clear the formatting on is TextChanged which seems a little late). I also really need more control over the rendering of the highlights
Textboxes are very limited in regards to formatting and text selection.
You might want to use the RichTextBox control rather than a TextBox as it gives you greater selection and formatting capabilities to make highlighting multiple terms easier.
Here's a quick start: http://www.devx.com/dotnet/Article/34644
I am having the same issue. But Incase you didn't know, I'll post this anyway (it's not a complete answer but might get you started.)
You can highlight words by using:
int indexOfFoundKeyword = 1;
int lastIndexOfFoundKeyword = 12;
txtbox.SelectionStart = indexOfFoundKeyword;
txtbox.SelectionLength = lastIndexOfFoundKeyword;
If you place this inside a button click event and then type about 20 chars and click the button you'll see that you can highlight words in a Metro textbox. The problem that I'm having is getting the start and ending of the found keyword so that I know what values to assign to SelectionStart and SelectionLength and if I understand your question correctly, I think that's where you're stuck at, too.
Update
OK, this seems to be a little tempremental but works most of the time. (and accurate when it does work):
// find the word 'jason'.
string word = "jason";
int a, b;
a = genericBox.Text.IndexOf(word);
b = word.Length;
genericBox.SelectionStart = a;
genericBox.SelectionLength = b;
If all you are trying to do is find some text, then select it, then this proves that it can be done. Now I know this is not the best way but it's the only way I've been able to get something like this to work in a plain TextBox for metro apps.
Perhaps you could create a control based on the TextBox control or altogether create a custom control. See here and here for more help.
I'm trying to learn WPF and was thinking about creating a simple IRC client. The most complicated part is to create the chat log. I want it to look more or less like the one in mIRC:
or irssi:
The important parts are that the text should be selectable, lines should wrap and it should be able to handle quite large logs.
The alternatives that I can come up with are:
StackPanel inside a ScrollViewer where each line is a row
ListView, since that seems more suitable for dynamic content/data binding.
Create an own control that does the rendering on its own.
Is there any WPF guru out there that has some ideas on which direction to take and where to start?
I suggest you start with a good object model independent of the UI, and then try a multi-line TextBox or a RichTextBox.
Whether these will suffice will depend on exactly how long you want the log to be able to get. If you run into performance issues, you may need to look at virtualization.
First of all, you should consider if you want to select only entire row (like in a listbox), or if you want to select certain characters from a row (like in a textbox).
In the first case, I think a ListView or even a ListBox should be enough, both of them support virtualization when bound to collection and there should be no problem with huge amounts of data. A stack panel inside a ScrollViewer is a little bit like reinventing the wheel for this case and creating a new control is not a very inspired approach in my opinion (as the functionality you want can be achieved with the existing controls, in WPF).
In the second case, if you want to select some text inside of a line, or if you want word wrapping for your longest lines in the log and want to select individual parts of the wrapped lines, then you need to use a control more oriented on displaying text. Kent already suggested a RichTextBox, I would add AvalonEdit control or even the WebBrowser control in which you directly modify its HTMLDocument.
I would suggest to use RichTextBox too, and store items in a log file or database, if you run into performance issues.
Another solution is to use the WPF WebBrowser control and modifiy its HTML content with:
webBrowser.NavigateToString("<HTML><H2><B>This page comes using String</B><P></P></H2></HTML>");
More information about using WebBrowser control
I can only find rather "complex" functions, like RTF Text box etc. I couldn't figure out how to display "plain text" in a Form1.cs
(I do not talk about HTML to WinForm. Search did not return any useful results)
You can use a Label or a TextBox.
If you use a TextBox, set MultiLine to true and ReadOnly to true to not allow editing.
Use a TextBox or Label. That's what they're designed for - displaying plain text.
Label is not very suitable for that. It makes a new line only on "\r\n", which means a very long paragraph may exceed size of the form.
A TextBox with the following properties is perfect for it:
Multiple lines
ReadOnly = true
No border.
And, drag the right / bottom borders to align with borders of the form, it's size will automatically change along with the form.