How would I make selections from one listbox show the selected items in a second listbox?

28 Views Asked by At

I have a listbox (lstbx1) that shows conversion options (Miles to KM, KM to miles, etc). I am pulling these from a text file. I want to show the final conversion in the second listbox (lstbx2). How would I show the selected items's conversions?

I have tried using a foreach method, but it only makes the number of selected items appear, not the selected items themselves.

Here is the main form: Main form

As you can see, I am wanting to convert 11 Miles to KM, 11 ft to m, and 11 in to cm. I have the calculations written, but I cannot figure out how to show them. Below is my code for my main form, my Conversion(calculation) class, and my database class.

I have thought about making the selections in lstbx1 an array, but I'm not sure how I would do that.

Thanks in advance!

 public partial class frmMain : Form
    {
        // Create blank list conversions
        private List<Conversion> conversions = ConversionsDB.GetConversions();
        

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // Close application
            System.Windows.Forms.Application.Exit();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            // Load data from file and create a list

            // Load conversion options list
            lstConversionOpts.Items.Clear();
            foreach(Conversion conversion in conversions)
            {
                lstConversionOpts.Items.Add(conversion.GetBasicConversions());
            }
        }

        // Open Manage conversions form
        private void btnManage_Click(object sender, EventArgs e)
        {
            frmManageConvs chManageConvsForm = new frmManageConvs();
            chManageConvsForm.ShowDialog();
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            lstMainConversions.Items.Clear();
            // Check to see if input is valid
            if (IsValid())
            {
                decimal chFinalVal = Convert.ToDecimal(txtOrigValue.Text);
                
                // Show final conversions
                foreach (var converionType in lstConversionOpts.SelectedItems)
                {
                    lstMainConversions.Items.Add(lstConversionOpts.Items.ToString());
                } 

            }

        }
        
        // Check for valid input
        private bool IsValid()
        {
            bool chValid = true;
            string chError = "";
            chError += Validator.IsNumeric(txtOrigValue.Text, txtOrigValue.ToString());

            if (chError != "")
            {
                chValid = false;
                MessageBox.Show(chError, "Entry Error");
            }
            return chValid;
        }
        
        
    }
public class Conversion
    {
        // Construct default conversion method
        public Conversion()
        {

        }

        public Conversion(string from, decimal fromVal, string to, decimal toVal, decimal inVal, decimal finalVal) =>
            (this.From, this.fromValue, this.To, this.toValue, this.inVal, this.finalVal) = 
            (from, fromVal, to, toVal, inVal, finalVal);

        // Get/set Conversion data
        public string From { get;  set; }
        public decimal fromValue { get;  set; }
        public string To { get;  set; }
        public decimal toValue { get;  set; }
        public decimal inVal { get; set; }
        public decimal finalVal { get; set; }

        // Display values
        public string GetDisplayText() => From + "|" + To + "|" + toValue;
        public string GetBasicConversions() => From + " to " + To.ToLower();
        public string GetFinalConversions(decimal inVal) => inVal + " " + From + " = " + (inVal * toValue) + " " + To;
    }
public class ConversionsDB
    {
        // Declare where files are stored
        private const string chDir = @"G:\Other computers\My Computer\School\Spring_Semester_2023\CIST2342 - C# II\ProgrammingProject1\CHPP1\CaseyHemphillCalculateConversions";
        private const string chPath = chDir + "Conversions.txt";
        private const string chDefault = chDir + "ConversionsDefault.txt";

        // Create list from files
        public static List<Conversion> GetConversions()
        {

            // Read from file
            using StreamReader chTxtIn =
                new StreamReader
                (
                    new FileStream(chPath, FileMode.OpenOrCreate, FileAccess.Read)
                );

            // Create list from file
            List<Conversion> chConversions = new List<Conversion>();
            while (chTxtIn.Peek() != -1)
            {
                string chRow = chTxtIn.ReadLine();
                String[] chColumns = chRow.Split(' ');
                Conversion chConversion = new Conversion();
                chConversion.From = chColumns[0];
                chConversion.fromValue = Convert.ToDecimal(chColumns[2]);
                chConversion.To = chColumns[1];
                chConversion.toValue = Convert.ToDecimal(chColumns[5]);
                chConversions.Add(chConversion);
            }

            return chConversions;
        }

        // Save list
        public static void SaveConversions(List<Conversion> chConversions)
        {
            using StreamWriter chTxtOut =
                new StreamWriter
                (
                    new FileStream(chPath, FileMode.Create, FileAccess.Write)
                );

            foreach (Conversion chConversion in chConversions)
            {
                chTxtOut.Write(chConversion.fromValue + " ");
                chTxtOut.Write(chConversion.From + " = ");
                chTxtOut.Write(chConversion.toValue + " ");
                chTxtOut.Write(chConversion.To);
            }
        }

        // Create default list from files
        public static List<Conversion> GetDefault()
        {

            // Read from file
            using StreamReader chTxtIn =
                new StreamReader
                (
                    new FileStream(chDefault, FileMode.OpenOrCreate, FileAccess.Read)
                );

            // Create list from file
            List<Conversion> chConversions = new List<Conversion>();
            while (chTxtIn.Peek() != -1)
            {
                string chRow = chTxtIn.ReadLine();
                String[] chColumns = chRow.Split(' ');
                Conversion chConversion = new Conversion();
                chConversion.From = chColumns[0];
                chConversion.fromValue = Convert.ToDecimal(chColumns[2]);
                chConversion.To = chColumns[1];
                chConversion.toValue = Convert.ToDecimal(chColumns[5]);
                chConversions.Add(chConversion);
            }

            return chConversions;
        }
    }```


0

There are 0 best solutions below