Question How do I make a program that would load data from comboBox selection?

Wowywow

Member
Joined
Nov 9, 2019
Messages
16
Programming Experience
Beginner
Right, so I've been trying to figure out this exercise for a few days now and I really can't come up with anything, just the pure basics.
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_Click(object sender, EventArgs e)
        {
            string filePath = System.IO.Directory.GetCurrentDirectory() + @"\txt1.txt";

            using (var streamReader = new System.IO.StreamReader(filePath))
            {
                while (!streamReader.EndOfStream)
                {
                    string line = streamReader.ReadLine();
                    string[] index = line.Split('\t');

                    for(int i=0; i<index.Length; i++)
                    {
                        string[] prekPav = index[i].Split(':');
                        comboBox1.Items.Add(prekPav[i]);
                        comboBox1.Click -= comboBox1_Click;
                    }
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string filePath = System.IO.Directory.GetCurrentDirectory() + @"\txt1.txt";

            using (var streamReader = new System.IO.StreamReader(filePath))
            {
                while (!streamReader.EndOfStream)
                {
                    string line = streamReader.ReadLine();
                    string[] index = line.Split('\t');

                    for (int i = 0; i < index.Length; i++)
                    {
                        string[] prek = index[i].Split(':');
                        string[] mat = prek[i+1].Split(',');
                        string[] kiek = prek[i+1].Split(':');
                        kiek[i] = kiek[i].Replace(mat[i] + ",", "");
                    }
                }
            }
        }
    }
}
What I'am pretty much doing is generating comboBox.Items when user clicks on the comboBox and unsubscribing from event, so I wouldn't just get many copies of said items. From there I need to make it so that when they choose any of those items data related to them would be loaded into the dataGridview and I get stuck here. The data in file follows this format: Item name: its dimensions, amount of it left: dimensions, amount: dimensions, amount: ... and so on. What happens that I load up data from 3 different items data and not the different dimensions or amounts that I need.
If you can please keep the code or any technical stuff simple, so I could understand. Thanks.
 

Attachments

  • txt1.txt
    150 bytes · Views: 35
hi,
so what does it do with the code applied ?
first of all looks like you are populating the combobox is incorrect
and do not name a stringarray index
 
let's look at the while-loop that is reading the data from the file.
ReadLine read a line, as it says. if you want to split the line with \t the data format you supplied is incorrect, 'cause there's no \t between items.
so please outline what you actually want to do.
I suggest you add a commentline to every line of your code
And if you have solved your problem, please notify the forum
 
What I'am pretty much doing is generating comboBox.Items when user clicks on the comboBox and unsubscribing from event, so I wouldn't just get many copies of said items.
Why not just populate the combobox when the form loads like a normal person would do? Why do you just populate the combobox when the user clicks on it? Why is your unsubscribe call inside your loop?
 
You should load your data just once and keep it all in memory -- hopefully as objects rather than strings. It's only later when you start dealing with large amounts of data, will you want to do the incremental loading of just what needs to be displayed. Anyway, as much as I want to lead you down that object oriented direction, for now I'll settle for trying to get you to simplify your loading code first.

Anyway, your loading code for the combobox population is working only by accident, not by intention.
C#:
string filePath = System.IO.Directory.GetCurrentDirectory() + @"\txt1.txt";

using (var streamReader = new System.IO.StreamReader(filePath))
{
    while (!streamReader.EndOfStream)
    {
        string line = streamReader.ReadLine();
        string[] index = line.Split('\t');

        for(int i=0; i<index.Length; i++)
        {
            string[] prekPav = index[i].Split(':');
            comboBox1.Items.Add(prekPav[i]);
            comboBox1.Click -= comboBox1_Click;
        }
    }
}
First, there is the multiple unregistering for click events. That shouldn't even be part of your load code. You should move it elsewhere, or as I said even better, just populate the combobox at form load time. Anyway for the sake of discussion, I'll just take it out to leave us with:

C#:
string filePath = System.IO.Directory.GetCurrentDirectory() + @"\txt1.txt";

using (var streamReader = new System.IO.StreamReader(filePath))
{
    while (!streamReader.EndOfStream)
    {
        string line = streamReader.ReadLine();
        string[] index = line.Split('\t');

        for(int i=0; i<index.Length; i++)
        {
            string[] prekPav = index[i].Split(':');
            comboBox1.Items.Add(prekPav[i]);
        }
    }
}

Your call to split doesn't really do anything because your file is line delimited, not tab delimited. So your index array will always have one element. So your loop will only ever loop one time since index.Length == 1. Furthermore, one of the happy accidents you have is that since i will always be 0, you are always happily getting the first part of line before the ':' (e.g. prekPav[0]):

Anyway since we only loop once and i == 0 that means we can take that for loop away and put in the constant 0 for i:
C#:
string filePath = System.IO.Directory.GetCurrentDirectory() + @"\txt1.txt";

using (var streamReader = new System.IO.StreamReader(filePath))
{
    while (!streamReader.EndOfStream)
    {
        string line = streamReader.ReadLine();
        string[] index = line.Split('\t');
        string[] prekPav = index[0].Split(':');
        comboBox1.Items.Add(prekPav[0]);
    }
}

And as previously mentioned, the splitting on the tab character isn't really doing anything, then we can just split line directly to get:
C#:
string filePath = System.IO.Directory.GetCurrentDirectory() + @"\txt1.txt";

using (var streamReader = new System.IO.StreamReader(filePath))
{
    while (!streamReader.EndOfStream)
    {
        string line = streamReader.ReadLine();
        string[] prekPav = line.Split(':');
        comboBox1.Items.Add(prekPav[0]);
    }
}

Next, it looks like you've overlooked that the .NET Framework already has support for reading files by lines. You could have used the older File.ReadAllLines() or newer File.ReadLines(). I prefer the latter when I know I'll be discarding each line after I've processed it. So we end up with:

C#:
string filePath = System.IO.Directory.GetCurrentDirectory() + @"\txt1.txt";

foreach (string line in File.ReadLines(filePath))
{
    string[] prekPav = line.Split(':');
    comboBox1.Items.Add(prekPav[0]);
}

And then it's a really bad idea to build up file paths yourself because it can get error prone when to add backslashes, and when not to. You should use Path.Combine() instead to let the .NET Framework do all the hard work for you:

C#:
using System.IO;
:
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "txt1.txt");

foreach (string line in File.ReadLines(filePath))
{
    string[] prekPav = line.Split(':');
    comboBox1.Items.Add(prekPav[0]);
}

But then it get's even better. You don't even need a full path when you want to access a file that is in the current directory. So you can just refer to the file by name:
C#:
foreach (string line in File.ReadLines("txt1.txt"))
{
    string[] prekPav = line.Split(':');
    comboBox1.Items.Add(prekPav[0]);
}
Nice and simple.

Hopefully this will help you unravel the extra complexity you've put into your loading code when the selected item changes. Make note that in that code, it is mostly working by accident, rather than intention except for the fact that your luck ran out when it came to storing values into your kiek array.
 
Like this : Question - How do you read data from file once and then use it with different button events? - I think this is a continued effort of their last topic. Atleast I can see the relevance in resemblance. Our OP could have applied the same technique, but didn't.
That question was just for self education and future reference, if I would have applied that to code proffesor would know right away that I just copied something of the internet without proper knowledge how it works. Also he said to play around with new events that come from combobox and datagrid , that's what I did. And anyway I figured out what was wrong and now it works ( made the same mistake as always of trying to make the exercise more difficult than it is ).
 
I don't think he would. What I gave you on the other topic was pretty basic, and I didn't even utilise any classes. I could have used a class of persons, and that would have probably raised some suspicions. But as it was how I gave it to you is basic and easy research, even for an amateur. :)
 
I don't think he would. What I gave you on the other topic was pretty basic, and I didn't even utilise any classes. I could have used a class of persons, and that would have probably raised some suspicions. But as it was how I gave it to you is basic and easy research, even for an amateur. :)
Maybe, I just dislike showing people that I can do something more than basics, so they don't out expectations on me. ( Seriously proffesor is already talking about internship and creating our portfolio for it, even thought only 3 months have passed in college )
 
Well consider that for many of the so-called "coding bootcamps", you are paying for as much as a 6 months of school for about 2-3 weeks of education. These bootcamps claim that their graduates are ready to go do production work. Personally, I've got my doubts because it sounds too good to be true.
 
Back
Top Bottom