Textbox append or set a line of text

geomeo123

New member
Joined
Apr 22, 2025
Messages
3
Programming Experience
1-3
I have a textbox in WPF, with some c# code behind. I'm also using the serialport library. Generally speaking I can put messages into the textbox when I write or receive from a serial port. I do this by an invoke method. Either by invoking this method:

c#:
private void SetTextADCPDisplay(string text)
{
 this.TextBoxADCPDisplay.Text = text;
}
Or this method....
C#:
private void AppendTextADCPDisplay(string text)
{
    this.TextBoxADCPDisplay.AppendText(text);      
}

The above works as expected with no problem.

What I'm trying to accomplish is after displaying some lines of text I want one of my lines of text to stay at a specific line index, because I have a counter, I don't really want the lines to be scrolling after each count. So, for example I've tried using
C#:
 var LineIndexNumber = TextBoxADCPDisplay.GetLastVisibleLineIndex(),

which retrieves the index # of the last line written, then I just add 1 to it, but I've not found anywhere I can use that index #. The only place I did find something I ended up writing to the character position.
 
Solution
It sounded like you just wanted to keep updating the same last line and keep things on screen. So just replace that last line or whichever line you wanted to replace, and then assign the new multiline text value to the text box. Often assigning a new text value to a text box causes a scroll operation to happen, and so you want to scroll back to the previous position you were last at before changing the text value.
Moving this thread to WPF...
 
It sounded like you just wanted to keep updating the same last line and keep things on screen. So just replace that last line or whichever line you wanted to replace, and then assign the new multiline text value to the text box. Often assigning a new text value to a text box causes a scroll operation to happen, and so you want to scroll back to the previous position you were last at before changing the text value.
 
Solution
In general, if you are using WPF the way it was designed to be used, you would update the value that the text property is bound to. You would make use of property binding instead of manipulating the property directly. Doing the latter is the WinForms way of doing things, rather than the WPF way of doing things.

But that's where things get hairy. If the text value is updated via the binding you need to force the scroll position back to its previous location.
 
Thank you!! The keyword "replace" is the one I needed. It's a little bit hacky on how I did it, but it works for me:

C#:
int i = ADCPCountTime60s;// assign i to the same value as my 60 second counter
i++; // plus one so i is the count history. (my counter is decreasing each time). 

if (MyFirstADCPCounterLineAdded)//If an initial counter line added @ around about line 7 in my textbox....
{
 
    TextBoxADCPDisplay.Text = TextBoxADCPDisplay.Text.Replace("Est Time Left " + i.ToString() + " Seconds", "Est Time Left " + ADCPCountTime60s.ToString() + " Seconds");
}
else
{

    this.TextBoxADCPDisplay.AppendText(text);//Is where it adds the initial line.
}

I hope this helps someone else. I've been trying to resolve this one for about 4 hours now.
 
Back
Top Bottom