TextBox paint disable?

G

Grant Schenck

I want to append text to the end of a text box. If the cursor is currently
at the end I just want to append it and have the normal scrolling take place
if needed.

However, if the selection isn't at the end of the text box I want to append
the text but WITHOUT changing the selection or scrolling the text.

I can easily tell if the selection is at the end or not.

However, I haven't found away to prevent the scrolling when I AppendText.

Is this possible? Seems like I'd need to do something like:
- Grab the selection point
- Turn off updating
- Append the text
- Restore the selection point
- Turn on updating in such a way that if part of the view changed it will
show.

Thanks,
 
G

Grant Schenck

Made some progress...

At this point I can write text to the end of my window without changing the
current selection or causing any scrolling IF the end of the file is not
visible on the screen. If the end of the file is visible then the cursor
jumps to the start.

This is the logic I'm using:

void Log(string str)
{
str += "\r\n";
int nLength = textBoxLog.TextLength;
int nSelectionStart = textBoxLog.SelectionStart;
int nSelectionLength = textBoxLog.SelectionLength;

if (nSelectionStart >= nLength && nSelectionLength == 0)
// We are, write the text to the end and let it scroll (or make it
if need be)
textBoxLog.AppendText(str);
else
{
textBoxLog.SuspendLayout();

// Write the text
textBoxLog.Text += str;

// Restore the insertion point
textBoxLog.SelectionStart = nSelectionStart;
textBoxLog.SelectionLength = nSelectionLength;
textBoxLog.ResumeLayout();
}
}


Any ideas?

Thanks, Grant Schenck
 
G

Grant Schenck

Actually, what is happening is when I add the text, it scroll the view to
the start. The selection seems to be correct.

So, how do I prevent it scrolling to the start when I add text as below?

Thanks, Grant Schenck

Grant Schenck said:
Made some progress...

At this point I can write text to the end of my window without changing the
current selection or causing any scrolling IF the end of the file is not
visible on the screen. If the end of the file is visible then the cursor
jumps to the start.

This is the logic I'm using:

void Log(string str)
{
str += "\r\n";
int nLength = textBoxLog.TextLength;
int nSelectionStart = textBoxLog.SelectionStart;
int nSelectionLength = textBoxLog.SelectionLength;

if (nSelectionStart >= nLength && nSelectionLength == 0)
// We are, write the text to the end and let it scroll (or make it
if need be)
textBoxLog.AppendText(str);
else
{
textBoxLog.SuspendLayout();

// Write the text
textBoxLog.Text += str;

// Restore the insertion point
textBoxLog.SelectionStart = nSelectionStart;
textBoxLog.SelectionLength = nSelectionLength;
textBoxLog.ResumeLayout();
}
}


Any ideas?

Thanks, Grant Schenck
 
K

Kevin Yu [MSFT]

Hi Grant,

We have reviewed this issue and are currently researching on it. We will
update you ASAP. Thanks for your patience!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
J

Jeffrey Tan[MSFT]

Hi Grant,

Thanks for your post.

Yes, I see your concern. The code snippet in your second reply almost gets
the result you wanted. However, setting the TextBox.Text property will
always force the textbox focus(caret) go to the start, which you do not
want.

Currently, I can not find a perfect workaround for this issue, because many
methods of TextBox will do the automatic scrolling out of our control. For
example, AppendText will auto scroll to the end, set the Text property will
auto scroll to the start. However, TextBox.Select method does not do the
scrolling anymore.

Does the code snippet below meet your need? I just use
TextBox.ScrollToCaret method to force the textbox to scroll to the caret,
which near where focus reside. For your information:
void Log(string str)
{
str += "\r\n";
int nLength = textBoxLog.TextLength;
int nSelectionStart = textBoxLog.SelectionStart;
int nSelectionLength = textBoxLog.SelectionLength;


if (nSelectionStart >= nLength && nSelectionLength == 0)
{
textBoxLog.AppendText(str);
}
else
{
textBoxLog.AppendText(str);
this.textBoxLog.Select(nSelectionStart, nSelectionLength);
this.textBoxLog.ScrollToCaret();
}
}

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top