Scrolling all but the top line in a multiline textbox - c#

I have a multiline textbox in a WinForms application. What I'd like to do is always have the top line visible, even if it scrolls. Is there some trick someone knows to do this?

Fake it. Use two TextBox objects, draw your own borders. You will need to deal with wrapping to the next line yourself.
You could also copy the first X characters to a label so when the TextBox scrolls they can see the first line in the label.
Unless it is an essential feature I would try to cut it.

The simple answer; depending on the appearance you are going for is to use existing windows controls to get the effect you want.
You can use a label control above a textbox and allow the textbox to scroll.
You can use two textbox - the top with it's .multiline property set to false, while the bottom allows scrolling.
You could encapsulate this all into a user control for reuseability.
Beyond that, I think you'd be looking at a fairly large project to implement your control (or at least overriding the onPaint() event of the textbox) with the desired behavior.

Related

how to match scrollbar to lines in RichTextBox

I'm adding buttons (with text and images) to a richtextbox control, but because I don't use the following line:
richTextBox1.AppendText(Environment.NewLine + str);
The scroll bar doesn't match the total number of lines, and doesn't resize properly, how can I solve it?
What is going wrong
I find this to be a very peculiar way of doing things. So first, lets add a little background:
When you use richTextBox1.Controls.Add(new button()) - what you are doing is: you are adding the new button as a child of the richtextbox, not as content - and this is the problem.
The scrollbars are set to read their height/scrollable area etc from the content of the richtextbox, and yours has no content, hence no scrollbar (or false readings etc).
You could create a custom richtextbox control that inherits its scrollable area from the location of its child controls. However, if you would rather avoid that route, please continue reading:
The correct way
I do not know your use case, so this may not work, but I would remove the richtextbox control and instead use a panel control. The panel control for example will update it's scrollbars based on child controls, whereas the richtextbox wont - and this should give you the behaviour you are expecting.

How to make Windows.Form-Element disappear when content is empty?

I have a SplitContainer that contains a textBox, which is used to inform about errors or speficic situations. But most of the handled cases don't produce errors and therefore the box is not needed. In thoses cases I would like to make it disappear. Setting Visibile=false is not what I intend, because it still limits the other Windows.Form-elements. Instead those elements should "grow" in the left space from the box. Is there some thing like a floating disapear behaviour?
If I understand correctly, the text box in question is placed inside the let say right panel of the SplitContainer while the other controls - inside the left panel (I might be wrong, but otherwise I see no reason why you are mentioning the SplitContainer in the question). Then, depending on which panel of the SplitContainer contains the text box, you can set Panel1Collaped or Panel2Collapsed property to false to make it disappear and let the other panel fully occupy the split container space.
If you want the control to be removed, you can do:
textBox1.Dispose();
This also causes your other elements to "grow" in the left space like you wanted.

How to fix translated text over controls in Winforms?

I have got labels in my Windows Forms application which are placed next to controls.
When I change my language options to another language, the text needs more space now it covers my control.
Are there any ways to fix this problem?
No magic here I'm afraid. You have to try making your labels as (reasonably) wide as possible to accomodate each of your supported languages. Don't use AutoSize property here.
If labels appears on the left, have your text aligned to the right using the TextAlign property. This way they should always appear close to your control without ever overlapping them.
Also try to use containers to design your forms: TableLayoutPanel, FlowLayoutPanel. Controls that are placed in their cells, are autosized and wordwrapped correctly in most cases.

Perform some action when tabbing between labels?

I know that label can't receive focus, it doesn't seem to be responsive to tab switching. But all I need is perform some action when user uses tab consequently on a form which has only labels. For example, each label has an associated textbox but this textbox is hidden when the label is visible and vice-versa. What I want is allowing user to use tab to switch between the hidden textboxes on the form, normally, all the textboxes are hidden while all the labels are shown, the labels are supposed to be focusable so that when using tab, it can know that (as some event) to show the associated textbox and hide itself, when switching to another label, the current label whose the associated textbox is shown will become visible again and its associated textbox will become hidden.
I have to implement this kind of 2 in 1 control (textbox and label in a composited control) because I just want to show only the text (no border and background) as if the textbox has a transparent background and only show the textbox (and hide the label) when user need to edit (start by clicking on the field or using tab). This should have been easier for me if there was a transparent background textbox but there isn't a decent one in the world of windows forms. Please notice that I also know of the alpha blend transparent textbox presented in an article in codeproject but it can't meet my need because the text is rendered wrong with ugly border around the text path (some kind of missing antialiasing but it's even worse than that).
I'm really pity if this mechanism can't be implemented, the forms look better when all the fields seem to show info only but a click or tab switch can let user jump in edit mode.
I hope there is some solution out there. Thank you in advance.
I found this solution by a whim in my mind. I didn't think there was such a solution but it does help solve my problem (and I'm sure many others will benefit from it). Simply I have to create my own Label inheriting UserControl. I didn't thought of UserControl before and it is very helpful. Focusability, transparent background, borderlessness are all which can be done easily to a UserControl. The only custom feature I have to do myself is rendering the text which is also very simple and there are many ways to do. I just add a Label to the UserControl and set Label's Dock to DockStyle.Fill, adjust the height of the UserControl properly and that's all.
Thank God helping me think of UserControl before trying any other complicated solution such as listening to TAB and SHIFT + TAB keypress events.

Including SelectionColor in TextBox

I need the property called SelectionColor in the TextBox class, for a simple Syntax Highlighter - I can't use directly a RichTextBox - it causes too many problems, that's why I try to do this.
Is there any way to make that property available for a TextBox?
If it isn't possible, I'd try to write my own, but I need an idea on how to do it, basically how it works - is it based on drawing strings over the original text?
Thanks in advance.
Basically, using a TextBox for anything but plain text is a bad idea. First of all, you will eventually get a new feature to implement which is not present in TextBox and you will have to handle it manually. After some time you will implement a custom RichTextBox or something similar.
Second, it is relatively hard to even solve the problem you mentioned. Technically, you can override painting function (which you have to do if you want new functionality for TextBox). You can then let TextBox paint itself and paint the colored text above the image. But don't do it. You will get two (maybe more) problems:
Flicker of image. Once the original textbox has drawn itself, the image can be shown on screen (if you don't use double buffering).
Text alignment. It is hard to place colored text exactly above black text, plus you can run into problems with text rendering: you will need to clear area you're drawing in.

Categories