How to implement a "fully viewed" multi-line TextBox? - c#

I am trying to create a form with a multi-line TextBox with the following requirements:
The text in the text box might be short or long.
There is not much space availble.
I want to be sure that the entirety of the text has been seen. I can't be sure that the user has actually read it, but I can at least require that all of it has been seen.
So I'm trying to make a "fully viewed" multi-line TextBox.
This picture should make it clear what I'm trying to do:
If they check the checkbox before they've scrolled through the whole thing, I'll know not to believe them.
I think I need to know:
When the form comes up, was the text that was put into the TextBox short (all visible without scrolling) or long (the vertical scroll bar was shown)?
If the vertical scroll bar is showing, has the user scrolled it all the way to the bottom?
Any ideas about how to achieve this?

The TextBox has no scrolling event but the RichTextBox has. Also it has a method that allows you to get the index of the character closest to a point position.
private readonly Point _lowerRightCorner;
public frmDetectTextBoxScroll()
{
InitializeComponent();
_lowerRightCorner = new Point(richTextBox1.ClientRectangle.Right,
richTextBox1.ClientRectangle.Bottom);
}
private void richTextBox1_VScroll(object sender, EventArgs e)
{
int index = richTextBox1.GetCharIndexFromPosition(_lowerRightCorner);
if (index == richTextBox1.TextLength - 1) {
// Enable your checkbox here
}
}

Related

Prevent text inside TextBox from scrolling horizontally

I have a WPF application, that has a number TextBox elements. The user fills those out, and the application then prints them. The problem with TextBoxes is that if you keep typing in one, once you fill it out to the end, the text starts scrolling horizontally to make room for more letters, and you no longer see the first couple letters that were typed in.
It was decided that the solution is to prevent the user from entering more text that will fit inside the TextBox. What's the best way of going about it?
I looked at TextBox properties, and didn't see anything that would do what I want directly. The first idea, is to set wrapping to Wrap. Then subscribe to PreviewTextInput event, and if the number of lines is going to exceed 1, handle the event without adding newly typed in text. Obviously you'll still be able to get around it by pasting text, but the bigger issue is that it will only work for single line TextBoxes, and I need it to work with multiline TextBoxes as well.
Is there a better approach that I'm missing? Would calculating text width, and then making sure it's less than TextBox width/height (how?) be a better option? Or perhaps another solution?
Here's my final solution, that also works for multiline TextBoxes. It will even work when pasting-in text. The only weirdness is that I delete trailing characters when text overflows, and that may seem strange if you are typing in text in the middle of the text box. I tried working around that by removing characters at the point of CaretIndex, but it was getting too involved. But other than that it does what I need it to do. To improve performance you can cache results from GetLineHeight function, so that you only need to call it once per TextBox (EDIT - I added the code for that as well).
<TextBox Height="23" Width="120" TextWrapping="Wrap" TextChanged="TextBoxTextChanged" AcceptsReturn="True"/>
private void TextBoxTextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox == null)
return;
double textLineHeight = GetCachedTextLineHeight(textBox);
int maxTextBoxLines = (int)(textBox.ViewportHeight / textLineHeight);
while (textBox.LineCount > maxTextBoxLines) //if typed in text goes out of bounds
{
if (textBox.Text.Length > 0)
textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1, 1); //remove last character
if (textBox.Text.Length > 0)
textBox.CaretIndex = textBox.Text.Length;
}
}
private double GetTextLineHeight(TextBox textBox)
{
FormattedText formattedText = new FormattedText(
"a",
CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight,
new Typeface(textBox.FontFamily, textBox.FontStyle, textBox.FontWeight, textBox.FontStretch),
textBox.FontSize,
Brushes.Black);
return formattedText.Height;
}
#region Caching
Dictionary<TextBox, double> _cachedLineHeights = new Dictionary<TextBox, double>();
private double GetCachedTextLineHeight(TextBox textBox)
{
if (!_cachedLineHeights.ContainsKey(textBox))
{
double lineHeight = GetTextLineHeight(textBox);
_cachedLineHeights.Add(textBox, lineHeight);
}
return _cachedLineHeights[textBox];
}
#endregion
You're looking for the MaxLength property. You'll have to experiment with the number since you need to remember that you can fit a lot of i's in the space you can fit one W in. Typically when I size TextBoxes, I size and set the max length based on W's since thats the widest character.
EDIT: just saw you want it to work with multi-line text boxes too... in that case, just set it to wrap and it won't scroll horizontally.

