更改标签页后,组合框崩溃了

时间:2014-02-19 09:52:48

标签: c# winforms datagridview combobox tabcontrol

我的tabcontrol外面有一个组合框。在每个选项卡控件中都有一个充满值的datagridview。在组合框中,您可以为所有值选择转换。例如eV→meV。 当我在第一个选项卡中并使用组合框时没有问题,但是在我切换选项卡然后想要使用组合框之后程序列表然后整个组合框充满了try/catch

private void OpenB_Click(object sender, EventArgs e)
    {            
        string[] result = new string[2];
        bool lesen = false;
        int Spalte = 0;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {

            //Datagridview will be rested, so all values and rows are removed

            for (int i = 1; i < Anzahl; i++)
            {
                DGV[i].Rows.Clear();
            }

            Anzahl = openFileDialog1.FileNames.Length;
            counter = new int[Anzahl];

            try
            {
                if (tabControl1.TabCount < Anzahl)
                {
                    for (int i = 1; i <= Anzahl; i++)
                    {
                        if (i > tabControl1.TabCount)
                        {
                            string title = "Tab " + (tabControl1.TabCount + 1).ToString();
                            TabPage myTabPage = new TabPage(title);
                            tabControl1.TabPages.Add(myTabPage);

                            DataGridView NewDGV = new DataGridView();

                            NewDGV.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
                            NewDGV.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                            NewDGV.Columns.Add("Energy", "Energy");
                            NewDGV.Columns[0].ReadOnly = true;
                            NewDGV.Columns.Add("Count Rate", "Count Rate");
                            NewDGV.Columns[1].ReadOnly = true;
                            NewDGV.Location = new System.Drawing.Point(3, 3);
                            NewDGV.Name = "NewDGV" + Convert.ToString(i);
                            NewDGV.RowHeadersVisible = false;
                            NewDGV.Size = new System.Drawing.Size(276, 379);
                            NewDGV.TabIndex = i;
                            foreach (DataGridViewColumn col in NewDGV.Columns)
                                col.SortMode = DataGridViewColumnSortMode.NotSortable;
                            NewDGV.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;

                            DGV.Add(NewDGV);

                            tabControl1.TabPages[i - 1].Controls.Add(NewDGV);

                        }
                    }
                }
                else if (tabControl1.TabCount > Anzahl)
                {
                    for (int i = tabControl1.TabCount - 1; i >= Anzahl; i--)
                    {
                        tabControl1.TabPages.Remove(tabControl1.TabPages[i]);
                    }
                }
            }
            catch { }

            try
            {
                //Double arrays and Datagridview will be attuned to the count of data
                eV = new double[openFileDialog1.FileNames.Length][];
                meV = new double[openFileDialog1.FileNames.Length][];
                cm = new double[openFileDialog1.FileNames.Length][];
                CR = new double[openFileDialog1.FileNames.Length][];
                CRmax = new double[openFileDialog1.FileNames.Length][];


                for (int i = 0; i < Anzahl; i++)
                {
                    //Naming the columns after data names
                    string[] Dateiname = openFileDialog1.FileNames[i].Split('\\');
                    int L = Dateiname.Length;

                    tabControl1.TabPages[i].Text = Dateiname[L-1];

                }
            }
            catch
            {
            }

            //Datafiles will be read one by one
            DataRead(result, ref lesen, ref Spalte);
        }
    }

    /// Reading loop
    /// 
    /// double[] eV2 = Energy values of the current data file in eV
    /// double[] meV2 = Energy values of the current data file in meV
    /// double[] cm2 = Energy values of the current data file in cm^-1
    /// double[] CR2 = Intensities of the current data file in CR
    /// double[] CRmax2 = normalizied Intensities of the current data file in 1/CRmax
    private void DataRead(string[] result, ref bool lesen, ref int Spalte)
    {
        for (Spalte = 0; Spalte < Anzahl; Spalte++)
        {
            string line;

            lesen = false;
            counter[Spalte] = 0;
            try
            {
                Ursprung = openFileDialog1.FileNames[Spalte];
                //initialize stream reader
                System.IO.StreamReader file1 = new System.IO.StreamReader(openFileDialog1.FileNames[Spalte]);
                //read line per line in stream reader
                while (((line = file1.ReadLine()) != null))
                {
                    counter[Spalte]++;
                    Count2 = counter[Spalte];
                    Count2 = Count2 / 2;

                    try
                    {
                        string[] splitter = line.Split(' ');
                        if ((splitter[0] == "S") && (splitter[1] == "0000"))
                        {
                            lesen = true;
                            counter[Spalte] = 0;
                        }
                        if (lesen == true)
                        {
                            //Rows will be filled an added with data value strings
                            if (counter[Spalte] % 2 == 0)
                            {
                                result[0] = splitter[2];
                            }
                            else
                            {
                                result[1] = splitter[2];

                                dataGridView1.Rows.Add();
                                DGV[Spalte].Rows.Add();
                                int Zeile = (counter[Spalte] - 1) / 2;
                                DGV[Spalte][0, Zeile].Value = result[0];
                                DGV[Spalte][1, Zeile].Value = result[1];
                            }
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
                //Streamreader is closed
                file1.Close();
                counter[Spalte] = counter[Spalte] / 2;

                //Current datagridviw values are saved in arrays
                //The conversions will be calculated and saved in new arrays
                //So every unit gets its own array
                double[] eV2 = new double[counter[Spalte]];
                double[] meV2 = new double[counter[Spalte]];
                double[] cm2 = new double[counter[Spalte]];
                double[] CR2 = new double[counter[Spalte]];
                double[] CRmax2 = new double[counter[Spalte]];


                //Conversion calculation
                for (int i = 0; i < counter[Spalte]; i++)
                {
                    eV2[i] = Convert.ToDouble(DGV[Spalte][0, i].Value);
                    CR2[i] = Convert.ToDouble(DGV[Spalte][1, i].Value);
                    meV2[i] = 1000 * eV2[i];
                    cm2[i] = 8066 * eV2[i];
                }

                //Current file's arrays are saved in double arrays
                eV[Spalte] = eV2;
                CR[Spalte] = CR2;
                meV[Spalte] = meV2;
                cm[Spalte] = cm2;

                for (int i = 0; i < counter[Spalte]; i++)
                {
                    CRmax2[i] = CR2[i] / CR2.Max();
                }

                CRmax[Spalte] = CRmax2;

                //Chosen conversion replaces values in datagridview
                if (Hilfe == 1)
                {
                    for (int i = 0; i < counter[Spalte]; i++)
                    {
                        DGV[Spalte][0, i].Value = meV2[i];
                    }
                }
                else if (Hilfe == 2)
                {
                    for (int i = 0; i < counter[Spalte]; i++)
                    {
                        DGV[Spalte][0, i].Value = cm2[i];
                    }
                }

                if (Hilfe2 == 1)
                {

                    for (int i = 0; i < counter[Spalte]; i++)
                    {
                        DGV[Spalte][1, i].Value = CRmax2[i];
                    }
                }
            }
            catch
            {
                MessageBox.Show("Es ist ein Fehler beim Einlesen eingetreten");
            }
        }
    }
    /// Energy Unit
    /// Choses between eV, meV, 1/cm
    /// Datagridview values are replaced by the unit array values
    /// Hilfe... Saves current energy unit
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int L = comboBox1.SelectedIndex;
        try
        {
            if (L == 0)
            {
                Hilfe = 1;
                try
                {
                    for (int Spalte = 0; Spalte < Anzahl; Spalte++)
                    {
                        for (int i = 0; i < counter[Spalte]; i++)
                        {
                            DGV[Spalte][0, i].Value = meV[Spalte][i];
                        }
                    }
                }
                catch
                {
                }
            }
            if (L == 1)
            {
                Hilfe = 2;
                try
                {
                    for (int Spalte = 0; Spalte < Anzahl; Spalte++)
                    {
                        for (int i = 0; i < counter[Spalte]; i++)
                        {
                            DGV[Spalte][0, i].Value = cm[Spalte][i];
                        }
                    }
                }
                catch
                {
                }
            }
            if (L == 2)
            {
                Hilfe = 0;
                try
                {
                    for (int Spalte = 0; Spalte < Anzahl; Spalte++)
                    {
                        for (int i = 0; i < counter[Spalte]; i++)
                        {
                            DGV[Spalte][0, i].Value = eV[Spalte][i];
                        }
                    }
                }
                catch
                {
                }
            }
        }
        catch { }
    }

1 个答案:

答案 0 :(得分:0)

如果你不知道how to post your own answer?,请接受这些答案。

答案:

问题已解决。原因是dgv.autosizemode,当我在它运行的转换之前停用它时。

for (int Spalte = 0; Spalte < Anzahl; Spalte++)
                        {
                            DGV[Spalte].AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
                            for (int i = 0; i < counter[Spalte]; i++)
                            {
                                DGV[Spalte][0, i].Value = meV[Spalte][i];
                            }
                            DGV[Spalte].AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                        }