Problem Creating ListView Items - updated

G

garyusenet

I have had no replies to my previous post so perhaps I didn't write it
good enough.
Please excuse new thread but i wanted to break from the last thread
hopefully this thread will be better.

Very simple. I would like to create listviewitem's for display in a
listview control.

The listview items need to contain properties from Internet Explorer
windows i've managed to collect into an arraylist.

This is the product of many different answers to different questions in
these forums. I dont really understand arraylists but the code is
working now except for the creation of the listview items. I can't seem
to create them. TIA. Gary.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SHDocVw;

namespace InternetExplorerInterface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ArrayList ar = new ArrayList();
foreach (InternetExplorer ie in new ShellWindows())
{
Int32 iLocation = ie.FullName.IndexOf("iexplore");
if (iLocation >= 0) ar.Add(ie);

}

foreach (InternetExplorer ie in ar)
{
int i = 0;
ListViewItem ("item" + i) = new
ListViewItem(ie.LocationURL, 0);
i++;
}

}

}
}
 
R

robb.andrew

When populating a ListView collection at runtime I often use code
similar to this:

ArrayList results = new ArrayList();

results.Add(new Person("1", "John", "Smith", 29,
"Madrid"));
results.Add(new Person("2", "John", "Smith", 29,
"Madrid"));
results.Add(new Person("3", "John", "Smith", 29,
"Madrid"));
results.Add(new Person("4", "John", "Smith", 29,
"Madrid"));
results.Add(new Person("5", "John", "Smith", 29,
"Madrid"));
results.Add(new Person("6", "John", "Smith", 29,
"Madrid"));


foreach(Person person in results)
{
ListViewItem item = new ListViewItem(person.ID);
item.SubItems.Add(person.FirstName);
item.SubItems.Add(person.LastName);
item.SubItems.Add(person.Age.ToString());
item.SubItems.Add(person.BirthPlace);

this.listView1.Items.Add(item);
}

Not sure if it helps...

Cheers
Robb
 
S

Stephany Young

Give people a chance Gary! Your earlier posts on this subject were only made
less than 4 hours before this one. People might be having a meal, watching a
movie, or (horrors of horrors) getting some sleep!

Seeing as you have provided nowhere enough information we have to play 20
questions.

What is 'InternetExplorer'? One can assume it's a class of some description
but what properties does it have that you wish to see in the ListView?

We can see that you are attempting to create a ListViewItem but what do you
want to do with it once you have created it? E.g. There is no point in
creating a swag of ListViewItems that go out of scope immediately.

You need to decide what 'columns' you want to see in the ListView and then
decide which property of 'InternetExplorer' provides the piece of
information for each column. Then and only then can you create the
ListViewItem which, of course you need to add to the ListView.
 
G

garyusenet

Hi sorry for if my post was bad.

What I am doing is this. I am using SHDocVw to give me access to
Internet Explorer windows that are open on my computer. I don't
understand why SHDocVw allows me to do this, but following examples on
the internet I have managed to get it working.

This is the heart of the programme I have written so far: -

ArrayList ar = new ArrayList();
foreach (InternetExplorer ie in new ShellWindows())
{
Int32 iLocation = ie.FullName.IndexOf("iexplore");
if (iLocation >= 0) ar.Add(ie);

}

When a new ShellWindows() is created it somehow (i don't understand
how) builds a list of open windows on the computer. I then use a
foreach to isolate those windows that are of type InternetExplorer
(again i don't understand the type, but have managed to get it to
work).

However not all windows of type InternetExplorer are actually internet
explorer windows, for instance the 'search' window in XP is of type
internet explorer. However by using the property: -

ie.FullName.IndexOf

I can isolate only the 'internet explorer' windows. The foreach loop
builds an array list of these internet explorer windows. I don't
understand arraylists (i have just about got to grips with one
dimensional arrays!)

So I have an arraylist with all my open internet explorer windows.

Now I hope I have explained better the background. To the heart of the
problem I hope you can all help with. I now want to display a list of
all open Internet Explorer windows to the user. The user then decides
which window they would like to work with (so i just want a variable
returned which uniquely references a particular window, so I know which
one the user wants to work with.) My first thoughts are that the
Titlebar text, and the URL of the window in question would suffice to
distinguish between open windows. And so I need a way of presenting
these two pieces of information to the user for every open window, and
then allowing the user to select one of these windows to work with.
Following Stephany's very kind and helpful post in windowsforms, it
would seem that the listview control is the best way of doing this.

So my columns would look something like this.

