listview can't work in console mode


J

justdo

Hi, I am writing the unit test for our application. It use "listview
component" in some functions. The following code work fine and get c =
1 in Windows Application mode, but fail in unit test which get c = 0 :

listView2.Items.Add("xxx");
listView2.Items[0].Selected = true;
int c = listView2.SelectedItems.Count;

in unit test, we create listView2 by this way:

class CreateListViews
{
private ListView listview1;
private System.Windows.Forms.ColumnHeader Number;
private System.Windows.Forms.ColumnHeader Start;
private System.Windows.Forms.ColumnHeader End;
private System.Windows.Forms.ColumnHeader Event;
private System.Windows.Forms.ColumnHeader Length;
public ListView GetListViews()
{
listview1 = new System.Windows.Forms.ListView();
this.Number = new System.Windows.Forms.ColumnHeader();
this.Start = new System.Windows.Forms.ColumnHeader();
this.End = new System.Windows.Forms.ColumnHeader();
this.Event = new System.Windows.Forms.ColumnHeader();
this.Length = new System.Windows.Forms.ColumnHeader();
listview1.Columns.AddRange(new
System.Windows.Forms.ColumnHeader[] {
this.Number,
this.Start,
this.End,
this.Event,
this.Length});
return listview1;
}
}

do someone know why, and what wrong?
 
Ad

Advertisements

G

Guest

Hi Justdo,

I also faced a similar problem before. I think it has some sort of a bug. If
you add the control to a parent - if windows forms, then it will work. (eg.
Controls.Add(listView2);). As an alternate, in console application you can
use

Rectangle rect = listView2.Items[0].Bounds;

before getting the count.

HTH

Cheers,
Chester
 
Ad

Advertisements

J

justdo

Hi Chester,

thx for your response, the second method is in effect, but the method
using windows forms is fail, the code as following:

class CreateListViews
{
//added
static Form manageForm = new Form();
private ListView listview1;
private System.Windows.Forms.ColumnHeader Number;
private System.Windows.Forms.ColumnHeader Start;
private System.Windows.Forms.ColumnHeader End;
private System.Windows.Forms.ColumnHeader Event;
private System.Windows.Forms.ColumnHeader Length;
public ListView GetListViews()
{
listview1 = new System.Windows.Forms.ListView();
this.Number = new System.Windows.Forms.ColumnHeader();
this.Start = new System.Windows.Forms.ColumnHeader();
this.End = new System.Windows.Forms.ColumnHeader();
this.Event = new System.Windows.Forms.ColumnHeader();
this.Length = new System.Windows.Forms.ColumnHeader();
listview1.Columns.AddRange(new
System.Windows.Forms.ColumnHeader[] {
this.Number,
this.Start,
this.End,
this.Event,
this.Length});
this.listview1.FullRowSelect = true;
this.listview1.GridLines = true;
this.listview1.LabelEdit = true;
listview1.TabIndex = 0;

//added
manageForm.Controls.Add(listview1);
return listview1;
}
}

Is anything I did wrong?
 

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