C#, Display plain text in a form - c#

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.

Related

Hyperlink inside a WinForms Label

For example, the About dialogue of Notepad has only "Microsoft Software Licence Terms" as a hyperlink but the rest of the text is not. How can I achieve similar result with the Label (or Labels) of WinForms?
I can use two labels, one Label and another LinkLabel, but then the two will not be concatenated naturally (the width of labels may vary by the system font face or the language).
You could use a LinkLabel, then set the LinkArea property. This should highlight the link you want and will also be the only clickable part.
Properties > LinkArea > ... button then just highlight the text part you want as a link

How to lighlight link in text box

I have to create some controls similar to a control TextBox. But I want this control can Highlight link as RichTextBox, Because I have problem with RichTextBox so i can't use it. How do I highlight a link in a TextBox? or I have to set each property by a line of code?
I want to display it in a TextBox as shown below:
This is not possible. A simple TextBox control cannot be used to display formatted text.
A hyperlink is just specially formatted text: all it really does is change the foreground color to blue and the text style to underlined. TextBox controls don't allow this type of granularity. All text in the control has the same formatting applied to it.
What you would need to use is a RichTextBox control. This is specifically designed for displaying formatted text, and not only does it allow you to define arbitrary formatting for specific bits of text, but it has built-in support for detecting and highlighting hyperlinks. You say in your question that you had a "problem" with using the RichTextBox, so that is really the question that you should have been asking.

What is the (technical) difference between a Label and a TextBox?

I have seen a lot of answers on the web stating that a Label's text can't be selected/copied in the way that a TextBox's contents can,
but what is the underlying reason that a Label's text can't be copied?
Windows itself can find text under the cursor position, so why can't the WinForm Label control?
In order for a user to select or copy a control's text, the control must allow you to set focus to it, either by clicking or tabing to the control.
A Label doesn't allow this, by design.
Label controls are typically used to provide descriptive text for a control. For example, you can use a Label to add descriptive text for a TextBox control to inform the user about the type of data expected in the control.
So while Labels and TextBoxes both inherit from System.Windows.Control they are different things, intended for different purposes. In the same way that oranges and apples are both fruit, but are different.
However, if you're creating an application and want to have something that looks like a label, but allows the user to select (but not edit) the text, then you can use a TextBox with the following properties set:
Backcolor = Control
ReadOnly = true
BorderStyle = none
As shown below...
Alternatively, if you have an application and want to get text from something like a label, you can use the Win32 API function GetWindowText, if you know the handle to the window that contains the text. In a Win32 context a "window" means just about anything distinct that is on the screen, not just the windows that you can drag around with your mouse. WinForms is an abstraction on top of all this.
As for getting the handle to the window that is under the mouse cursor, see this question.

Aligning controls inside cells of a TableLayout

In the picture below I have defined a 6-row table layout and placed controls inside its cells, in one cell goes the Label, the other cell for the control its self (a text box for example )
The problem is caused by that field options radio box, it will make the cell so big so then the text box that is under "Preview Data Value" label, goes way below...
How do you suggest fixing this issue?
From my prior experience, I would not use the table layout at all. Instead I would set the anchors on each control to dynamically change with resize.
But if you insist on using the table layout, perhaps you should change the RowSpan attribute of the Preview Data Value cell, and have the label at the top with the text box underneath.
Another option would be to use a container like a GroupBox with a title Attribute that you can set to "Preview Data Value", put the text box in that container and set the "Dock" Attribute to "Fill". Then from the looks of it, you don't even need the last Row.
See this article on MSDN if you want to go totally nuts and implement your own drawing.
Make a seventh row and have the option group span two rows.

WPF Multiline TextBox for large content

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.

Categories