none
Call class failed RRS feed

  • Question

  • I've got an app, that is writing and reading from a text file(Name, teacher, grade, years). The output(read text) goes into a listbox. But when I run the program, nothing is shown in the listbox. I've been trying differents methods, even ask here a question but it's still not working.

    Here is the class:

    namespace TextFiles_2
    {
        public class Class1
        {
            public string[] ReadText()
            {
                var fileName = @"C:\Users\user\Desktop\Document.TXT";
                if (File.Exists(fileName))
                {
                    return File.ReadAllLines(@"C:\Users\user\Desktop\Document.TXT");
                }
                else
                {
                    return new string[] { };
                }
            }
    
        }
    }
    

    And here I call the method:

            public void userListBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                var class1 = new Class1();
                userListBox.Items.Add(class1.ReadText());
            }
    

    This is my last try from last night, that failed. If you have any ideea why this isn't working, please let me know. Thanks!

    Monday, October 14, 2019 11:45 AM

Answers

  • Hi horatiu_alex,

    Thank you for posting here.

    According to your description, you want to load a text file to listbox.

    If you want to add one Item to listbox, you can use userListBox.Items.Add(), but in your code, the result of class1.ReadText() has more than one string, so you can use the following two ways to get it.

    First:

          var class1 = new Class1();
          foreach (var item in class1.ReadText())
          {
               userListBox.Items.Add(item);
          }
    

    Second:

          var class1 = new Class1();
          userListBox.Items.AddRange(class1.ReadText());
    

    Result:

    Hope my solution could be helpful.

    Best regards,
    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by horatiu_alex Thursday, October 17, 2019 2:50 PM
    Tuesday, October 15, 2019 8:37 AM

All replies

  • I assume there is file and that it has lines?

    You need to add item one by one to ListBox by iterating result of ReadText and call Add method with each. Or then there might by AddRange method that adds multiple items.

    Monday, October 14, 2019 12:13 PM
  • Take a look at the patterns in your class1.. you use two string literals..

    Without knowing the purpose of this exercise, I would add the possibility to show more information in the UI. E.g.

    namespace TextFiles_2
    {
        public class Class1
        {
            public bool ReadText(string fileName, List​Box listBox)
            {
    			bool result = false;
                if (File.Exists(fileName))
                {
                    listBox.Items.Add(File.ReadAllLines(fileName));
    				result = true;				
                }
    			
    			return result;
            }
        }
    }
    
    public void userListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
    	Class1 class1 = new Class1();
    	string result = class1.ReadText((@"C:\Users\user\Desktop\Document.TXT", userListBox) ? "data loaded" : "data not loaded";
    	MessageBox.Show(result);
    }

    Monday, October 14, 2019 12:15 PM
  • Oh, this is a great ideea!

    I have something like this:

    Alexander, Mr.Brown,8,15

    John,Ms.Karren,9,17

    Monday, October 14, 2019 12:40 PM
  • Hello,

    This is the perfect opportunity to set a breakpoint on the following line

    var fileName = @"C:\Users\user\Desktop\Document.TXT";

    When the debugger stops on that line, step through each line, does it go to the file exist part or the file does not exists part of the if condition?

    Also, verify the file has lines.

    Bottom line is learning how to debug will benefit you a good deal as you learn to code.

    Also a slight code change.

    namespace TextFiles_2
    {
        public class Class1
        {
            public string[] ReadText()
            {
                var fileName = @"C:\Users\user\Desktop\Document.TXT";
                if (File.Exists(fileName))
                {
                    return File.ReadAllLines(fileName);
                }
                else
                {
                    return new string[] { };
                }
            }
    
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, October 14, 2019 12:44 PM
    Moderator
  • Hey Keren, thanks for the tip. I tried it and it reads the file. But it's content is not in the listbox
    Monday, October 14, 2019 2:02 PM
  • This means, that your question is answered: Your class works.

    Then the next step is to look at your ListBox.Items.Add call. Especially you need to look at the existing add methods, namely Add(Object) and Add​Range(Object[]). Maasam has this also already pointed out.

    Monday, October 14, 2019 3:34 PM
  • Hi horatiu_alex,

    Thank you for posting here.

    According to your description, you want to load a text file to listbox.

    If you want to add one Item to listbox, you can use userListBox.Items.Add(), but in your code, the result of class1.ReadText() has more than one string, so you can use the following two ways to get it.

    First:

          var class1 = new Class1();
          foreach (var item in class1.ReadText())
          {
               userListBox.Items.Add(item);
          }
    

    Second:

          var class1 = new Class1();
          userListBox.Items.AddRange(class1.ReadText());
    

    Result:

    Hope my solution could be helpful.

    Best regards,
    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by horatiu_alex Thursday, October 17, 2019 2:50 PM
    Tuesday, October 15, 2019 8:37 AM