Window Number | Titlebar Text | URL address from address bar
----------------------------------------------------------------------------------------------

Titlebar Text is accessed from the property: ie.LocationName
Current URL is accessed from the property: ie.LocationURL

I have two problems. The first problem I have is creating the listview
control. I have found some code that shows me how to create a listview
control in code, but it dimensions it. I am thinking I would need it to
be automatically dimensioned to allow for varying numbers of windows
that it needs to display. The second and more daunting problem I have
is i do not know how to populate the Listview control with the two
pieces of information (LocationName, and Location URL) from my
arraylist.

As my listview code doesn't work i've deleted it from my programme. So
my programme as posted below is functioning in itself, and is quite
succinct. I hope someone can spare the time to help me with this.

I hope I have provided enough information now, If anyone needs any more
information, please just ask.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SHDocVw;

namespace InternetExplorerInterface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ArrayList ar = new ArrayList();
foreach (InternetExplorer ie in new ShellWindows())
{
Int32 iLocation = ie.FullName.IndexOf("iexplore");
if (iLocation >= 0) ar.Add(ie);
}


}
}
}
 
S

Stephany Young

Gary!

The ListView is a Windows control that you drag from the toolboz and drop it
on your form. Once you have done that you can size it to however you want
it.

Once you have done that you nedd to set it's View property to "Details". Now
you can create 3 columns (or more correctly ColumnHeaders) in you ListView.

If you haven't got a window number in your InternetExplorer object then I
would suggest that you don't even attempt to try and create one, because you
don't really need one anyway.

To add the information at runtime it is then matter of adding ListViewItems.
Each ListViewItem will equate to a row in the ListView which in turn will
equate to a single element of your Arraylist of InternetExplorer objects.

foreach (InternetExplorer ie in ar)
{
listView1.Items.Add(New ListViewItem(new string[] {ie.LocationName,
ie.LocationURL}))
}

I strongly recommend that you read the documentation on the ListView class
and control and the ListViewItem class. All you need to know is demonstrated
in the included examples.
 
G

garyusenet

Hi Stephany I tried that but it's not working.

It says ' missing ) '

I have:

foreach (InternetExplorer ie in ar)
{

listView1.Items.Add(New ListViewItem(new string[]
{ie.LocationName, ie.LocationURL})) ;
}
Gary!

The ListView is a Windows control that you drag from the toolboz and drop it
on your form. Once you have done that you can size it to however you want
it.

Once you have done that you nedd to set it's View property to "Details". Now
you can create 3 columns (or more correctly ColumnHeaders) in you ListView.

If you haven't got a window number in your InternetExplorer object then I
would suggest that you don't even attempt to try and create one, because you
don't really need one anyway.

To add the information at runtime it is then matter of adding ListViewItems.
Each ListViewItem will equate to a row in the ListView which in turn will
equate to a single element of your Arraylist of InternetExplorer objects.

foreach (InternetExplorer ie in ar)
{
listView1.Items.Add(New ListViewItem(new string[] {ie.LocationName,
ie.LocationURL}))
}

I strongly recommend that you read the documentation on the ListView class
and control and the ListViewItem class. All you need to know is demonstrated
in the included examples.



Hi sorry for if my post was bad.

What I am doing is this. I am using SHDocVw to give me access to
Internet Explorer windows that are open on my computer. I don't
understand why SHDocVw allows me to do this, but following examples on
the internet I have managed to get it working.

This is the heart of the programme I have written so far: -

ArrayList ar = new ArrayList();
foreach (InternetExplorer ie in new ShellWindows())
{
Int32 iLocation = ie.FullName.IndexOf("iexplore");
if (iLocation >= 0) ar.Add(ie);

}

When a new ShellWindows() is created it somehow (i don't understand
how) builds a list of open windows on the computer. I then use a
foreach to isolate those windows that are of type InternetExplorer
(again i don't understand the type, but have managed to get it to
work).

However not all windows of type InternetExplorer are actually internet
explorer windows, for instance the 'search' window in XP is of type
internet explorer. However by using the property: -

ie.FullName.IndexOf

I can isolate only the 'internet explorer' windows. The foreach loop
builds an array list of these internet explorer windows. I don't
understand arraylists (i have just about got to grips with one
dimensional arrays!)

So I have an arraylist with all my open internet explorer windows.

