Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with the TextBox.AppendText method

Status
Not open for further replies.

dafyddg

Programmer
Dec 2, 2003
92
GB
Hi,

I'm using a TextBox to provide output from a process that a utility i'm writing runs. Now i thought this would be a simple case of calling the AppendText method each time a new message was ready to disply. But no. For the first 1000 or so message all it fine, but then after a certain point it stops adding more messages. No exception is thrown and the program is still running. It simply seems to get to a limit, which i have found to be 29687 characters and adds nothing more. Now i have checked the MaxLength property and that is fine, set it to 100,000 or 0 and the same thing happens.

What is odd though is if i do:

Code:
this._txt_Output.Text += newMessage;

Then is appears to hit no limit at all.

However the only problem with this method is that it does not scroll the text box down as happens with AppendText.

If you can tell me how to force the TextBox to scroll down (ScrollToCarat) does not appear to work, then that would help.

Otherwise, what on earth is wrong with AppendText?

Thanks in advance

Dafydd
 
After you append text call:

this.Select ( this.Text.Length, 0 );

Hope this helps.
 
By this I ment whatever your textbox is.
so I guess it would be:

this._txt_Output.Select ( this._txt_Output.Text.Length, 0 );
 
Nope, that didn't do anything sorry.

Could you try running this code so you can see what i mean.

Just create a simple windows form application with a button and a multi line text box and for the on click event of the button, put the following example code in

Code:
for (int i = 0 ; i < 2000 ; i++)
{
	this.textBox1.Text += "This is message " + i.ToString() + Environment.NewLine;
	this.textBox1.Select(this.textBox1.Text.Length, 0);
}

You should see that it just doesn't go past a certain character limit.

Any other ideas?
 
By the way
this._txt_Output.Text += newMessage;
is not using TextAppend. It is equivalent to
this._txt_Output.Text = this._txt_Output.Text + newMessage;

Do you want
this._txt_Output.TextAppend(newMessage)


- free online Compare/Diff of snippets
 
JohnYingLing

I know that using += is not the same as AppendText().

I'd actually be curious to see inside the AppendText method to see how it works and why it works the way it does.

I don't suppose there is anywhere you can view the pdb file for the class is there? I would guess not.

 
Lutz Roeder's Reflector shows.
TextboxBase

public void AppendText(string text)
{
int num1;
if (base.IsHandleCreated)
{
num1 = SafeNativeMethods.GetWindowTextLength(new HandleRef(this, base.Handle)) + 1;
}
else
{
num1 = this.TextLength;
}
this.SelectInternal(num1, num1);
this.SelectedText = text;
}

internal virtual void SelectInternal(int start, int length)
{
int num1 = start + length;
if (base.IsHandleCreated)
{
base.SendMessage(0xb1, start, num1);
}
else
{
this.selectionStart = start;
this.selectionLength = length;
}
}


- free online Compare/Diff of snippets
 
Well after a bit of digging, i have found out that the AppendText method does indeed have a bug in it. I tried to understand the nature of the problem but got lost while digging through disassembly of the dll.

The one thing i did find is that if you do

Code:
_txt_Output.SelectedText = text;

Then you get the same result. i.e. it appends text normally until it reaches a limit.

The method i have decided to go for is

Code:
private void AppendMethodToTextBox(string text)
{
	int length = this._txt_Output.TextLength;
	this._txt_Output.SelectionStart = length ;	
	this._txt_Output.SelectionLength = length;
	this._txt_Output.SelectedText = text;
	this._txt_Output.ScrollToCaret();
			
}

Hope this is of some help to others in the future.

Hopefully MS will fix this bug in version 2.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top