Having problems succuessfully populating the listBox via arrays. Array size is input by user. Also, cant quite get the SaveFileDialog working with the option for the user to create own filename & location.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace _7_3
{
    public partial class Form1 : Form
    {
        //string[] studentData = null;
        int arraySize = 0;
        
        int counter = 0;
        

        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {


                    string firstName = "";
                    string lastName = "";
                    firstName = Convert.ToString(textBoxFirstName.Text);
                    lastName = Convert.ToString(textBoxLastName.Text);
                    arraySize = Convert.ToInt32(textBoxSize.Text);

                    string[] studentData = new string[arraySize];

                    //send data to array
                    studentData[counter] = textBoxFirstName.Text;
                    studentData[counter] = textBoxLastName.Text;
                    counter++;
                    

                    //clear textbox fields
                    textBoxLastName.Clear();
                    textBoxFirstName.Clear();
                    textBoxFirstName.Focus();

                    if (arraySize == counter)
                    {
                        MessageBox.Show("All students have been input.");
                        File.WriteAllLines("TextFile.txt", studentData);
                    }
                    

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            string[] textFile = File.ReadAllLines("TextFile.txt");
            listBox1.DataSource = textFile.ToList();
             
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog Open = new SaveFileDialog();
        }
    }
}

SaveFileDialog : In the menubar of VS C# you as last menu Help
I use it quite often, it is there for a reason. Now in that menu select Search a window opens, Type in SaveFileDialog and see what happens.

In line 46 and 47 you want to store first- and lastname in the same place in an array, that is very hard to do.
I would first create a Student class like this:

class Student
    {
        private string _FirstName = string.Empty;
        private string _LastName = string.Empty;

        public Student() //default constructor
        {
        }

        public Student(string firstName, string lastName)
        {
            _FirstName = firstName;
            _LastName = lastName;
        }

        public string FirstName 
        {
            get { return _FirstName; }
            set { _FirstName = value; }
        }

        public string LastName
        {
            get { return _LastName; }
            set { _LastName = value; }
        }
    }

And then make a list instead of an array like this:

List<Student> StudentData = new List<Student>();
Student = new Student(FirstN,LastN);
StudentData.Add(Student);
//etc...

This has great advantage over an array, consider:
No more worries about counters, indexes etc.
If you later want to add extra info(adress, grades) you just have to adjust the Student class!

Thanks but as part of the assignment the user defines the size of the array (classroom). I'm supposed to put it through a loop until it fills the inputted size of the array. Then it should populate the listBox with the array of the inputted student names.

True, it is difficult to cast the two different inputs in a single array. So would I be better off casting to two arrays, but that begs the question of how I would populate the listBox with both??

Concatenate the strings and put the concatenation into the stringarray:
studentData[counter] = textBoxFirstName.Text + " " + textBoxLasttName.Text;The C# compiler knows that the + operator here means concatenation of strings and not summing up two numbers.

B.T.W. to fill a list box with strings is easy:
Start an forms application drop a listbox on the form and fill in this code in the Forms.cs file:

public partial class Form1 : Form
    {
        string[] myStrArr = new string[]{"AB","BC","CD"};//***

        public Form1()
        {
            InitializeComponent();
            // use this
            this.listBox1.Items.Add(myStrArr[0]);
            this.listBox1.Items.Add(myStrArr[1]);
            this.listBox1.Items.Add(myStrArr[2]);
            // or use this, but better not use both
            //this.listBox1.DataSource = myStrArr;//***           
        }    
    }

Hit F5 and see.
myStrArr is studentData in your code.

The SaveFileDialog facilitates the selection of filename and path to save the file, but you must perform the actual write of the data. See How to: SaveFileDialog.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.