User Control Label and Textbox flow from right to left

It was pretty tough to think a title for this question so I'll try to make a better job explaining myself here.
I needed to create a dynamic Windows Form so that when checkbox gets checked/unchecked, few input fields appear/disappear. As far as I know FlowLayoutPanel seemed to be the best tool to achieve this. So I created a Custom User Control that included a Label and a Textbox. I designed this new Control in VS2013 desginer view:
Since the text on the label can vary in length it is important that textbox begins only when label has already ended. However the result I get at the moment looks like this:
The label should read out "ConnField" instead of "ConnFie". I tried adding these items in FlowLayoutPanel but that resulted in label and textbox not lining up correctly. Are there any attributes/properties that should be set in order to get the expected result? Should I use a container that does it all for me?
On a side note, if there are any other methods to dynamically show/hide elements in the fashion I described above I'd be very happy to use those instead.
For perfect fits you can script the TextChanged event(s) to make sure the TextBox always sits in place and keeps a nice size as well..
I have placed a Label and a TextBox into a Panel for testing. You will probaly not need or want the textBox1_TextChanged event but it was nice for testing..:
private void textBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = textBox1.Text; // this is for testing
}
private void label1_TextChanged(object sender, EventArgs e)
{
textBox1.Left = label1.Right + 6; // <= this is what you need
textBox1.Width = panel2.Width - label1.Width - 8; // <= this is nice to have
}
Of course your offsets may vary..and obviously the Label has AutoSize = true
Edit
Since you commented on the problem of getting the TextBoxes aligned with each other across rows here are a few thoughts about this problem. As Hans noted, you can't have it all:
Complete freedom for the Labels' content
Perfect fits
And aligned Textboxes
The three goals conflict. So you need to make compromises:
If you can restrict the content to a fixed maximum, the result will look best
Sometimes it helps to have a collegue or even a user look at the content to find a shorter way to express the meaning
Ellipsis or abbreviations may help. I both cases you should set a ToolTip to show the full content
Another option is to switch to a narrower Font for some Labels
Instead of one fixed Label size maybe 2 or 3 will help: The look will be a bit jagged but will look a lot better than with completely free sizes.

Maintain TextBox scroll position while adding line

