Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

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

$
0
0

I am still a novice at Visual Studio so if you're going to give me some help, please explain it in full detail. Thanks!

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? 

If not, is there an easier way to go by and do this? I don't have SQL or Access to use to create my project, so please don't bring those into the question! 



Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>