简体   繁体   English

为什么无法在Method中访问数据?

[英]Why can't I reach data within Method?

I'm currently trying to fill a TableLayoutPanel through a method which goes as follows: 我目前正在尝试通过如下方法填充TableLayoutPanel

private int _rowCount;
public void InitPaths()
{
    int c = 1;
    int a = 1;

    while (a < _PathRows.Length - 1)
    {
        var label = new Label();
        //
        // Label - Format.
        //
        label.Dock = DockStyle.Fill;
        label.AutoSize = false;
        label.Text = _pfadZeilen[a];
        label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        label.Size = new System.Drawing.Size(22, 13);
        label.BackColor = System.Drawing.Color.Transparent;
        TableLayoutP.Controls.Add(label, 3, c);

        //Checkboxen Einfügen
        var cbox = new CheckBox();
        //
        //Checkbox Format.
        cbox.Anchor = System.Windows.Forms.AnchorStyles.None;
        cbox.AutoSize = true;
        cbox.CheckAlign = System.Drawing.ContentAlignment.MiddleCenter;
        cbox.Name = "checkBoxPfad" + a;
        cbox.Size = new System.Drawing.Size(15, 14);
        cbox.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        cbox.UseVisualStyleBackColor = true;
        TableLayoutP.Controls.Add(cbox, 0, c);
        a++;
        c++;

    }

    this._rowCount = BibTable.GetRowHeights().Length; // which seems to be Holding the value only within the method
}

and then delete all rows on Action, through the following Method: 然后通过以下方法删除Action上的所有行:

public void RemoveRows()
{
    for (int row = _rowCount; row >= 0; row--)
    {
        BibTable.RowStyles.RemoveAt(row);
        BibTable.RowCount--;
    }
}

Now the Problem is, if I try to do anything with the TableLayoutP outside of the method where all rows are initialized, it will tell me: 现在的问题是,如果我尝试在初始化所有行的方法之外对TableLayoutP进行任何操作,它将告诉我:

Object reference not set to the instance of an object. 对象引用未设置为对象的实例。

What can I do? 我能做什么? Is there a way to get a method inside a method (I'm realising just how stupid that sounds while typing it) or any other way to deal with this Situation? 有没有办法在方法内部获取方法(我正在意识到键入它时听起来多么愚蠢)或其他任何方法来处理这种情况?

You are ittering through GetRowHeights() , returning the height of each row. 您正在通过GetRowHeights() ,返回每一行的高度。 But you are deleting from the RowStyles collection which is not directly related to the first collection. 但是,您将从与第一个集合没有直接关系的RowStyles集合中删除。 I assume that GetRowHeights() returns much more rows, than RowStyles has. 我假设GetRowHeights()返回的行比RowStyles

Why not: 为什么不:

BibTable.RowCount = 0;
BibTable.RowStyles.Clear();

You are ittering through GetRowHeights() , returning the height of each row. 您正在通过GetRowHeights() ,返回每一行的高度。 But you are deleting from the RowStyles collection which is not directly related to the first collection. 但是,您将从与第一个集合没有直接关系的RowStyles集合中删除。 I assume that GetRowHeights() returns much more rows, than RowStyles has. 我假设GetRowHeights()返回的行比RowStyles

Why not: 为什么不:

BibTable.RowCount = 0;
BibTable.RowStyles.Clear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM