Screen logger class

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

How to create screen logger class from WinForms application.

It should have the following methods:

Open() - creates a window in screen
Writeln(string s) - writes text to this window
Close() - closes the window

The issue is with Open() function.
How to create a window which remains in screen and allows to write log text
into it?

Andrus.
 
Hi,

You can just open a new windows form that contains only a textbox, then you
can write to it as needed.

Not sure if this will solve your problem.
 
Ignacio,

Thank you.
I think this solves the issue.
I'nt it better to write to the form area directly ?

Where to find some sample code which implements this ?

Andrus.
 
You can just open a new windows form that contains only a textbox, then
you can write to it as needed.

I created the following form for logging.
Howver, messages are not shown. Why ?
How to implement scrolling when writing to last line in login form?

Andrus.


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

class main {
[STAThreadAttribute()]
public static void Main() {

LoginForm f = new LoginForm();
f.Show();

f.Writeln("Login message 1");
f.Writeln("Login message 2");
MessageBox.Show("");

}

class LoginForm : Form {

int vpos = 0;

public void Writeln(string s) {

Label l = new Label();
l.Visible = true;
l.Text = s;
l.Top = vpos;
vpos += 20;
}
}
}
 
Look at TextBox.ScrollToCaret()

thank you.
i tried listbox and this seems to solve the scolling issue.

Is this code best way to create scrolling logging window ?

Andrus.

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

class main {
[STAThreadAttribute()]
public static void Main() {

LoginForm f = new LoginForm("Performing task x");

for (int i = 0; i <= 100; i++) {
f.Writeln("Login message " + i.ToString());
}
MessageBox.Show("");

}

class LoginForm : Form {

ListBox lb;

public LoginForm(string s) {
Text = s;
Left = 0;
Top = FindForm().Height / 2;
Height = FindForm().Height/2;
Width = FindForm().Width;
lb = new ListBox();
lb.Dock = DockStyle.Fill;
this.Controls.Add(lb);
Show();
}

public void Writeln(string s) {
lb.Items.Add(s);
if (Control.ModifierKeys != Keys.Control) lb.TopIndex =
lb.Items.Count - 1;
}
}
}
 
I created the following form for logging.
Howver, messages are not shown. Why ?
How to implement scrolling when writing to last line in login form?

Best guess: you haven't added the Label control to the LoginForm's control
collection. So it's not part of the LoginForm and so doesn't get redrawn
when the form is drawn.

As far as scrolling goes, you can do it a couple of ways. Using the
method you're showing here, you will need to shift all the Text properties
once you run out of room for the Label instances. That is, if creating a
new Label instance would result in a Label instance that is past the
bottom of your form, instead of creating a new Label instance go through
all the existing ones, setting the Text property of the older Label
instance to that of the next-newest one.

If you do this, you should keep track of the Label instances you create,
in the order in which they were created. A List<Label> instance would be
fine for this, using the Add() method to add a new Label each time. You
can retrieve the Labels from the LoginForm's Controls collection, and in
fact they are likely to be in a consistent order. But I wouldn't rely on
it, since as far as I can recall, the order is not documented as reliable.

You could, of course, sort them according to position to determine order,
but IMHO it's easier just to keep a list.

A second way would be to not add a new Label control each time, but rather
to simply have a TextBox to which you add text. You can use the Lines
property to get and set the text in the TextBox using a string array.
Once you reach the maximum number of lines you want to support, simply
remove the first line when you add a new one.

Pete
 
* Andrus wrote, On 23-7-2007 18:51:
thank you.
i tried listbox and this seems to solve the scolling issue.

Is this code best way to create scrolling logging window ?

I'm not sure what FindForm() does exactly, but calling it 3 times in a
row might be a little overdone.

I'd replace that by:

Form foundForm = FindForm();
Top = foundForm .Height / 2;
Height = foundForm .Height/2;
Width = foundForm .Width;

Jesse
Andrus.

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

class main {
[STAThreadAttribute()]
public static void Main() {

LoginForm f = new LoginForm("Performing task x");

for (int i = 0; i <= 100; i++) {
f.Writeln("Login message " + i.ToString());
}
MessageBox.Show("");

}

class LoginForm : Form {

ListBox lb;

public LoginForm(string s) {
Text = s;
Left = 0;
Top = FindForm().Height / 2;
Height = FindForm().Height/2;
Width = FindForm().Width;
lb = new ListBox();
lb.Dock = DockStyle.Fill;
this.Controls.Add(lb);
Show();
}

public void Writeln(string s) {
lb.Items.Add(s);
if (Control.ModifierKeys != Keys.Control) lb.TopIndex =
lb.Items.Count - 1;
}
}
}
 

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

Back
Top