0
    private void subjectpagenextbtn_Click(object sender, EventArgs e)
    {

        TextBox[] subjectnamearray = new TextBox[] { subjectname1, subjectname2, subjectname3, subjectname4, subjectname5, subjectname6 };
        int i = 0;
        while (i <= numofsubjects.SelectedIndex)
        {
            if (subjectnamearray[i].Text.Trim() == string.Empty)
            {
                MessageBox.Show("Please fill out all textboxes.", "Error Message");
                i = i + 1;
                return;
            }
        }

        i = 0;
        string[] subjects = new string[numofsubjects.SelectedIndex];
        List<string> datatablesubjectnamearray = new List<string>();

        this.Hide();
        var TaskPage = new Task_Page(subjectnamearray,datatablesubjectnamearray,numofsubjects.SelectedIndex);
        TaskPage.Closed += (s, args) => this.Close();
        TaskPage.Show();
        TaskPage.StartPosition = FormStartPosition.Manual;
        TaskPage.Location = new Point(this.Location.X, this.Location.Y);



        while (i < numofsubjects.SelectedIndex)
        {

            datatablesubjectnamearray.Add(subjectnamearray[i] + "Weighting");
            datatablesubjectnamearray.Add(subjectnamearray[i] + "Marks");
            /*http://csharp.net-informations.com/collection/list.htm*/


            i = i + 1;

        }

        Task_Page taskpage = new Task_Page(subjectnamearray, datatablesubjectnamearray, numofsubjects.SelectedIndex);

    }

This is the code for one of my buttons on a form that I have that collects the subjects of the users and number of subjects for users and I basically also made an array for the titles for the datatable that I'm going to create on the next form. I'm also passing the values,subjectnamearray, datatablesubjectnamearray, numofsubjects.SelectedIndex to TaskPage. So, Task Page is the page that I want to get to when I click the button. I also want these values to be passed but when I click on the button, it just freezes and I can't even close the application. I have to press the stop button on visual studio. But I don't know why it's freezing.

habib
  • 2,366
  • 5
  • 25
  • 41
Asereta
  • 1
  • 2
  • 5
    In your first `while` loop, move `i = i + 1` outside of the `if` statement, otherwise you are stuck in that loop forever. – eye_am_groot Jun 22 '18 at 14:36
  • Do not perform intensive tasks on the behalf of GUI thread. Use a seprate thread or background worker to do this. https://stackoverflow.com/questions/11500563/winform-multithreading-use-backgroundworker-or-not – habib Jun 22 '18 at 15:10

1 Answers1

1

Your first while loop appears to be the issue. You get caught in infinite loop. I do not see any other issues further down in the code.

Your code looks like this:

private void subjectpagenextbtn_Click(object sender, EventArgs e)
{

    TextBox[] subjectnamearray = new TextBox[] { subjectname1, subjectname2, subjectname3, subjectname4, subjectname5, subjectname6 };
    int i = 0;
    while (i <= numofsubjects.SelectedIndex)
    {
        if (subjectnamearray[i].Text.Trim() == string.Empty)
        {
            MessageBox.Show("Please fill out all textboxes.", "Error Message");
            i = i + 1;
            return;
        }
    }

I think if you changed your code to this:

private void subjectpagenextbtn_Click(object sender, EventArgs e)
{

    TextBox[] subjectnamearray = new TextBox[] { subjectname1, subjectname2, subjectname3, subjectname4, subjectname5, subjectname6 };
    int i = 0;
    while (i <= numofsubjects.SelectedIndex)
    {
        if (subjectnamearray[i].Text.Trim() == string.Empty)
        {
            MessageBox.Show("Please fill out all textboxes.", "Error Message");            
            return;
        }
        i = i + 1;
    }

It should fix your issue with visual studio crashing. If it does not I would recommend closing VS, restarting your computer and trying again. Good Luck!

Fletchy
  • 311
  • 1
  • 4
  • 18
  • Did this resolve the issue? If so please mark as answer or let me know and I can try to give some further assistance. – Fletchy Jun 22 '18 at 15:31