【问题标题】:Winforms: Boolean logic not working properlyWinforms:布尔逻辑无法正常工作
【发布时间】:2019-06-09 06:18:11
【问题描述】:

我的情况是我有Form 1和Form 2。在Form 1中我有button1和button2。我在 Form1 中将 bool dtgmb 定义为 false。但是,如果单击 button2,则 dtgmb 为真。

在表格 2 中,我有 1 个 DTG 和 button3,其中行的所有数据都已定义。因此,我运行实例,如果dtgmb 为假(单击按钮1),则显示object[] rows 的内容,如果dtgmb 为真(单击按钮2),则显示object[] rows1 的内容。然后点击button3返回Form1。

但是,如果我先单击button1,然后返回Form 1并单击button2,Form2将两次显示object[] rows的显示内容。然后如果我关闭应用程序并重新启动,这一次我先点击button2,然后返回Form 1并点击button1,Form2将显示object[] rows1的两次显示内容。

下面是我的代码:

表格 1

public bool dtgmb = false;

private void button1_Click(object sender, EventArgs e)
{
    //Forms saved in class called FormsCollection
    FormsCollection.Form1.Hide();
    FormsCollection.Form2.Show();
}

private void button2_Click(object sender, EventArgs e)
{
    FormsCollection.Form1.Hide();
    dtgmb = true;
    FormsCollection.Form2.Show();
}

表格 2

private void Form2_Load(object sender, EventArgs e)
 {
   stuff(FormsCollection.Form1);
 }   

public void stuff(Form1 form)
{
    DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
    DataGridViewCheckBoxColumn check1 = new DataGridViewCheckBoxColumn();
    dataGridView4.ColumnCount = 1;
    dataGridView4.Columns[0].Width = 380;
    dataGridView4.Columns[0].Name = "Item";
    string[] row1 = new string[] { "Tables" };
    string[] row2 = new string[] { "Chairs" };
    string[] row3 = new string[] { "Lamps" };
    string[] row4 = new string[] { "Pillows" };
    string[] row5 = new string[] { "Blankets" };
    object[] rows = new object[] { row1, row2, row3, row4, row5 };
    object[] rows1 = new object[] { row1, row2, row3, row4 };

    if (form.dtgmb == false)
        foreach (string[] rowArray in rows)
        {
            this.dataGridView4.Rows.Add(rowArray);
        }
    else
        foreach (string[] rowArray in rows1)
        {
            this.dataGridView4.Rows.Add(rowArray);
        }

    check.HeaderText = "Pass";
    check1.HeaderText = "Fail";
    dataGridView4.Columns.Add(check);
    dataGridView4.Columns.Add(check1);
}

【问题讨论】:

  • 可能是因为忘记再次调用 stuff() 导致 Form2 没有刷新?
  • 因为您没有在button1_Click 中将dtgmb 设置为false
  • 您正在使用static 变量。 dtgmb 的值取决于您设置的最后一个值。它只会在重新启动应用程序时重置为默认值false
  • 嗨 Antoine 和 Gserg,我尝试了你们的建议,但仍然没有改变。
  • 嗨,约翰。如果我错了纠正我。嗯,我对使用静态的理解是因为这个布尔值总是调用按钮,按钮属性永远不会改变。因此,这就是我将其保留为静态的原因。如果我删除静态,则会发生对象引用错误,我不确定下一步该做什么。

标签: c# excel datagridview boolean-logic


【解决方案1】:

问题是由于dtgmb 被标记为静态。这意味着所有实例,无论它们的生命周期,都将共享相同的值。 静态变量在应用程序启动时被初始化为其原生默认值(例如,整数被初始化为零,而布尔值被初始化为 false)。

如果您删除静态修饰符,您将需要使用对表单的引用来调用 stuff 方法。由于您没有向我们展示您在哪里调用内容,因此我在此处发布了您发布的内容的重构。

Form1.cs

public bool dtgmb = false;

private void button1_Click(object sender, EventArgs e)
{
    //Forms saved in class called FormsCollection
    FormsCollection.Form1.Hide();
    FormsCollection.Form2.Show();
}

private void button2_Click(object sender, EventArgs e)
{
    FormsCollection.Form1.Hide();
    dtgmb = true;
    FormsCollection.Form2.Show();
}

Form2.cs

public void stuff(Form1 form)
{
    DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
    DataGridViewCheckBoxColumn check1 = new DataGridViewCheckBoxColumn();
    dataGridView4.ColumnCount = 1;
    dataGridView4.Columns[0].Width = 380;
    dataGridView4.Columns[0].Name = "Item";
    string[] row1 = new string[] { "Tables" };
    string[] row2 = new string[] { "Chairs" };
    string[] row3 = new string[] { "Lamps" };
    string[] row4 = new string[] { "Pillows" };
    string[] row5 = new string[] { "Blankets" };
    object[] rows = new object[] { row1, row2, row3, row4, row5 };
    object[] rows1 = new object[] { row1, row2, row3, row4 };

    if (form.dtgmb == false)
        foreach (string[] rowArray in rows)
        {
            this.dataGridView4.Rows.Add(rowArray);
        }
    else
        foreach (string[] rowArray in rows1)
        {
            this.dataGridView4.Rows.Add(rowArray);
        }

    check.HeaderText = "Pass";
    check1.HeaderText = "Fail";
    dataGridView4.Columns.Add(check);
    dataGridView4.Columns.Add(check1);
}

请记住,直接共享字段不是一个好习惯,但这有助于了解问题所在。

按照 cmets 编辑

我不清楚你想要达到什么目的。当您尝试将类型作为函数的正式参数传递时,您的代码不起作用。我重构了你的代码以便编译(但我不明白它应该如何表现):

Program.cs

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        new Form1().Show();
        Application.Run(new Form2());
    }

Form1.cs

public partial class Form1 : Form
{
    public bool dtgmb = false;

    public Form1()
    {
        InitializeComponent();

        FormCollection.Form1 = this;
    }


    private void button1_Click(object sender, EventArgs e)
    {
        //Forms saved in class called FormsCollection
        FormCollection.Form1.Hide();
        FormCollection.Form2.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        FormCollection.Form1.Hide();
        dtgmb = true;
        FormCollection.Form2.Show();
    }

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

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        FormCollection.Form1 = null;
    }
}

public static class FormCollection
{
    public static Form1 Form1;
    public static Form2 Form2;

}

Form2.cs

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();

        FormCollection.Form2 = this;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        stuff(FormCollection.Form1);
    }

    public void stuff(Form1 form)
    {
        DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
        DataGridViewCheckBoxColumn check1 = new DataGridViewCheckBoxColumn();
        dataGridView4.ColumnCount = 1;
        dataGridView4.Columns[0].Width = 380;
        dataGridView4.Columns[0].Name = "Item";
        string[] row1 = new string[] { "Tables" };
        string[] row2 = new string[] { "Chairs" };
        string[] row3 = new string[] { "Lamps" };
        string[] row4 = new string[] { "Pillows" };
        string[] row5 = new string[] { "Blankets" };
        object[] rows = new object[] { row1, row2, row3, row4, row5 };
        object[] rows1 = new object[] { row1, row2, row3, row4 };

        if (form.dtgmb == false)
            foreach (string[] rowArray in rows)
            {
                this.dataGridView4.Rows.Add(rowArray);
            }
        else
            foreach (string[] rowArray in rows1)
            {
                this.dataGridView4.Rows.Add(rowArray);
            }

        check.HeaderText = "Pass";
        check1.HeaderText = "Fail";
        dataGridView4.Columns.Add(check);
        dataGridView4.Columns.Add(check1);
    }

    private void Form2_FormClosed(object sender, FormClosedEventArgs e)
    {
        FormCollection.Form2 = null;
    }
}

请注意,上面的代码有几个问题:一是它依赖于表单的初始化方式。

【讨论】:

  • 嗨,码头。欣赏解释。使用您的代码,仍然没有任何更改。因此,我编辑了上面的代码以显示我如何调用stuff()。希望你能帮忙。感谢您的宝贵时间。
  • 嗨,码头。老实说,我的大部分代码都是从我发现的其他论坛/网站复制粘贴的,我还没有真正理解这样做的原因。使用您的代码,发生了错误,是的,肯定与我在FormsCollection.cs 下初始化这些表单的方式有关。我很可能会修改我的整体代码。感谢您的努力,先生。
猜你喜欢
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多