我在我的应用程序中创建了一个方法,它在运行时创建了许多面板。在创建面板的循环中,我创建了一个面板 MouseMove 事件,该事件根据鼠标指针在所创建的任何一个面板中的位置来控制工具提示的位置。
我在编译时遇到了这个错误,我很欣赏这pnlOverview
是在不同的构造函数中创建的,但是对于我来说,我无法理解如何pnlOverview
从事件处理程序中访问实例。
谁能指出我正确的方向?
这只是我认为您需要查看的代码:
public void CreatePanels()
{
int PanelPosX = 50;
int PanelPosY = 500;
int LabelPosX = 10;
int LabelPosY = 10;
for (int i = 0; i < (Convert.ToInt32(txtNoOfPanels.Text)); i++)
{
// Create a new panel, each with a unique label identifying the inspector
Panel pnlOverview = new Panel();
pnlOverview.Name = "InspectorPanel" + (i + 1).ToString();
pnlOverview.Text = "Inspector Panel " + (i+1).ToString();
pnlOverview.Location = new Point(PanelPosX, PanelPosY);
pnlOverview.Size = new Size(1200, 180);
pnlOverview.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
Controls.Add(pnlOverview);
pnlOverview.Paint += new PaintEventHandler(newPanelPaint);
// Create a MouseMove event for each panel created
pnlOverview.MouseMove += new MouseEventHandler(pnlOverview_OnMouseMove);
Label lblInspectorName = new Label();
lblInspectorName.Name = "InspectorName" + (i+1).ToString();
lblInspectorName.Text = " Inspector " + (i+1).ToString();
lblInspectorName.Width = 100;
lblInspectorName.Height = 13;
lblInspectorName.Location = new Point(LabelPosX, LabelPosY);
lblInspectorName.Size = new Size(82, 13);
pnlOverview.Controls.Add(lblInspectorName);
PanelPosY += 190;
}
return;
}
// Show a tooltip
public void pnlOverview_OnMouseMove(object sender, MouseEventArgs e)
{
toolTip1.Show("HELLO", this, new Point(pnlOverview.Left + e.X + 1, pnlOverview.Top + e.Y + 1), int.MaxValue);
}