2

In the combo box I can get it to load all the conversions to the combo box. But, when I click a conversion from the combo box it changes the data to null for some reason. I am not sure if anyone can help me without looking at all my code. But I thought it was worth a shot. I am trying to select a conversion I.e feet to miles and have the "feet" be in the first label and the "miles" be in the second. Here is the code for the click event of the combo box:

public void CmboBxConversion_SelectedIndexChanged(object sender, EventArgs e)
{
    //This is not working. If I were to say change the  conversion.From to "hello world" the label text does change to hello world.
    //But, as is it is it changes it to a null.
    Conversion conversion = new Conversion();

    Lbl1stConv.Text = conversion.From;  //these are labels
    LblSecondConv.Text = conversion.To;
}

//this is my Conversion class
public class Conversion
{
    public string From { get; set; }
    public string To { get; set; }
    public decimal Multiplier { get; set; }

    public string GetDisplayText(string sep)
    {
        return From + ("|") + To + ("|") + Multiplier;
    }

    public string GetCmboBxText(string sep)
    {
        return From + (" to ") + To;
    }
}

//this is how I am loading the combo box
private void Conversion_Load(object sender, EventArgs e)
{
    conversions = ConversionDB.GetConversions();
    FillConversionComboBox();
}

//here is the method used to fill the combobox
private void FillConversionComboBox()
{
    CmboBxConversion.Items.Clear();

    foreach (Conversion c in conversions)
    {
        CmboBxConversion.Items.Add(c.GetCmboBxText("\n"));
    }
}

    //this is a data base class that the textfile is pulling data
    public static class ConversionDB
{
    public const string Path = @"..\..\Conversions.txt";
    //public const string Path = Dir + "Conversions.txt";

    public static List<ConversionList> GetConversions()
    {


        StreamReader textIn = new StreamReader(
            new FileStream(Path, FileMode.Open, FileAccess.Read));
        List<ConversionList> conversions = new List<ConversionList>();
        while (textIn.Peek() != -1)
        {
            string row = textIn.ReadLine();
            string[] columns = row.Split('|');
            ConversionList conversion = new ConversionList();
            conversion.From = columns[0];
            conversion.To = columns[1];
            conversion.Multiplier = Convert.ToDecimal(columns[2]);
            conversions.Add(conversion);
        }

        textIn.Close();
        return conversions;
    }
  • In your example it looks like `From` is never given a value yet you reference it. Where is it given a value? – Krikor Ailanjian Jul 23 '15 at 05:24
  • Ah, good catch. That would explain the null. I also have a database class. I will edit my question with it. Not sure if that answers your question on giving From a value – Sam Peebles Jul 23 '15 at 05:35
  • Get rid of those parenthesis around your string literals. For instance, change `return From + ("|") + To + ("|") + Multiplier;` to `return From + "|" + To + "|" + Multiplier.ToString();`. – Idle_Mind Jul 23 '15 at 15:26

1 Answers1

1

I'm not sure if this is what you want but it is a little simpler approach that gives you the same functionality. I think it will be much simpler if you bind the combobox to your list of conversions. The selecteditem and selectedvalue will be easier to work with.

    public partial class Form1 : Form
{
    private List<Conversion> conversions=new List<Conversion>();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        loadconversions();
        //foreach (Conversion c in conversions)
        //{this.comboBox1.Items.Add(c); }
        comboBox1.DataSource = conversions;
        this.comboBox1.DisplayMember = "GetCmboBxText";

    }

    private void loadconversions()
    {
        fillconversions("feet","yards",3);
        fillconversions("yard", "feet", 0.33m);
        fillconversions("km", "mile", 0.54m);
        fillconversions("mile", "km", 1.6m);
        fillconversions("quarts", "gallons", 4);

    }
    private void fillconversions(string from, string to, decimal multiplier)
    {
        conversions.Add(new Conversion(from, to, multiplier));
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Conversion c = (Conversion)this.comboBox1.SelectedItem;
        this.textBox1.Text = c.GetDisplayText;
    }
}
public class Conversion
{
    public string From { get; set; }
    public string To { get; set; }
    public decimal Multiplier { get; set; }

    public Conversion(string from, string to, decimal multiplier)
    {
        From = from;
        To = to;
        Multiplier = multiplier;
    }

    public string GetDisplayText
    {
        get { return From + ("|") + To + ("|") + Multiplier; }
    }

    public string GetCmboBxText
    {
        get
        {
            return From + (" to ") + To;
        }
    }
}
Haim Katz
  • 455
  • 3
  • 15
  • so I should put conversion = CmboBxConversion_SelectedIndexChanged.Properties.Items[CmboBxConversion_SelectedIndexChanged.SelectedIndex]); right above the label lines? – Sam Peebles Jul 23 '15 at 05:54
  • I am getting an error when I do that saying it is a method which is not valid in this context – Sam Peebles Jul 23 '15 at 06:02
  • Inspect the combo box properties for how the item is stored but I would think this would work: – Haim Katz Jul 23 '15 at 10:11
  • This is great stuff! My new question is, I am pulling my data from a text file as it is a requirement for my school project. With this example shown will a user of the program be able to add conversions via another form into the text file and then it be updated into this combo box as an item that can be selected? – Sam Peebles Jul 23 '15 at 12:56
  • When I add all this code I am having trouble. I will play around with it some. I really appreciate the help – Sam Peebles Jul 23 '15 at 13:46
  • I have my text file set up like this: Kilometers|Miles|0.6214 (new line) Feet|Meters|0.3048 (new line) Meters|Feet|3.2808 (new line) Inches|Centimeters|2.54 (new line) Centimeters|Inches|0.3937 So how would I be able to distinguish for example that "kilometers" = From, "Miles" = To and 0.6214 = Multiplier? – Sam Peebles Jul 23 '15 at 15:04
  • That's really another question Sam, but you'd use StreamReader(), ReadLine() and String.Split(). – Idle_Mind Jul 23 '15 at 15:27
  • what is the line this.comboBox1.DisplayMember = "GetCmboBxText" doing? – Sam Peebles Jul 23 '15 at 22:35
  • When you bind the combobox to a list (as opposed to just additing items) The Display Member is defining the property on the list's object which will be displayed. If you bind your conversion object as in the example you don't need to split your string from the combobox. You can get the property directly from the item. – Haim Katz Jul 26 '15 at 03:28
  • Conversion c = (Conversion)this.comboBox1.SelectedItem; single myanswer = somevalue * c.multiplier – Haim Katz Jul 26 '15 at 03:31