Textbox based status window issues

A

Andrus

I created winforms .net 2 mdi child status window class below.

My long running code writes messages to this window to allow user to see
progress.
I want that Log() method writes text at new line and textbox scrolls when
window becomes full.

Currently it adds text to end of current line and does not scroll if window
becomes full. How to fix ?

Is it reasonable to use BackgroundWorker class instead of this ?
If yes, how to change this class to use backgroundworker ?

Andrus.

public class Status : Form {

TextBox lb;

public Status(string title) {

lb = new TextBox();
lb.Dock = DockStyle.Fill;
lb.Multiline = true;
Controls.Add(lb);
StartPosition = FormStartPosition.Manual;
AutoScroll = false;
FormBorderStyle = FormBorderStyle.FixedSingle;
Text = title;
Left = 0;
MdiParent = AppDesktop.MainForm;
Top = (int)(0.6 * (double)MdiParent.Height) - 60;
Height = MdiParent.Height / 3;
Width = MdiParent.Width - 60;
ControlBox = false;
Icon = null;
MaximizeBox = false;
MinimizeBox = false;
Show();
}

public void Log(string s) {
lb.Text += "\r";
lb.Text += s;
lb.ScrollToCaret();
Application.DoEvents();
}}}
 
P

Peter Duniho

Andrus said:
I created winforms .net 2 mdi child status window class below.

My long running code writes messages to this window to allow user to see
progress.
I want that Log() method writes text at new line and textbox scrolls when
window becomes full.

Currently it adds text to end of current line and does not scroll if window
becomes full. How to fix ?

Well, your code already has a call to ScrollToCaret(), so all you should
need to do is ensure that the caret is at the end before you make that
call. Use the Select() method or the SelectionStart property to do this.
Is it reasonable to use BackgroundWorker class instead of this ?
Yes.

If yes, how to change this class to use backgroundworker ?

IMHO, the documentation for the BackgroundWorker class is actually
reasonably good at describing the basic mechanics. You'll subscribe
something to the DoWork event and that will be called to do the actual
work. You can also subscribe to the ProgressChanged event, and from
within your DoWork handler you can call the ReportProgress method to
have that method raised; the ProgressChanged handler is likely a good
place to handle updating the TextBox.

Pete
 
A

Andrus

Pete,
Well, your code already has a call to ScrollToCaret(), so all you should
need to do is ensure that the caret is at the end before you make that
call. Use the Select() method or the SelectionStart property to do this.

Thank you. I used Select method but messages do not scroll and caret appears
in start of second line. Here is complete code to reproduce issue:

using System;
using System.Windows.Forms;
using System.IO;

static class AppDesktop {
public static Form MainForm;
}

static class Program {

[STAThread]
static void Main() {
AppDesktop.MainForm = new Form();
AppDesktop.MainForm.IsMdiContainer = true;
AppDesktop.MainForm.Show();
Status s = new Status("working");
for (int i = 0; i < 10; i++)
s.Log("Line " + i.ToString());
Application.Run(AppDesktop.MainForm);
}
}

public class Status : Form {

TextBox lb;
public Status(string title) {
lb = new TextBox();
lb.Dock = DockStyle.Fill;
lb.Multiline = true;
Controls.Add(lb);
StartPosition = FormStartPosition.Manual;
Text = title;
Left = 0;
MdiParent = AppDesktop.MainForm;
Top = (int)(0.6 * (double)MdiParent.Height) - 60;
Height = MdiParent.Height / 3;
Width = MdiParent.Width - 60;
Show();
}

public void Log(string s) {
lb.Text += s;
lb.Text += "\r\n";
lb.Select(Text.Length + 1, 0);
lb.ScrollToCaret();
Application.DoEvents();
}
}
IMHO, the documentation for the BackgroundWorker class is actually
reasonably good at describing the basic mechanics. You'll subscribe
something to the DoWork event and that will be called to do the actual
work. You can also subscribe to the ProgressChanged event, and from
within your DoWork handler you can call the ReportProgress method to have
that method raised; the ProgressChanged handler is likely a good place to
handle updating the TextBox.

Thank you.

BackgroundWorker makes code complicated. What are advantages of using it in
this case ?

Andrus.
 
P

Peter Duniho

Andrus said:
Thank you. I used Select method but messages do not scroll and caret appears
in start of second line. Here is complete code to reproduce issue:

Try using "lb.Text.Length" instead of "Text.Length + 1" in your call to
Select().

Pete
 

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