Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I show the upper part of a line that doesn't fit whole in a multiline textbox?

In my WinForms application, I have fixed-size multiline textboxes that can contain a variable number of lines. I want the textboxes to fit two lines plus tax in them so that only the upper part of the third line is displayed when a third line is present. Unfortunately, the third line is not displayed at all and there's just empty space instead -- you need to scroll down to see it. This is not good because the idea is to indicate to the user that scrolling down might be necessary. Is it possible to force the textbox to display the upper part of the third line?

like image 277
Michał Masny Avatar asked Feb 01 '16 02:02

Michał Masny


2 Answers

I don't think you can do this out-of-the-box. But, there are 2 ways that I can think of by which you can achieve your goal

  1. Do you really need a TextBox control. Can a Label work for you. If yes, then Label does not have the problem you describe above. If not, then you can use a nifty trick to always display your contents in a Label and switch it to a TextBox when a user starts typing.
  2. Another way is to disable scrolling in the TextBox. Adjust the height of the TextBox to 3 clearly visible lines. Now drop this TextBox into a Panel. Make sure the Panel uses Panel.AutoScroll = true (you can use a separate VerticalScrollbar or HorizontalScrollbar or both if you need more control). Now adjust the Panel.Height so that only 2 full lines are visible and the 3rd line is partially visible.

You mentioned that your TextBox

can contain a variable number of lines

But you also mentioned

I want the textboxes to fit two lines plus tax in them so that only the upper part of the third line is displayed when a third line is present

So not sure what is the case. If you need to dynamically adjust the height of your TextBox, then look at this post to Autoresize textbox control vertically

like image 78
Vikhram Avatar answered Sep 30 '22 02:09

Vikhram


Perhaps you could use the RichTextBox class, which displays partial lines by default. It is derived from the same base class as TextBox (TextBoxBase), so it should be a drop-in replacement.

like image 24
cokeman19 Avatar answered Sep 30 '22 03:09

cokeman19