4 Replies - 2114 Views - Last Post: 17 November 2012 - 12:59 PM Rate Topic: -----

#1 The3xorc1st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-October 12

Using Combobox SelectedIndex to Display text using separators

Posted 17 November 2012 - 10:18 AM

Hi!
I am trying to use a selectedindex_changed method to change a label's text.
The basic idea is that I have a list of Cars and the info regarding them in a text document (info separated by '_' and cars by ';'), i use a constructor in a Car Class to create them. (I can add the code if you want though the variables are in French).
I am using a Display window with a ComboBox to display my cars (When i initialise the display window using arrays and lists i retrieve only the Car's Make and Model and use those as my Combobox's collection).
My problem is that I would like to display all the info without having to use the StreamReader and a two dimensional array, i'de rather use a class method to return the info but i cant seem to connect the combobox's selectedindex with a car's location in my text doc.
I hope this makes a bit of sense...
Thanks to anyone that can help:)

Is This A Good Question/Topic? 0
  • +

Replies To: Using Combobox SelectedIndex to Display text using separators

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Using Combobox SelectedIndex to Display text using separators

Posted 17 November 2012 - 10:49 AM

Let's see your code. We have nothing to help with so far.

You might get some benefit from the classes and properties tutorials linked in my signature block. They both show linking to properties of objects to update the GUI.
Was This Post Helpful? 0
  • +
  • -

#3 The3xorc1st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-October 12

Re: Using Combobox SelectedIndex to Display text using separators

Posted 17 November 2012 - 11:35 AM

The text changed method:
        private void cBoxCars_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = cBoxCars.SelectedIndex;
            Voiture Voit1 = new Voiture();
            CarInfolbl.Text = Voit1.DisplayCar(i);
            

        }



And the unfinished display method:
internal string DisplayCar(int i)
        {
            _CarDisplay = "";
            using (StreamReader s1 = new StreamReader(@"C:\Users\Nicolas\Voitures_.txt"))
            {
                _CarDisplay += @"Marque: " + Marque + Environment.NewLine;
                _CarDisplay += @"Modele: " + Modele + Environment.NewLine;
                _CarDisplay += @"Puissance: " + Puissance + Environment.NewLine;
                _CarDisplay += @"Occasion " + Occasion + Environment.NewLine;
                _CarDisplay += @"Prix: " + Prix + Environment.NewLine;
                _CarDisplay += @"Libre: " + ((Libre==false)?("Non"):(/>/>"Oui")) + Environment.NewLine;
                s1.Close();
            }
            return _CarDisplay;
        }


And just in case my constructor and variables:
        #region Constructeurs
        public Voiture() {}
       
        public Voiture(string ma, string mo, int pui, string occ, int p, string im1,string im2,string im3)
        {
            Marque = ma;
            Modele = mo;
            Puissance = pui;
            Occasion = occ;
            Prix = p;
            Libre = true;
            Image1 = im1;
            Image2 = im2;
            Image3 = im3;
        }
        #endregion

        #region Propriétés
        protected string Marque { get; set; }
        protected string Modele { get; set; }
        protected int Puissance { get; set; }
        protected string Occasion { get; set; }
        protected int Prix { get; set; }
        protected bool Libre { get; set; }       
        protected string Image1 { get; set; }
        protected string Image2 { get; set; }
        protected string Image3 { get; set; }
        #endregion


I know that i am missing some huge chunks (ie:using an int as an argument but never actually doing anything with it). But I dont really know where to start.
Was This Post Helpful? 0
  • +
  • -

#4 The3xorc1st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-October 12

Re: Using Combobox SelectedIndex to Display text using separators

Posted 17 November 2012 - 12:05 PM

I am thinking of doing this on second thought
        private Voiture AffichageVoitures(int i) 
        {
           
            using (StreamReader _reader1 = new StreamReader(@"C:\Users\Nicolas\Voitures_.txt")) 
            { 
                string text = _reader1.ReadToEnd();
                string[] text2 = text.Split(';');
                Voiture v1 = new Voiture(/*Split the car info using .split('_') and text2[i]*/);
                _reader1.Close();                
                return v1;
            }
        }


I am not sure if this is a better idea and i know there are some errors. Is there a method to directly split a string and use every element as an argument for my Voiture constructor?
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Using Combobox SelectedIndex to Display text using separators

Posted 17 November 2012 - 12:59 PM

Quote

My problem is that I would like to display all the info without having to use the StreamReader and a two dimensional array, i'de rather use a class method to return the info but i cant seem to connect the combobox's selectedindex with a car's location in my text doc.


Position in the text doc? Eh?

Are you trying to use the text doc in real time like a database?

Since it is just a text doc you would be better off reading all of it into a collection, such as a List<car> - then add them to the .Items of the combobox.

YOu do realize that a combobox takes more than just text, right? it takes objects. You can put the entire car object as an item in the combobox. Then when the .SelectedIndex changes you just get the actual car object that was selected. No syncing to anything else.

Other options that are far better than an all-inclusive text file would be:

Serialize each car object to its own XML document.
Separating data from GUI - PLUS - serializing the data to XML


Use a proper database
Read this tutorial
Entire section of tutorials
Parameterizing Your SQL Queries: The RIGHT Way To Query A Database.
Using SqlDependency to monitor SQL database changes
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1