C# - ListView不会从不同的表单刷新

时间:2017-12-08 10:53:19

标签: c# windows forms listview refresh

// PaddlerList (Form 1)
        public void RefreshListView()
        {
            PaddlerListView.Items.Clear();
            PaddlerToList();
            PaddlerListView.Refresh();
            Console.WriteLine("Refresh....");
        }
        public void PaddlerToList() // Just adds 
        {
            for (int i = 1; i < Main.paddlerList.Count(); i++) // Repeats for all
            {
                string[] array = new string[4];
                ListViewItem item;
                array[0] = Main.paddlerList[i].FirstName.ToString();
                array[1] = Main.paddlerList[i].LastName.ToString();
                array[2] = Main.paddlerList[i].Weight.ToString();
                array[3] = Main.paddlerList[i].PreferredSide.ToString();
                item = new ListViewItem(array);
                PaddlerListView.Items.Add(item);
            }
        }

        private void RefreshList_button_Click(object sender, EventArgs e)
        {
            RefreshListView();
        }


// NewPaddler (Form 2)
            private void SubmitPaddler_button_Click(object sender, EventArgs e)
            {
            // Code here
            PaddlerList ListViewRefresh = new PaddlerList(); // Creates an instant of the other form, so it can run the procedure
            ListViewRefresh.RefreshListView();
            }

添加新的paddler(按下提交按钮)后,控制台会输出&#34; Refresh ....&#34;显示该函数已运行,但ListView不刷新

但是,当我在与ListView相同的表单上设置一个按钮时,当按下按钮时,它会使用新项目刷新ListView。

我无法解决问题所在?我认为它与ListView的保护级别有关。

谢谢!

2 个答案:

答案 0 :(得分:0)

创建新的PaddlerList对象时,所有成员变量(例如您的Items)都会重新开始。我认为你需要将原始的PaddlerList传递给你的第二张表格才能实现正确的表格。

答案 1 :(得分:0)

我找到了解决方案:

新Paddler表格

    // NewPaddler Form

PaddlerList ListObject_global = null;
public NewPaddler(PaddlerList PaddlerList_Object)
{
  InitializeComponent();
  FormBorderStyle = FormBorderStyle.FixedDialog;
  ListObject_global = PaddlerList_Object;
}


if (ListObject_global != null)
{
  ListObject_global.RefreshListView(); // Runs function in PaddlerList
}

Paddler List Form

// PaddlerList Form
public void RefreshListView()
{
  PaddlerToList();
  PaddlerListView.Refresh();
  Console.WriteLine("Refresh....");
}