Add Items to Listview

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Add Items to Listview

 
0
  #1
Sep 20th, 2009
I want to convert VB.Net to C# code

VB.NET-
  1. Public Class Form1
  2.  
  3. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4. Try
  5. Dim lCount As Integer
  6.  
  7. For lCount = 1 To 5
  8. ListView1.Items.Add(lCount.ToString)
  9. Next
  10. Catch ex As Exception
  11. MsgBox(ex.Message)
  12. End Try
  13. End Sub
  14.  
  15. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  16. Try
  17. Dim lCount As Integer
  18. For lCount = 0 To ListView1.Items.Count
  19. MsgBox(ListView1.Items(lCount).Text)
  20. Next
  21.  
  22. Catch ex As Exception
  23. MsgBox(ex.Message)
  24. End Try
  25. End Sub
  26. End Class

C#
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. int lcount;
  4. for (lcount = 0; lcount <= 5;lcount ++ )
  5. {
  6. listView1.Items.Add( lcount.ToString);
  7. }
  8.  
  9. }

I m getting Errors
Error 1 The best overloaded method match for 'System.Windows.Forms.ListView.ListViewItemCollection.Add(string)' has some invalid arguments

Error 2 Argument '1': cannot convert from 'method group' to 'string'
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 940
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 154
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: Add Items to Listview

 
0
  #2
Sep 20th, 2009
You left off the parenthesis: lcount.ToString() should eliminate that error message.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,710
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 483
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Add Items to Listview

 
0
  #3
Sep 20th, 2009
Use parenthesis.
  1. listView1.Items.Add(lcount.ToString());
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 940
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 154
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: Add Items to Listview

 
0
  #4
Sep 20th, 2009
Your next loop should look like:
  1. for (int lCount = 0; lCount < ListView1.Items.Count; lCount++)
  2. MessageBox.Show(ListView1.Items[lCount].Text)

to replace:
  1. Dim lCount As Integer
  2. For lCount = 0 To ListView1.Items.Count
  3. MsgBox(ListView1.Items(lCount).Text)
  4. Next


NOTE: Not sure how VB handles declaration of iterator inside the loop ( lCount (maybe you can't), but in C# the variable lCount (as I demonstrated it) will no longer be in scope outside the confines of the loop block (single statement in this case).
Last edited by DdoubleD; Sep 20th, 2009 at 11:46 am. Reason: added NOTE
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC