Derron R
Derron R

Reputation: 225

Click a ComboBox to display a particular line from the .txt file

I am creating a shopping cart using .txt files, but before I start calculating, I need to display the information on the form.

I have 2 separate .txt files. One .txt files have the categories of the different sections you can search in the programs (i.e. Clothing, Games, Accesories, Misc etc). I used a for loop to loop the name of each category into the Combo Box. The other .txt file has the actual names of the products along with its Price, Description, and the Category the product it belongs to.

What I am trying to do is select the particular category from the combo box and show the products from that particular category on the page. Since I am displaying multiple items, I have to use an array, so what I do first is declare the new arrays.

public partial class Form2 : Form
{
    //Name, description and price are the fields for the products
    //Category is the field for the combo box on the home page
    string[] name = new string[100];
    string[] description = new string[100];
    string[] price = new string[100];
    string[] category = new string[100];

This code below populates the ComboBox with the information from the .txt file.

try { StreamReader categories = new StreamReader("categories.txt"); int index = 0; while (categories.Peek() != -1) { category[index] = categories.ReadLine(); index++; }

            for (int i = 0; i < index - 1; i++)
            {
                cBSearch.Items.Add(category[i]);
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

And this one below actually displays the information from the other .txt file. But instead of displaying the information based on the category, it displays all of the information in the .txt file.

try
        {
            StreamReader products = new StreamReader("products.txt");
            int index = 0;
            while (products.Peek() != -1)
            {
                name[index] = products.ReadLine();
                description[index] = products.ReadLine();
                price[index] = products.ReadLine();
                category[index] = products.ReadLine();
                index++;
            }
            //When you click the group for the combo box, the items from the group are not selected man! 
            //if (category[index] == cBSearch.SelectedIndex) 
            for (int i = 0; i < index; i++)
            {
                lblProductName.Text += name[i] + "  ";
                lblDescription.Text += description[i];
                lblPrice.Text += price[i];
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

I think I am missing something. Since there are 2 seperate .txt files, it makes it more difficult to select information from one .txt file to another. What do I need to add so that only those products from a particular category are shown?

Upvotes: 1

Views: 585

Answers (2)

user3055138
user3055138

Reputation: 1

I tried your method, but nothing displayed in the form. I think more has to be done to get this to work.

Also, I am not switching to database because it would only complicate things for myself. I only know the fundamentals of C#.

Upvotes: 0

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

1. you are not comparing your Category Items of the second file with the SelectedItem from the ComboBox

Try This:

        while (products.Peek() != -1)
        {
            category[index] = products.ReadLine();
            if(cBSearch.SelectedItem.ToString().Equals(category[index]))
            {
            name[index] = products.ReadLine();
            description[index] = products.ReadLine();
            price[index] = products.ReadLine();
            }
            index++;
        }

2. I strongly suggest you to use Database instead of files here as going forward it would be complicated to manage your data using files.

Upvotes: 2

Related Questions