In my WinForm application I have a multiline TextBox control (uiResults) which is used for reporting progress while processing a large number of items. Using AppendText works great for automatically scrolling to the bottom at every update, but if the user scrolls back to read some older data I need to turn off the autoscroll. I would rather stay away from P/Invoke calls if possible.
Is it possible to detect if the user has scrolled back without using P/Invoke? For now, I just check SelectionStart which works but requires the user to move the caret from the end of the textbox to stop the autoscroll:
if(uiResults.SelectionStart == uiResults.Text.Length)
{
uiResults.AppendText(result + Environment.NewLine);
}
My main problem is that when appending a string using the Text property, the textbox is scrolled to the beginning. I tried to solve this by storing the caret position and resetting and scrolling to it after the update, but this causes the current line to move to the bottom (of course, since ScrollToCaret scrolls no more than the necessary distance to bring the caret into view).
[Continued from above]
else
{
int pos = uiResults.SelectionStart;
int len = uiResults.SelectionLength;
uiResults.Text += result + Environment.NewLine;
uiResults.SelectionStart = pos;
uiResults.SelectionLength = len;
uiResults.ScrollToCaret();
}
Auto-scrolling text box uses more memory than expected
The code in the question implements exactly what you are looking for. Text is added, but scrolling only occurs if the scroll bar is at the very bottom.
I have had the same problem.
And finally, I made an easy way.
(Sorry, I'm not good at English.)
key point is get the first displayed char index using GetCharIndexFromPosition method.
//Get current infomation
int selStart = textBox.SelectionStart;
int selLen = textBox.SelectionLength;
int firstDispIndex = textBox.GetCharIndexFromPosition(new Point(3, 3));
//Append Text
textBox.AppendText(...);
//Scroll to original displayed position
textBox.Select(firstDispIndex, 0);
text.ScrolltoCaret();
//Restore original Selection
textBox.Select(selStart, selLen);
And, if textbox is flicking, use this extention.
Call textBox.Suspend() before adding text, and call textBox.Resume() after adding text.
namespace System.Windows.Forms
{
public static class ControlExtensions
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool LockWindowUpdate(IntPtr hWndLock);
public static void Suspend(this Control control)
{
LockWindowUpdate(control.Handle);
}
public static void Resume(this Control control)
{
LockWindowUpdate(IntPtr.Zero);
}
}
}
Hope this will help you.
Thank you~
Are you open to another approach, because you are bound to get into trouble this way and the solutions will get complex (pinvoke etc. that you want to avoid). For eg. suppose you find a way to "detect if the user has scrolled back" and you stop scrolling to bottom. But after reading the line user might want the scroll to bottom feature to resume. So why not give user a way to control Auto-Scrolling. Here's how I would do it...
Use a RichTextBox to show data and a Checkbox to control AutoScrolling, then your code might be something like this:
richTextBox1.AppendText(result + Environment.NewLine);
if (checkBoxAutoScroll.Checked)
{
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.ScrollToCaret();
}
RichTextBox by default will not automatically scroll to bottom on AppendText, so the first line would always be visible (and not the newly appended line). But if user checks this checkbox called AutoScroll, our code will then scroll the richtextbox to the bottom where new lines are shown. If user wants to manually scroll to read a line he will first need to uncheck the checkbox.

How to disable flickering of scroll bar when constantly changing multiline text box content?

So I'm using this code which moves the cursor to the end of the text box content each time I'm adding something to it.
void txtDisplay_TextChanged(object sender, EventArgs e)
{
txtDisplay.SelectionStart = txtDisplay.Text.Length;
txtDisplay.ScrollToCaret();
txtDisplay.Refresh();
}
The problem is that I see like flickering of text box scroll bar which goes up and down each time I add something to the text box. Doing this 10 times a second seems like it consumes some processing power and it looks ugly.
How to keep the scroll bar scrolled down all the time?
Hi there ( again :) ),
I've just looked for some methods to avoid this flickering and I found this post exploring both SelectedText property and AppendText() method, with the lattest actually appending the text and scrolling only if it's necessary.
Hope that'll help !

How to get the CaretPosition on textbox using GetCaretPos() or any other method?

I am trying to make a new application on C#, as a part of this I want to know the caret position (The exact point within the control) on a rich text control box.
I will explain it: assume I have a win form, rich textcontrol box and a contextmenustrip. When I type a specific charector or string on textbox I want to pop up this contextmenu item.
For this reason I want to know the exact point of caret on that text box.
As a result of googling + SO articles I found a way through GetCaretPos(), but I am unable to use it.
I did something with richtextbox get functions. One is the following:
Point k= richTextBox1.GetPositionFromCharIndex((richTextBox1.Lines[richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine())].Count() + 1));
I don't know if this is the exact point or not, but sometimes I am getting the correct value.
How can I fix the problem?
Here's a quick way of seeing where the context menu would appear. Just make sure you subscribe to the event.
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
Point point = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
Text = point.ToString ();// Write to window title for fun
new ContextMenu(new MenuItem[] {new MenuItem("test")}).Show (richTextBox1, point);
}

Categories

Resources