Go Premium for a chance to win a PS4. Enter to Win

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
?
283 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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

916 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