Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to add entry for url value and it's description to add it to a list box

Posted on 2006-11-13
5
Medium Priority
?
276 Views
Last Modified: 2010-04-16
Hi,
I need sample example for adding , updating and deleting entries for a list box
please help as soon as possible

Regards
Hatem
0
Comment
Question by:shang3000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 2000 total points
ID: 17928747
AFAIK, You can't do this with a list box.
You need to use a list view or a data grid
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17928772
Example :

this.listView1.View = View.Details;
this.listView1.Columns.Add("URL",(this.listView1.Width/2)-10,HorizontalAlignment.Left);
this.listView1.Columns.Add("Description",(this.listView1.Width/2)-10,HorizontalAlignment.Left);
this.listView1.Items.Add(new ListViewItem(new string[]{"www.google.com","The Google search engine"}));
this.listView1.Items.Add(new ListViewItem(new string[]{"www.yahoo.com","The Yahoo search engine"}));
this.listView1.Items.Add(new ListViewItem(new string[]{"www.dogpile.com","The DogPile search engine"}));
this.listView1.Items.Add(new ListViewItem(new string[]{"www.msn.com","The MSN search engine"}));

To Edit :

this.listView1.Items[0].SubItems[0].Text = "New text first column of item 1";

To Delete :

this.listView1.Items.RemoveAt(1);

Removes the item at the index position of 1.
0
 

Author Comment

by:shang3000
ID: 17929082
Hi,
Thanks for your effort  but  how can get the index of the selected Item on the list view so that I can modify or detete it

your help is highly appriciated and it's very important to me so please reply as soon as possible
Regards
Hatem :-)
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17929099
You can get the selected items in the listview directly or by index :

int[] indices  = this.listView1.SelectedIndices;

or

SelectedListViewItemCollection c = this.listView1.SelectedItems;

If the listview only allows single item collection then you can just get the item at 0 in eitehr collection;

int index = this.listView1.SelectedIndices[0];

or

ListViewItem item =  this.listView1.SelectedItems[0];

If multiple selection is allowed and the collection contains more than one item or index then you have to decide what to do.

this.listView1.Items.Remove(this.listView1.SelectedItems[0]); // will remove the currently selected item in the list.

this.listView1.SelectedItems[0].SubItems[0].Text = "new value"; // will set the text of first column for the selected item to "new value"
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17929106
The listview can have no item selected so both this.listView1.SelectedItems and this.listView1.SelectedIndices can be empty collections and this.listView1.SelectedIndices[0] and  this.listView1.SelectedItems[0] can be null.
If you want to avoid null reference exceptions check for nulls first.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question