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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
Achieve json result 2 64
Syntax error 9 45
What .NET URL re-routing tool did I use? 2 31
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 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

22 Experts available now in Live!

Get 1:1 Help Now