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 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.
 
I taught guitar for years, what I didn't expect my students to do was go find 15 pieces of information in 25 different places.

When I taught something, I explained what it was, why it was done, where it came from, and how to implement it and then pointed them in the direction of more information should they want it.

Yes, I know it is all out there, but my point was that having a tutorial that simple shows you x + y = 4 is not really that useful unless you needed exactly x + y = 4. Without knowing the value of x, why x, why is x first etc. The example isn't that useful.

Hence my point that it will take a lot of digging into to get to grips with it all and that I am not finding that particular brand of tutorial very useful.

Also, MSDN is a very very poor educational tool. It may be a fantastic tool for developers to hone their skills and learn new skills, but as a learning platform? It is horrendous. It goes from having a page with barely anything on it, for one aspect, to being an intensely dense text for another - with really as far as I can tell, no direction to it at all. It is like throwing sheet music at a blind person and expecting them to learn music from it.

I will get to grips with this, but without a good learning tool that contains all the information you need with examples and explanations...it is going to take longer.
 
Status
Not open for further replies.
Back
Top Bottom