Hello, I'm here to ask a question about listboxes/textboxes. I have two forms. The main form has a listbox with some buttons on it. Two buttons open a 2nd form, if I open the 2nd form using the Open button, the textboxes should be blank. I have this part figured out, it works great, it allows me to add a new list item to form 1. The Edit button, should open the 2nd form populating 3 different textboxes (name, address, type). For the life of me I haven't been able to figure out how to display only the persons name in the Listbox form, and when clicking the edit button pulling up ALL information.

Here is my code so far, sorry for the mess, it's still a work in progress.

This is Form 1(the main form with listbox on it)

public partial class Form1 : Form
    {

        //public vars for file access
        FileStream output;
        StreamReader fileReader;
        StreamWriter fileWriter;
        bool bCheck;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            MenuOpen();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Frmtwo myForm = new Frmtwo();

           // myForm.FullName = lstRecords.Items.Add(fields[0]);
           // lstRecords.Items.Add(fields[0] + "\t" + fields[1]);

            DialogResult result = myForm.ShowDialog();

            if (result == DialogResult.Cancel)
                return;

            //lblResult.Text = myForm.FullName;

        }



        private void MenuOpen()
        {
            Frmtwo frm2 = new Frmtwo();
            if (frm2.ShowDialog(this) == DialogResult.OK)
            {
                lstRecords.Items.Add(frm2.getItem());
            }
            frm2.Close();
            frm2.Dispose();
        }
        // opens text file
        private void FileOpen()
        {
            OpenFileDialog fileChooser = new OpenFileDialog();

            fileChooser.Title = "Pick a file";
            fileChooser.Filter = "TXT Files (*.txt)|*.txt";

            DialogResult result = fileChooser.ShowDialog();

            // did they click cancel?
            if (result == DialogResult.Cancel)
            {
                //do nothing
                return;
            }

            //proceed with processing file
            // get the file name they chose
            string strFileName = fileChooser.FileName;

            try
            {
                // open the file for read access
                output = new FileStream(strFileName,
                    FileMode.Open, FileAccess.Read);

                fileReader = new StreamReader(output);

                //var to hold the read record
                string strInputLine;
                string[] fields;

                //loop to get records and break into fields
                while (fileReader.EndOfStream != true)
                {
                    //read a record
                    strInputLine = fileReader.ReadLine();

                    //split the record
                    fields = strInputLine.Split(',');

                    //add to the listbox
                    lstRecords.Items.Add(fields[0] + ", " + fields[1]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //always close resources
                fileReader.Close();
                output.Close();
            }
        }
        private void SaveFile()
        {
            //create a save file dialog
            SaveFileDialog fileChooser = new SaveFileDialog();
            Frmtwo frm2 = new Frmtwo();

            fileChooser.Title = "Choose Save Location";
            fileChooser.Filter = "Text Files (*.txt)|*.txt";

            //open dialog, get result
            DialogResult result = fileChooser.ShowDialog();

            //did they click cancel?
            if (result == DialogResult.Cancel)
            {
                return;
            }

            //get the file name from the dialog
            string strFileName = fileChooser.FileName;

            try
            {
                //save via filestream
                //open the new file for write access


                if (bCheck)
                {
                    output = new FileStream(strFileName,
                        FileMode.OpenOrCreate, FileAccess.Write);
                    bCheck = false;
                }
                else
                {
                    output = new FileStream(strFileName,
                      FileMode.Append, FileAccess.Write);

                }


                fileWriter = new StreamWriter(output);

                //take the text from the box and write it to the file
                // this is only saving what is in the list box, i need to save the properties(like salary and employee type)
                foreach (var item in lstRecords.Items)
                {
                    fileWriter.WriteLine((frm2.getItem()));
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //close resources
                fileWriter.Close();
                output.Close();
            }
        }
        private void tbtnOpen_Click(object sender, EventArgs e)
        {
            FileOpen();
        }


        private void frmOpen_Click(object sender, EventArgs e)
        {
            FileOpen();
        }

        private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MenuOpen();
        }

        private void saveAsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SaveFile();
        } 
    }

This is Form 2 (this form is used to edit listbox entries, which I'm trying to figure out how to populate the textboxes in this form with which ever lisbox item is selected, however, in the listbox I only want a name displayed, not the address etc. I need the textboxes to display this information when I edit it though and when I save it to a text file.

public partial class Frmtwo : Form
    {
        public string getItem()
        {
            return txtLast.Text + txtName.Text + txtEmp.Text+ txtSal.Text;
        }

        //passes item back and forth (will need a separate one for each textbox
        public string FullName{ get; set;}
        public string LastName { get; set; }
        public string EmpNum { get; set; }
        public string Salary { get; set; }

         StringBuilder tabs = new StringBuilder();


        public Frmtwo()
        {
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            FullName = txtName.Text;
            LastName = txtLast.Text;
            EmpNum = txtEmp.Text;
            Salary = txtSal.Text;

        }

        private void Frmtwo_Load(object sender, EventArgs e)
        {
            if (FullName != "")
            {
                txtName.Text = FullName;
            }
            if (LastName != "")
            {
                txtLast.Text = LastName;
            }
            if (EmpNum != "")
            {
                txtEmp.Text = EmpNum;
            }
            if (Salary != "")
            {
                txtSal.Text = Salary;
            }
        }
    }
}

How can I go about doing this? I've searched on Google looking for help, I'm no search genie, but I have spent a good amount of time searching(I always do before I post a question.) I've found something about maybe implementing an Employee class, but I'm not sure how to work that in, also I saw a few things about splitting the string. Any help would be greatly appreciated.

i think i found a class i made a while back to do this, tomorrow i'll try implementing it. i think i can rest now.

You might want to look at the ListView control. The Items collection will hold all your data(Item is the name and 2 subitems address and type), but when you choose the List view option only each item is displayed and none of the subitems. A class would certainly help in passing your data to different methods if necessary.

Edited 3 Years Ago by tinstaafl

I found a way with listbox to hide the data ;) Thank you for your responses, you're one of the few that has helped me quite a bit in my C# journey.

This question has already been answered. Start a new discussion instead.