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...
Don't answer this yet....I had an idea as I laid my head down - I can now use the code i have from the Win Form - now I have identified the TextBoxes and loop goes through ALL of the text boxes on the form - so all I have to do is create a similar piece of code to identify each textbox and enter data into it .....

will need some time...but I think I might get there.

Of course, if I am doing it entirely wrong and it will never work, please do let me know save me fruitless hours of trying...
 
You are doing things wrong in WPF if you need to refer to controls by name or ID. That means you are trying to use WPF the WinForms way instead of the WPF MVVM way. The only time you should be referring to controls by name or ID in WPF is if you are creating a custom control and you are wiring up some code behind to implement a behavior that is not achievable via XAML.
 
You are doing things wrong in WPF if you need to refer to controls by name or ID. That means you are trying to use WPF the WinForms way instead of the WPF MVVM way. The only time you should be referring to controls by name or ID in WPF is if you are creating a custom control and you are wiring up some code behind to implement a behavior that is not achievable via XAML

Yeah, but I would like to see it done this side of xmas. I have looked at MVVM, it isn't something you pick up easily, it is a long-term indepth investigation of a rather convoluted method of coding.

I barely know the basics of c#.

It is like telling your 5-year old that their finger painting is wrong because they aren't using renaissance perspective.
 
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 window structure in xaml, and then you can bind the properties of your controls to your models.
 
Last edited:
Solution
Yeah I did start the reading and looking at a few tutorials, I did see the binding done in XAML. I couldn't get it to work but I think that is my failure to understand it at this point.

It does look like it isn't a short path though, I felt learning C#, while there is always more to learn, you could get a handle on some of it with a bit of practice....MVVM it reminds me of learning guitar....you can't strum the guitar at all, it just isn't there but if you keep at it, one day you just can ....there is no gradual learning you don't know, then you do.

I need to find the right resource for me. So many are just do this do that....great, why? How do I take your example and use that to create my own work.

I also work for a living so finding the time to really get into reading deeper will take some time. Though, I am in London so in lockdown with not much else to do.
 
But, you've got all the great bookshops in London. I bet the lockdown sucks bigtime. At least here in the US, they still let us go out and shop.

Anyway, if I've got free time tonight, I'll show a small example of loading that file in using XAML and minimal code behind.
 
How do I take your example and use that to create my own work.
You don't and that's a shitty way to learn to programme anything. Never take an example from anyone without knowing full well how and why their code and example works. The general pragmatic way to learn is to look at what people are telling you to write from their examples, and then study why it works the way it does by debugging your code, especially if you are still on a learning curve. The debugger is your best friend. I have a tutorial in my signature for debugging, and would recommend you read it, regardless of your experience level while using the debugger. I too would draft you an example with some context, but as Skydiver knows, I have an extremely heavy work load to finish by summer time.

It sounds to me, when you say you tried this and that tutorial, example :
So many are just do this do that....great, why?
Well not being a dick here, but the why is all on you. If your understanding of the fundamentals are covered well enough, and you have spent time studying the language and its documentations on the basics, you should be able to read an example and know what the demonstrator is doing in their example.
 
On a side note, since you mention lockdowns.
But, you've got all the great bookshops in London. I bet the lockdown sucks bigtime. At least here in the US, they still let us go out and shop.
The lockdowns are all a load of ********. It was about a virus in December 2019; a virus which has run its course. It's all about control and compliance by corrupt governments now. They've rebranded the flu, and called it covid, while using PCR tests to test for viruses when the PCR test was never designed to be used for virus testing. That's because the PCR is designed to test positive for any genetic material being tested...further when these tests are sampled, they are amplified between 10 and 30 times above the normal limit further eradicating the chances of a negative result being returned. It's the PCR tests that are creating the cases which are creating the models used to justify unlawful lockdowns, and quarantining of healthy people. And I say that confidently after looking into the evidence of this since November 2018 when all this actually outbroke in China. And another thing, its not the china virus. It was made in a UK lab. And this will be remembered as one of the biggest genocidal plans the world has seen.

Had to get that off my chest. Anyway...

The OP doesn't need books. All the required info is readily available on MSDN. Dig in.
 
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.
 
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.
The information is not in 25 different locations. It's in one location called MSDN, and it covers different sections in their respective categories.
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.
That is exactly what the MSDN docs do. Clearly you haven't spent the required time there in order to find this to be true.
x + y = 4
I'd call that a matter of perspective rather than a a lack of understanding. If you know the end result of x + y = 4, you would be able to formulate the answer however you like. What would it matter if x = 3 and y = 1 or any other formula of integers that make up a value of 4?
I am not finding that particular brand of tutorial very useful.
I linked you three of them tutorials. I guarantee you the devs who answer questions on this forum will disagree with you on that. How are they not useful?
Also, MSDN is a very very poor educational tool.
Dude were you drunk when writing this?
It may be a fantastic tool for developers to hone their skills and learn new skills, but as a learning platform? It is horrendous.
That's your opinion against almost every developer out there who uses .Net Frameworks for their application development. Without it, we wouldn't know how anything works... Now you're talking rubbish. But you're entitled to your opinion.
It goes from having a page with barely anything on it, for one aspect, to being an intensely dense text for another
Please, show us an example of what you are talking about as proof before bashing one of the greatest documentation resources on the net for .Net Frameworks?
It is like throwing sheet music at a blind person and expecting them to learn music from it.
Perhaps if you feel that way, you should return to teaching music and leave app development to those who spend the time reading the MSDN resources, and understand 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.
What the hell are you shiteing about, seriously? Now you are just being ridiculous. Clearly we are not on the same page here.

Like yea; because MSDN doesn't provide you example code to follow with well documented articles explaining all the nitty gritty bits involved in their examples... Taken from the page I linked you :

Screenshot_9.jpg

Go back to writing music if you're not willing to read the resources provided.
 
I wasn't talking about the links you supplied, I had not gotten to them yet, I was talking generically about the tutorials that I had found on MVVM.

You say I am talking rubbish about MSDN, but point out that every developer uses it....when I specifically said it was a great tool for developers, but as a learning platform isn't great.

Perhaps we simply disagree on the separation of what constitutes learning, you perhaps feel that this is life-long learning that never ends and even experienced developers are still learning....and I am talking about learning something new for the first time with no prior knowledge.

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.
 
To give you an example of what good teaching is..

This is an 8 hour video, it is something I am about 5 hours into at the moment. It takes it step by step, adding knowledge, demonstrating how the knowledge is used and provides an incredible learning experience and has filled lots of gaps that I had from my own self-teaching...including reading MSDN.

I can use MSDN on the points covered in this video to expand my knowledge - which is what MSDN is great at doing.

This is how you teach, not just having a huge resource - that is useful, but without the structure and order and direction, it is just a lot information.

 
Status
Not open for further replies.
Back
Top Bottom