Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Textbox based status window issues

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrus - 23 Sep 2007 22:50 GMT
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();
}}}
Peter Duniho - 23 Sep 2007 23:20 GMT
> I created  winforms .net 2 mdi child status window class below.
>
[quoted text clipped - 5 lines]
> 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
Andrus - 24 Sep 2007 09:17 GMT
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();
}
}

>> If yes, how to change this class to use backgroundworker ?
>
[quoted text clipped - 5 lines]
> 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.
Peter Duniho - 24 Sep 2007 09:27 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.