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
265 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 500 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

Technology Partners: 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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

752 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