I've created a method in my application which creates a number of panels at runtime. Within the loop that creates the panels, I have created a panel MouseMove event which controls the position of a tooltip based on the position of the mouse pointer in any one of the panels created.
I'm getting this error when I compile, I appreciate that pnlOverview
is a creation within a different constructor, but for the life of me I can't understand how I can reach the pnlOverview
instance from within the event handler.
Can anybody point me in the right direction?
Here's just the code I think you need to look at:
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);
}
pnlOverview
won't be in scope in your MouseMove handler, because it is a local variable in CreatePanels().
sender
should be the control you're mousemoving over, but you'll need to cast it to the appropriate type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.