User3283827
User3283827

Reputation: 33

How to transfer info from textboxes into a listbox in another form

In this student registry of mine, I have a list box in the main form and in my second form I have add student. In this second form, I add a name to the name text box and numbers for the student which need to be separated by pipe characters ("|"). After the name and numbers in the textboxes are filled out when the user presses the okay button it should look like the hard coded students below. Code below

First Form

private void listForm1_SelectedIndexChanged(object sender, EventArgs e)
        {


            string val = listForm1.GetItemText(listForm1.SelectedItem);



            string[] valArray = val.Split('|');

            int sum = 0;
            int scores = 0;


            for (int i = 1; i < valArray.Length; i++)
            {
                int num = Convert.ToInt32(valArray[i]);
                sum += num;
                scores++;
            }


            int average = sum / scores;


            txtAverage.Text = average.ToString();
            txtTotal.Text = sum.ToString();
            txtScoreCount.Text = scores.ToString();
        }


        private void Form1_Load(object sender, EventArgs e)

        {

            listForm1.Items.Add("Hamish Overtone" + "|" + 39 + "|" + 12 + "|" + 85);
            listForm1.Items.Add("Claudia Dye" + "|" + 44 + "|" + 56 + "|" + 85);
            listForm1.Items.Add("Mike Layne" + "|" + 12 + "|" + 47+ "|" + 28);

        }

Second Form

 private void btnADDSCORE_Click(object sender, EventArgs e)
        {


            string userInput = txtNewScore.Text;

            txtNewScores.Text = txtNewScores.Text + " " + userInput;

            txtNewScore.Text = String.Empty;


        }
           private void btnNewOk_Click(object sender, EventArgs e)
        {

        }

        private void txtNewName_TextChanged(object sender, EventArgs e)
        {

        }
    }
ERRORING PART:

private void listForm1_SelectedIndexChanged(object sender, EventArgs e) {

    // Get the value from the selected item
    string val = listForm1.GetItemText(listForm1.SelectedItem);


    // Split the item's value to a string array according to the pipe char
    string[] valArray = val.Split('|');

    int sum = 0;
    int scores = 0;

    // Iterate through all possible values and sum it up, 
    // while keeping count to how many numbers there are:
    for (int i = 1; i < valArray.Length; i++)
    {
        int num = Convert.ToInt32(valArray[i]);
        sum += num;
        scores++;
    }

    // Calculate the  average. 
    // Keep in mind using an integer will create a whole number, without decimal points.        
    int average = sum / scores;

    // Place the average and the sum in textboxes
    txtAverage.Text = average.ToString();
    txtTotal.Text = sum.ToString();
    txtScoreCount.Text = scores.ToString();
}

Upvotes: 0

Views: 1650

Answers (2)

nolnah93
nolnah93

Reputation: 134

Try adding a button and use the below code (in the form1 class) to its click event handler.

class form1
{
    private void form1_addButton_Click(object sender, EventArgs e)
    {
         Form2 newForm = new Form2();
         newForm.ShowDialog();

         foreach(string s in newForm.studentInfo)
         {
             listForm1.Items.Add(s);
         }
    } 
}

The code below would be added to your form2 class file.

class form2
{
    public List<string> studentInfo {get; set;} //add this to form2 

    private void form2_addButton_Click(object sender, EventArgs e)
    {


        //add scores to list here.

    }

}

Edit

//THIS IS IN FORM2 :)
private void btnNewOk_Click(object sender, EventArgs e)
{
    string student = textBoxName.Text;
    string[] scores = textBoxNewScores.Text.Trim().Split(' '); 

    for(int i = 0; i < scores.Length; i++)
    {
        student = student + "|" + scores[i];
    }
    studentInfo.Add(student);
    this.Close();
}

Edit 2

private void listForm1_SelectedIndexChanged(object sender, EventArgs e)
{


    string val = listForm1.GetItemText(listForm1.SelectedItem);



    string[] valArray = val.Split('|');

    int sum = 0;
    int scores = 0;


    for (int i = 1; i < valArray.Length; i++)
    {
        int num = Convert.ToInt32(valArray[i]);
        sum += num;
        scores++;
    }


    int average = 0;

    if (scores != 0)
        average = sum / scores;


    txtAverage.Text = average.ToString();
    txtTotal.Text = sum.ToString();
    txtScoreCount.Text = scores.ToString();
}

Upvotes: 0

Sach
Sach

Reputation: 10393

In your Main window (Form1 here), you want to create a delegate that lets you pass data, say, a string. Then you create an instance of that delegate type and subscribe a method that matches. Then when you open your secondary window, you pass that delegate to your secondary window.

public delegate void DataTransfer(string data);

public partial class Form1 : Form
{
    public DataTransfer transferDelegate;

    public Form1()
    {
        InitializeComponent();
        transferDelegate += new DataTransfer(DataMethod);
    }

    public void DataMethod(string data)
    {
        // Do what you want with your data.
        MessageBox.Show(data);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        InputWindow win = new InputWindow(transferDelegate);
        win.Show();
    }
}

Now, when you invoke that delegate in your secondary window, the DataMethod() of your Form1 gets called, and so you can pass information between windows.

Your secondary window implementation should look like this:

public partial class InputWindow : Form
{
    DataTransfer transferDel;

    public InputWindow(DataTransfer del)
    {
        InitializeComponent();
        transferDel = del;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string data = textBox1.Text;
        transferDel.Invoke(data);
    }
}

As you can see, when you invoke the delegate that was passed, it calls the corresponding method in your main program which is Form1.

Upvotes: 1

Related Questions