Resolved List <> to Text Box - working but terrible code - suggestions welcome

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
I have a small application in Win Form, that takes data entered by a user and places it into a comma separated text file.

I want to be able to load that data back into the application when it is started on the next occasion.

The data is taken from 5 text boxes which are generated by the user and the program. So the txt file looks like this:

123, 12345, ABC ABC, 00:00:10, ABC ABC
124, 12345, ABC ABC, 00:00:15, ABC ABC

The values change depending on what is entered by the user - however, only the FIRST entry is required to be unique - the others can and often will be repeats.

So I have used:

C#:
List<string> lines = File.ReadAllLines("../../../txtFile.txt").ToList();

To create a list, and then used the following code to decide how to proceed:

C#:
int sizeoflist;
sizeoflist = lines.Count();

Once I have the count, I have then written a number of IF statements that follow this logic:

C#:
if (sizeoflist == 1)
            {
                string[] entries = lines[0].Split(',');
                HoldS1.Text = entries[0].ToString();
                HoldC1.Text = entries[1].ToString();
                HoldP1.Text = entries[2].ToString();
                HoldT1.Text = entries[3].ToString();
                desctbox1.Text = entries[4].ToString();
            }
            else if (sizeoflist == 2)
            {
                string[] entries = lines[0].Split(',');
                string[] entries2 = lines[1].Split(',');

                HoldS1.Text = entries[0].ToString();
                HoldC1.Text = entries[1].ToString();
                HoldP1.Text = entries[2].ToString();
                HoldT1.Text = entries[3].ToString();
                desctbox1.Text = entries[4].ToString();

                HoldS2.Text = entries2[0].ToString();
                HoldC2.Text = entries2[1].ToString();
                HoldP2.Text = entries2[2].ToString();
                HoldT2.Text = entries2[3].ToString();
                desctbox2.Text = entries2[4].ToString();
            }

This works, but it requires me to do this twelve times for twelve sets of text boxes (the last TextBox is HoldS12) - and that means that by the time I get to the last one, I will have an else if statement that is longer than the entire programs code combined.

I am quite new at this and just thought perhaps there was a better way than repeating code over and over again. I mean this works, but I can't imagine it is a good way to do it.
 
Firstly, what's the point of calling ToList when ReadAllLines returns an array and you can just use that?

As for the question, you can just use a for loop and then get the controls to populate by name based on the loop counter:
C#:
var lines = File.ReadAllLines("..\..\..\txtFile.txt");

for (var i = 0; i < lines.Length; i++)
{
    var fields = lines[i].Split(',');
    var controlNumber = i + 1;
   
    Controls($"HoldS{controlNumber}").Text = fields[0];  
    Controls($"HoldC{controlNumber}").Text = fields[1];  
    Controls($"HoldP{controlNumber}").Text = fields[2];  
    Controls($"HoldT{controlNumber}").Text = fields[3];  
    Controls($"desctbox{controlNumber}").Text = fields[4];
}
Note that String.Split returns a String array so there's no point to calling ToString on its elements. Just be aware that, with this code, you won't clear the other controls if they don't get populated from the file. If you want that to happen then you'd need to do a bit more work.
 
Thank you for taking the time to reply - it is actually really appreciated.

The whole ToList / split thing was trying to get them into objects as I thought that might be a better way to manage the data, having a HoldS object list I thought might be easier to populate the textboxes - but that didn't really pan out and I had not removed the backbone of that.

Thank you for the code, the other controls should be blank on load, so if they remain blank that is absolutely fine.
 
I do have the same issue that I have been having while trying this myself. I have tried this.Controls and various other pieces of code Controls.Find etc.

C#:
Controls($"HoldS{controlNumber}").Text = fields[0];

This shows Controls as an error.

"C# Non-invocable member cannot be used like a method."

It provides no option for any "using System. " and I feel like perhaps I am missing something I should be doing before this to make this work.

I had this problem when trying to use controls before, but I also noticed that when using C# WPF - you can't use Timer - which you can use in WinForm - so I thought maybe it was because Controls was a similar problem, you can use in C# WPF but not in WinForm.

But now you have posted it in the WinForm area, I am thinking that maybe I am missing a key piece that I need to put in before this will work?
 
Controls(index) should have been Controls[index]
 
Controls(index) should have been Controls[index]

Ahh thank you! For anyone in future, do remember if you put your TextBoxes in a panel - as that will save you a good 20 minutes of fraught worry...
 
Last edited:
Controls(index) should have been Controls[index]
Oops! That what you get for writing a lot of VB code as well as C# and typing code straight into the forum instead of via an IDE.
 
Oops! That what you get for writing a lot of VB code as well as C# and typing code straight into the forum instead of via an IDE.

I know how you feel, I wrote this program in VBA in excel but had issues with the spreadsheet interacting with other spreadsheets so moved it to C#.

I didn't post to say that, I posted to say....thank you so much for this code, it is really versatile and I have been able to go through my program and remove literally hundreds of lines of code and replace it with variations of your code.

I knew Controls would be useful, but was never able to interact with anyone to nail down its proper working...now I have, it is proving very useful. So thanks again for taking the time to write it up.
 
Take time to browse through the documentation instead of being dependent on Intellisense and autocomplete. You will learn a lot about the various classes and what they offer.
 
Take time to browse through the documentation instead of being dependent on Intellisense and autocomplete. You will learn a lot about the various classes and what they offer.
Note that there's really no excuse for not doing that if you already know the type or member. Context-sensitive Help, which has been a thing in Windows for decades, means that you can click a type or member in code and press F1 to be taken directly to the documentation for that type or member. Even without that, you can still use the Help menu in VS to go to the documentation home page and navigate from there, but I would also recommend that everyone have the documentation API reference home page favourited/bookmarked in their browser.
 
Note that there's really no excuse for not doing that if you already know the type or member. Context-sensitive Help, which has been a thing in Windows for decades, means that you can click a type or member in code and press F1 to be taken directly to the documentation for that type or member. Even without that, you can still use the Help menu in VS to go to the documentation home page and navigate from there, but I would also recommend that everyone have the documentation API reference home page favourited/bookmarked in their browser.

MSDN's documentation page is the densest thing I think I have ever read in my life., and I am a lawyer that studied land law...

I understand why, it is a complex area and they can't afford to go wrong, but as a guide to learning C#? I think it is written for those who have studied it, and either need to learn more or want to remind themselves.

If the times were not so weird, I would have signed up for a night course, but given where we are - I have looked at online courses, but not really found the one that I think I would be happy with just yet.

So learning by doing, experimenting, manipulating, asking questions and grateful for the answers that teach me a bit more.

This isn't my job, it is a hobby, something I am learning because it is fun, interesting and to some degree, useful.
 

Latest posts

Back
Top Bottom