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
254 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 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now