Question Iterating through textbox populating from textfile

Status
Not open for further replies.

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
This had stumped me for awhile, I keep returning to it and feel I get a little closer - but honestly, I could be getting further away.

At the bottom is my winform code - directly below is my WPF code.

This kinda works, it is late, 3.30am and I have not perfectly refined it (it doesn't populate sdTB12 for something (might be < length will look at it closely tomorrow. (Edit, just realised it is because it counts from 0 and therefore ends on sdTB11 - i mentioned it was late right?)

My issue is that this will require me to create a foreach loop over the Controls for each bank of 12 textboxes - as you can see in the winform I was able to do this with a single code block rather than 5 sets of foreach loops each one containing an If statement to identify a textbox within that bank.

Anyone able to point me towards a better way?

WPF Code:

C#:
private void Reload()
        {
            string DS1 = DataSetOne.CombinedName("DataSet1.txt");

            if (File.Exists(DS1))
            {
                var lines = File.ReadAllLines(DS1);
                var lineCount = File.ReadLines(DS1).Count();

                for (var i = 0; i < lineCount; i++)
                {
                    if (lines.Length > 2)
                    {
                        entries = lines[i].Split(',');
                    }
                    foreach (var x in TimeKeeper.Children.OfType<TextBox>())
                    {
                        if (x.Name == $"sdTB{i}")
                        x.Text = entries[0];
                    }
                }
            }

WinForm Code:

C#:
public void Reload()
        {
            if (File.Exists(ctk1.DS1))
            {
                var lines = File.ReadAllLines(ctk1.DS1);
                var lineCount = File.ReadLines(ctk1.DS1).Count();

                for (var i = 0; i < lineCount; i++)
                {
                    if (lines.Length > 2)
                    {
                        var fields = lines[i].Split(',');
                        var controlNumber = i + 1;

                        Controls[$"HoldSD{ controlNumber }"].Text = fields[0];
                        Controls[$"HoldCode{ controlNumber }"].Text = fields[1];
                        Controls[$"HoldProj{ controlNumber }"].Text = fields[2];
                        Controls[$"HoldTime{ controlNumber }"].Text = fields[3];
                        Controls[$"desctbox{ controlNumber }"].Text = fields[4];
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
 
Solution
Jes, how long do you have, and what is your threshold for patience. lol

While MVVM is the way to go in WPF, I do know people who still apply the MVC approach. Its a bit like having your soup with a fork.

MVVM is a pattern that hasn't exactly taken even the experienced developers fancy. Rumours were floating about that Microsoft planned another alternate pattern, but it never materialised.
My issue is that this will require me to create a foreach loop over the Controls for each bank of 12 textboxes
I suggest you start reading up on binding in WPF - and more binding in WPF - and another with some other binding steps. There are endless tutorials on the interwebs for you to follow. In WPF, you setup your main...
I specifically said it was a great tool for developers, but as a learning platform isn't great.
You are simply wrong. MSDN has a getting started guide which I am sure is linked in my signature. And when you get into it, it takes you from the basics into more advanced stages. MSDN is a learning tool, but not if you are using it to skip introductory chapters and only learn what appeals to you. Which is what most new folk do when they are starting out. When people come here to ask where to start with C#. I direct them to MSDN, as it is the best place to learn. Youtube is horseshite, what you learn from that could be 8++ years old. Times change and so does the variations of the C# language as we move with the different cultures released by Microsoft themselves, and these variational changes are published on their own website.

All of the devs on this forum spend countless hours a week advising people on getting into programming as well as troubleshooting their issues, and we use MSDN as a source of documentation to assist those in need of our help. You are the only one developing an issue with this source of material. Most of us have been programming some 20 years. So if anyone is capable of telling someone how to learn or where to start learning in this industry, it will be us, and not the opinion of someone who is to lazy and inept to follow some simple getting started instructions...

I would rather take advice from those who have made careers for themselves in this industry. As we have all worked for major corporations in the programming industry. There is nobody better to learn from, and the admin of this forum; @Neal done a fantastic job hand-picking some of the greatest devs to make this forum the great resource that it is where 90% of questions are resolved within 24 hours. Out of the hundreds of people who use this forum, you are the only one holding a ridiculous defamatory opinion, and a grudge against using MSDN as a source of training and quality learning, while millions of people have come here and benefited from our shared MSDN resources already, and without complaint.

I'm probably the only one here who has not worked for Microsoft directly, while all the other devs have. (afaik..) (so I don't consider myself as talented a dev as they); Yet; I have spent many years working for game development companies, including Electronic Arts, Dice, Punk Buster, amongst others. So I feel I am experienced enough to vice my own opinions and direct advice on the subject of where a newbie should start out in this industry while on their learning quest to better their programming skillsets when using .Net Frameworks. Your opinion is irrelevant as our countless MSDN references have helped thousands of people to-date without complaints from them.

If you don't like Microsoft, why are you using them? Further, if you don't like their learning section, perhaps you should ask them to improve it? Contact - Microsoft Support
We don't just give kids encyclopedias and tell them to have at it. That is ineffective and a poor way to teach. If you don't understand that, perhaps you shouldn't be commenting on teaching.
You're arguing a point I never brought up. And please do not tell me what I should or should-not be commenting on, unless you'd like to be given a brief vacation from the forum. As you are also coming across quite patronising and provocative while trying to substantiate your argument.

We can all agree to disagree. as its a public forum after all, and I respect your opinion while I may not agree with it. We don't restrict people from expressing their opinions on here.
 
I took a day, but it is really hard when you are not reading what I am telling you.

MSDN is NOT a teaching method. It is a resource to assist in teaching. If you can't see the difference between the two, then I don't know how I can explain it.

I tried showing you a video of what actual teaching is, breaking down what you need to know, building on the knowledge step by step in a structured format.

MSDN is a resource, an incredible resource, but if you decide that you are going to learn programming by reading MSDN, it will take you so much longer than actually following a decently taught program.

I do not understand how you think that is a defamatory opinion when it is blindingly obvious.
 
Sometimes, MSDN is the only resource available for learning a new technology because nobody has yet written a book, put together a blogpost, or done a Channel 9 video.

Yes, learning to how to do Windows programming from Petzold's "Programming Windows" book is much easier that dig through the MSDN Win32 API, or learning how to do COM/OLE is much easier with either Craig Brockschmidt's "Inside OLE" or Don Box's "Inside COM" books instead of digging through the MSDN COM/OLE docs because they have distilled and information and restructured it for teaching. But consider that these people had to start off with MSDN before they could do that distillation and restructuring. That means that there was information there for them to learn from.

It's like people saying that Newton's Principia Mathematica is not good enough for learning Calculus.
 
I am not disparaging MSDN as a whole, just for me at this level - I feel there are better routes to gaining the basic understanding - I have just found a set of tutorials on WPF MVVM from start to finish, that I am hoping will give me the structure and guidance I need to get an idea - to grasp the concepts, which will then allow me to expand that understanding through reading of MSDN on points that I need further information on.

Which I think is the perfect way to use MSDN.
 
I am not disparaging MSDN as a whole
It appears you were :
Also, MSDN is a very very poor educational tool.
You branded it "a very very poor educational tool" as a whole, and not parts of it.
just for me at this level
What level? Microsoft have docs for the simple minded novice, and if that's not good enough for you, perhaps you should go back to teaching music instead.

Microsoft provide getting started tutorials which are for absolute beginner developers. They also provide tutorials and a further education section for beginner to intermediate level, and for noobs who want to excel to the next level after taken their beginner courses. Just one of many I can link re MVVM; MSDN Mag also provide great coverage on MVVM : Patterns - WPF Apps With The Model-View-ViewModel Design Pattern

The principle is the same regardless if you are in UWP or WPF.
I have just found a set of tutorials on WPF MVVM from start to finish, that I am hoping will give me the structure and guidance I need to get an idea - to grasp the concepts, which will then allow me to expand that understanding through reading of MSDN on points that I need further information on.
So Microsoft docs suck, except when it suits you to use them. Grand. Park this topic here.
 
Status
Not open for further replies.
Back
Top Bottom