【问题标题】:ListView wont refresh or update in C# Forms applicationListView 不会在 C# Forms 应用程序中刷新或更新
【发布时间】:2012-08-14 08:35:04
【问题描述】:

我正在尝试在 MS Visual Studios 中编写一个简单的 C# 密码保护实用程序。我在 formA 中有一个 listView,我想使用 formB 向列表中添加一个新项目。

FormB 调用作为 formA 成员的 forceAdd。 forceAdd 然后更新数据集并尝试通过调用 populateList() 来刷新 listView。

问题在于,直到我通过 formA 中的按钮或复选框调用我的 populateList() 函数时,listView 才反映更改。

例如,我可以在我的代码中调用 populateList() 50 次但无济于事,但只要我从 formA 上的按钮调用它就可以了。

(DieselPass = fromA, newForm = FormB, newForm::Save 调用 forceAdd, formA::new 打开 newForm)

(当我连接 formA::Edit... 按钮以填充并在保存后单击它时,listView 会更新)

我是新用户...这张图片说明了这一点: http://i50.tinypic.com/wk6bys.jpg

    public void forceAdd(String a, String u, String p)
    {
        accountsDataSet.tblAccounts.AddtblAccountsRow(a, u, p);
        tblAccountsTableAdapter.Update(accountsDataSet.tblAccounts);
        populateList();
    }

    private void populateList()
    {
        //make sure data set is up to date
        this.tblAccountsTableAdapter.Fill(this.accountsDataSet.tblAccounts);
        //clear existing items
        listView1.Items.Clear();
        for (int i = 0; i < accountsDataSet.tblAccounts.Count; i++)
        {
            String[] wtf;
            if (maskPasswords)
                wtf = new string[] { accountsDataSet.tblAccounts[i].Account, accountsDataSet.tblAccounts[i].Username };
            else
                wtf = new string[] { accountsDataSet.tblAccounts[i].Account, accountsDataSet.tblAccounts[i].Username, accountsDataSet.tblAccounts[i].Password };
            lvi = new ListViewItem(wtf);
            Console.WriteLine(lvi.ToString());
            listView1.Items.Add(lvi);
        }
    }

我已经尝试过的事情包括:

    listView1.Update();
    listView1.BeginUpdate();
    ...
    listView1.EndUpdate();
    listView1.Refresh();
    listView1.Clear();
    listView1.RedrawItems(0,i,t/f);
    this.Update();
    this.Refresh();

任何帮助或提示将不胜感激,谢谢。

更多代码:

在formB中设置FormA参考:

    myNewForm.getForm = this;

FormB 对 formA 的引用:

    private mainForm myForm = new mainForm();
    public mainForm getForm
    {
        get { return myForm; }
        set { myForm = value; }
    }        

FormB 调用 FormA 的函数:

    myForm.forceAdd(accountBox.Text.ToString(), usernameBox.Text.ToString(), passwordBox.Text.ToString());

这是我需要执行“调用/委托”的实例吗?

编辑 2:

对不起,我不够清楚,FormB 调用FormA::populateList(),我将控制台输出放在函数中以确认这一点,它成功地将项目添加到数据集中但不会刷新列表。它就像它知道这是一个不会让它刷新的外部形式。

我也公开了 populateList,没用

Visual Studio 不允许我从 (mainForm)this.Parent. 调用 pupulateList(),即使在调用 myNewForm.ShowDialog(this); 之后我是否必须包含某些内容或以其他方式传递它?

我通过myForm 对formA 的引用有效,只是不适用于更新列表..?

编辑 3: 这也不起作用:

((mainForm)this.Parent).populateList();

【问题讨论】:

  • formB 中包含调用formA 中的函数的代码

标签: c# forms listview refresh


【解决方案1】:

FormB 必须具有对 FormA 实例的引用。您可以在单击插入按钮时将此引用传递给 FormB:

在甲型

FormB PWDForm= new FormB();
FormB.ShowDialog(this);

接下来在 FormB 上,您可以像这样访问 FormA:

(FormA)this.Parent.populateList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2019-04-23
    • 2012-05-15
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    相关资源
    最近更新 更多