Now I hope I have explained better the background. To the heart of the
problem I hope you can all help with. I now want to display a list of
all open Internet Explorer windows to the user. The user then decides
which window they would like to work with (so i just want a variable
returned which uniquely references a particular window, so I know which
one the user wants to work with.) My first thoughts are that the
Titlebar text, and the URL of the window in question would suffice to
distinguish between open windows. And so I need a way of presenting
these two pieces of information to the user for every open window, and
then allowing the user to select one of these windows to work with.
Following Stephany's very kind and helpful post in windowsforms, it
would seem that the listview control is the best way of doing this.

So my columns would look something like this.

Window Number | Titlebar Text | URL address from address bar
----------------------------------------------------------------------------------------------

Titlebar Text is accessed from the property: ie.LocationName
Current URL is accessed from the property: ie.LocationURL

I have two problems. The first problem I have is creating the listview
control. I have found some code that shows me how to create a listview
control in code, but it dimensions it. I am thinking I would need it to
be automatically dimensioned to allow for varying numbers of windows
that it needs to display. The second and more daunting problem I have
is i do not know how to populate the Listview control with the two
pieces of information (LocationName, and Location URL) from my
arraylist.

As my listview code doesn't work i've deleted it from my programme. So
my programme as posted below is functioning in itself, and is quite
succinct. I hope someone can spare the time to help me with this.

I hope I have provided enough information now, If anyone needs any more
information, please just ask.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SHDocVw;

namespace InternetExplorerInterface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ArrayList ar = new ArrayList();
foreach (InternetExplorer ie in new ShellWindows())
{
Int32 iLocation = ie.FullName.IndexOf("iexplore");
if (iLocation >= 0) ar.Add(ie);
}


}
}
}
 
S

Stephany Young

1. Make sure that all the the ( and ) match.

2. Press F1 and check the actual syntax for the ListView.Items.Add method.

3. Learn some basic programming skills before you start trying to do
something as complex as you are attempting.


Hi Stephany I tried that but it's not working.

It says ' missing ) '

I have:

foreach (InternetExplorer ie in ar)
{

listView1.Items.Add(New ListViewItem(new string[]
{ie.LocationName, ie.LocationURL})) ;
}
Gary!

The ListView is a Windows control that you drag from the toolboz and drop
it
on your form. Once you have done that you can size it to however you want
it.

Once you have done that you nedd to set it's View property to "Details".
Now
you can create 3 columns (or more correctly ColumnHeaders) in you
ListView.

If you haven't got a window number in your InternetExplorer object then
I
would suggest that you don't even attempt to try and create one, because
you
don't really need one anyway.

To add the information at runtime it is then matter of adding
ListViewItems.
Each ListViewItem will equate to a row in the ListView which in turn will
equate to a single element of your Arraylist of InternetExplorer objects.

foreach (InternetExplorer ie in ar)
{
listView1.Items.Add(New ListViewItem(new string[] {ie.LocationName,
ie.LocationURL}))
}

I strongly recommend that you read the documentation on the ListView
class
and control and the ListViewItem class. All you need to know is
demonstrated
in the included examples.



Hi sorry for if my post was bad.

What I am doing is this. I am using SHDocVw to give me access to
Internet Explorer windows that are open on my computer. I don't
understand why SHDocVw allows me to do this, but following examples on
the internet I have managed to get it working.

This is the heart of the programme I have written so far: -

ArrayList ar = new ArrayList();
foreach (InternetExplorer ie in new ShellWindows())
{
Int32 iLocation = ie.FullName.IndexOf("iexplore");
if (iLocation >= 0) ar.Add(ie);

}

When a new ShellWindows() is created it somehow (i don't understand
how) builds a list of open windows on the computer. I then use a
foreach to isolate those windows that are of type InternetExplorer
(again i don't understand the type, but have managed to get it to
work).

However not all windows of type InternetExplorer are actually internet
explorer windows, for instance the 'search' window in XP is of type
internet explorer. However by using the property: -

ie.FullName.IndexOf

I can isolate only the 'internet explorer' windows. The foreach loop
builds an array list of these internet explorer windows. I don't
understand arraylists (i have just about got to grips with one
dimensional arrays!)

So I have an arraylist with all my open internet explorer windows.

Now I hope I have explained better the background. To the heart of the
problem I hope you can all help with. I now want to display a list of
all open Internet Explorer windows to the user. The user then decides
which window they would like to work with (so i just want a variable
returned which uniquely references a particular window, so I know which
one the user wants to work with.) My first thoughts are that the
Titlebar text, and the URL of the window in question would suffice to
distinguish between open windows. And so I need a way of presenting
these two pieces of information to the user for every open window, and
then allowing the user to select one of these windows to work with.
Following Stephany's very kind and helpful post in windowsforms, it
would seem that the listview control is the best way of doing this.

So my columns would look something like this.

Window Number | Titlebar Text | URL address from address bar
----------------------------------------------------------------------------------------------

Titlebar Text is accessed from the property: ie.LocationName
Current URL is accessed from the property: ie.LocationURL

I have two problems. The first problem I have is creating the listview
control. I have found some code that shows me how to create a listview
control in code, but it dimensions it. I am thinking I would need it to
be automatically dimensioned to allow for varying numbers of windows
that it needs to display. The second and more daunting problem I have
is i do not know how to populate the Listview control with the two
pieces of information (LocationName, and Location URL) from my
arraylist.

As my listview code doesn't work i've deleted it from my programme. So
my programme as posted below is functioning in itself, and is quite
succinct. I hope someone can spare the time to help me with this.

I hope I have provided enough information now, If anyone needs any more
information, please just ask.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SHDocVw;

namespace InternetExplorerInterface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ArrayList ar = new ArrayList();
foreach (InternetExplorer ie in new ShellWindows())
{
Int32 iLocation = ie.FullName.IndexOf("iexplore");
if (iLocation >= 0) ar.Add(ie);
}


}
}
}
 
G

garyusenet

Stephanies code has proved of no use as it has generated compile time
errors.
So i'm still looking for a solution if someone would care to offer one!

TIA.

Gary.

Stephany said:
1. Make sure that all the the ( and ) match.

2. Press F1 and check the actual syntax for the ListView.Items.Add method.

3. Learn some basic programming skills before you start trying to do
something as complex as you are attempting.


Hi Stephany I tried that but it's not working.

It says ' missing ) '

I have:

foreach (InternetExplorer ie in ar)
{

listView1.Items.Add(New ListViewItem(new string[]
{ie.LocationName, ie.LocationURL})) ;
}
Gary!

The ListView is a Windows control that you drag from the toolboz and drop
it
on your form. Once you have done that you can size it to however you want
it.

Once you have done that you nedd to set it's View property to "Details".
Now
you can create 3 columns (or more correctly ColumnHeaders) in you
ListView.

If you haven't got a window number in your InternetExplorer object then
I
would suggest that you don't even attempt to try and create one, because
you
don't really need one anyway.

To add the information at runtime it is then matter of adding
ListViewItems.
Each ListViewItem will equate to a row in the ListView which in turn will
equate to a single element of your Arraylist of InternetExplorer objects.

foreach (InternetExplorer ie in ar)
{
listView1.Items.Add(New ListViewItem(new string[] {ie.LocationName,
ie.LocationURL}))
}

I strongly recommend that you read the documentation on the ListView
class
and control and the ListViewItem class. All you need to know is
demonstrated
in the included examples.



Hi sorry for if my post was bad.

What I am doing is this. I am using SHDocVw to give me access to
Internet Explorer windows that are open on my computer. I don't
understand why SHDocVw allows me to do this, but following examples on
the internet I have managed to get it working.

This is the heart of the programme I have written so far: -

ArrayList ar = new ArrayList();
foreach (InternetExplorer ie in new ShellWindows())
{
Int32 iLocation = ie.FullName.IndexOf("iexplore");
if (iLocation >= 0) ar.Add(ie);

}

When a new ShellWindows() is created it somehow (i don't understand
how) builds a list of open windows on the computer. I then use a
foreach to isolate those windows that are of type InternetExplorer
(again i don't understand the type, but have managed to get it to
work).

However not all windows of type InternetExplorer are actually internet
explorer windows, for instance the 'search' window in XP is of type
internet explorer. However by using the property: -

ie.FullName.IndexOf

I can isolate only the 'internet explorer' windows. The foreach loop
builds an array list of these internet explorer windows. I don't
understand arraylists (i have just about got to grips with one
dimensional arrays!)

So I have an arraylist with all my open internet explorer windows.

Now I hope I have explained better the background. To the heart of the
problem I hope you can all help with. I now want to display a list of
all open Internet Explorer windows to the user. The user then decides
which window they would like to work with (so i just want a variable
returned which uniquely references a particular window, so I know which
one the user wants to work with.) My first thoughts are that the
Titlebar text, and the URL of the window in question would suffice to
distinguish between open windows. And so I need a way of presenting
these two pieces of information to the user for every open window, and
then allowing the user to select one of these windows to work with.
Following Stephany's very kind and helpful post in windowsforms, it
would seem that the listview control is the best way of doing this.

