locked
VB to C# RRS feed

  • Question

  • User-373571878 posted

    Hello,

    I have a listbox on a Window form (lstRecords).

    If I want to read through each line of the listbox in VB I have this code in a Do loop which I use the index x to get each line:

    I cannot find how to do this in C#, can you help?

    x= 0

    Do

    ListLine = Me.lstRecords.Items(x).ToString

    x = x + 1

    Wednesday, September 6, 2017 8:53 PM

Answers

  • User-373571878 posted

    Thank you for all of your help.

    I think I got it:

      foreach (string ListLine1 in lstRecordsContractsRates.Items)
           {
               ListLine = ListLine1;
           }

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 7, 2017 3:23 PM

All replies

  • User2103319870 posted

    I cannot find how to do this in C#, can you help?

    You can try with the below code

    int x = 0;
                do
                {
                    ListLine = this.lstRecords.Items(x).ToString;
                    x = x + 1;
                }
                //Your while condition here

    Also to convert the codes you can use this code converter : http://converter.telerik.com/

    Wednesday, September 6, 2017 9:02 PM
  • User-335504541 posted

    Hi

    If I want to read through each line of the listbox in VB I have this code in a Do loop which I use the index x to get each line

    I think you could also try to use foreach statement.

    For example:

                string ListLine = "";
                foreach (ListItem i in lstRecords.Items)
                {
                    ListLine += i.ToString();
                }

    Best Regards,

    Billy

    Thursday, September 7, 2017 6:00 AM
  • User-373571878 posted

    Thanks a2h

    I get the same error "Non-Invocable member 'system.windows.forms.listbox.items' cannot be used like a method.

    And it is flagging this: 'Items'

     ListLine = this.lstRecords.Items(x).ToString

    Thursday, September 7, 2017 1:20 PM
  • User-373571878 posted

    Thanks Billy, when I try to use a ForEach I get this error:

    Error 4 The type 'System.Windows.ContentElement' is defined in an assembly that is not referenced. You must add a reference to assembly 'PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. C:\Users\mkapler\Documents\Visual Studio 2012\Projects\IdleRequestAutoEmailCSharp\IdleRequestAutoEmailCSharp\Form1.cs 121 5 IdleRequestAutoEmailCSharp

    So I added the reference to PresentaionCore 4.0 but I still get this error.  Is there are particular using name needed?

    Thanks

    Thursday, September 7, 2017 2:31 PM
  • User753101303 posted

    Hi,

    What if you try Items[x] rather than Items(x) ? C# uses [] for indexing and () for method calls while VB uses () for both.

    Though this is a C# forum, it is part of the ASP.NET site which is for web based applications. You may want to use  https://msdn.microsoft.com/en-us forums if you need further help.

    Thursday, September 7, 2017 2:45 PM
  • User-373571878 posted

    Hello Billy,

    So I recompiled and the reference took and the highlighted error on the foreach went away.

    Upon running the program, the index i is being flagged with this error:

    Unable to cast object of type 'System.String' to type 'System.Windows.Documents.ListItem'.

    Any ideas?

    Thursday, September 7, 2017 2:49 PM
  • User-373571878 posted

    Thanks Patrice

    I did try the brackets. Like this:

      for (int i = 0; i < 10; i++)
           {
               ListLine = lstRecordsContractsRates.Items[i].ToString;           
           }

    But I receive this error:

    Error 1 Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method? 

    Thanks

    Thursday, September 7, 2017 2:53 PM
  • User2103319870 posted

    Error 1 Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method? 

    You are missing () after ToString, Try with below code

     for (int i = 0; i < 10; i++)
                {
                    ListLine = lstRecordsContractsRates.Items[i].ToString();
                }

    Thursday, September 7, 2017 3:17 PM
  • User-373571878 posted

    Thank you for all of your help.

    I think I got it:

      foreach (string ListLine1 in lstRecordsContractsRates.Items)
           {
               ListLine = ListLine1;
           }

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 7, 2017 3:23 PM