第一個問題是,在form1
頂我:如何在Windows窗體中輕鬆替換另一個具有相同名稱的控件?
ListViewNF lvnf;
的ListViewNF
是form1
類:
class ListViewNF : System.Windows.Forms.ListView
{
public ListViewNF()
{
//Activate double buffering
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
//Enable the OnNotifyMessage event so we get a chance to filter out
// Windows messages before they get to the form's WndProc
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
}
protected override void OnNotifyMessage(System.Windows.Forms.Message m)
{
if (m.Msg != 0x14)
{
base.OnNotifyMessage(m);
}
}
}
然後在Form1構造我正在做的實例爲lvnf
變量集大小,位置並將此控件lvnf
添加到form1
控件。 然後在我的程序中,我在某些地方使用了lvnf
。
但現在我想建立一個新UserControl
調用它lvnf
所以當我拖動UserControl
從工具箱中form1
設計師將取代我創建的lvnf
。
第一個問題是,一旦我刪除該行:
ListViewNF lvnf;
我得到許多錯誤,不能看到Form1設計我得到一個錯誤,因爲lvnf
不存在。
如果我不刪除它並拖動新的UserControl
它會給新的控件另一個名稱,我希望它是lvnf
。我不想在我的運行時代碼中改變任何與lvnf
相關的設計中的控件。
這是UserControl
的代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Pop3_Emails
{
public partial class ListViewNF : UserControl
{
public ListViewNF()
{
InitializeComponent();
}
}
}
我應該做lvnf
的所有事件將它們移動到UserControl
代碼?或者全部留在form1
?
主要問題是如何使用UserControl
作爲lvnf
。 另外我需要使用類ListViewNF
中的代碼。
真是一團糟。一般來說,我想要做的是在form1
設計器中控制lvnf
ListViewNF
,而不是運行時代碼中的控件。