So my columns would look something like this.

Window Number | Titlebar Text | URL address from address bar
----------------------------------------------------------------------------------------------

Titlebar Text is accessed from the property: ie.LocationName
Current URL is accessed from the property: ie.LocationURL

I have two problems. The first problem I have is creating the listview
control. I have found some code that shows me how to create a listview
control in code, but it dimensions it. I am thinking I would need it to
be automatically dimensioned to allow for varying numbers of windows
that it needs to display. The second and more daunting problem I have
is i do not know how to populate the Listview control with the two
pieces of information (LocationName, and Location URL) from my
arraylist.

As my listview code doesn't work i've deleted it from my programme. So
my programme as posted below is functioning in itself, and is quite
succinct. I hope someone can spare the time to help me with this.

I hope I have provided enough information now, If anyone needs any more
information, please just ask.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SHDocVw;

namespace InternetExplorerInterface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ArrayList ar = new ArrayList();
foreach (InternetExplorer ie in new ShellWindows())
{
Int32 iLocation = ie.FullName.IndexOf("iexplore");
if (iLocation >= 0) ar.Add(ie);
}


}
}
}
 
G

ghytred

Stephanie's code is correct. What you need is to get yourself on a
beginners' training course.

This group is not staffed by people paid to help you learn (not unless
you've paid some dosh to Microsoft in which case MS staff will answer
your questions).
Most people on this group read it to learn or to find answers to
immediate questions - but not as beginners. I would guess that over 90%
of people posting anything here (questions included) are professional
programmers helping out their peers. However, this is not the correct
place for a beginner to learn how to program - and from your questions
your are very obviously a beginner.
In addition to that, you are rude. That's not a good formula to get
help.

My best advice to you is to get yourself on a course - there are many
about. Also get a book. There are many for C#. Your questions show that
you don't know enough to ask sensible questions about general things,
and when given specific answers you don't understand them.

I'm not trying to knock you, but programming - or at least producing
good code - is hard work. You must learn it properly.

Just as an exercise, take Stephanie's code and use F1 to look up the
constructor for ListViewItem and try to work out what her code is
doing.


Good luck!


Stephanies code has proved of no use as it has generated compile time
errors.
So i'm still looking for a solution if someone would care to offer one!

TIA.

Gary.

Stephany said:
1. Make sure that all the the ( and ) match.

2. Press F1 and check the actual syntax for the ListView.Items.Add method.

3. Learn some basic programming skills before you start trying to do
something as complex as you are attempting.


Hi Stephany I tried that but it's not working.

It says ' missing ) '

I have:

foreach (InternetExplorer ie in ar)
{

listView1.Items.Add(New ListViewItem(new string[]
{ie.LocationName, ie.LocationURL})) ;
}

Gary!
 
G

ghytred

Stephanie's code is correct. What you need is to get yourself on a
beginners' training course.

This group is not staffed by people paid to help you learn (not unless
you've paid some dosh to Microsoft in which case MS staff will answer
your questions).
Most people on this group read it to learn or to find answers to
immediate questions - but not as beginners. I would guess that over 90%
of people posting anything here (questions included) are professional
programmers helping out their peers. However, this is not the correct
place for a beginner to learn how to program - and from your questions
your are very obviously a beginner.
In addition to that, you are rude. That's not a good formula to get
help.

My best advice to you is to get yourself on a course - there are many
about. Also get a book. There are many for C#. Your questions show that
you don't know enough to ask sensible questions about general things,
and when given specific answers you don't understand them.

I'm not trying to knock you, but programming - or at least producing
good code - is hard work. You must learn it properly.

Just as an exercise, take Stephanie's code and use F1 to look up the
constructor for ListViewItem and try to work out what her code is
doing.


Good luck!


Stephanies code has proved of no use as it has generated compile time
errors.
So i'm still looking for a solution if someone would care to offer one!

TIA.

Gary.

Stephany said:
1. Make sure that all the the ( and ) match.

2. Press F1 and check the actual syntax for the ListView.Items.Add method.

3. Learn some basic programming skills before you start trying to do
something as complex as you are attempting.


Hi Stephany I tried that but it's not working.

It says ' missing ) '

I have:

foreach (InternetExplorer ie in ar)
{

listView1.Items.Add(New ListViewItem(new string[]
{ie.LocationName, ie.LocationURL})) ;
}

Gary!
 
G

garyusenet

Life is too short to argue with you. The code is not correct. I have
been on a beginners training course. If you have no constructive input
to a thread do not participate.
 

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