I am trying to call a button's visibility, from a MouseHover event on another object. What I'm trying to do is when I mouse over a pictureBox to set the button that is attached to that pictureBox to be visible, on default the button when it is created is invisible. When I try to call it from the MouseHover event it says that the button is null. I'm not that good with inheritance so I'm kinda stuck here, any help would be appreciated. Here is the code (what I'm trying to do is only on MouseHover event):
private void Button1_Click(object sender, EventArgs e)
{
FlowLayoutPanel flP = new FlowLayoutPanel();
PictureBox picB = new PictureBox();
Label laB = new Label();
Button btn = new Button();
picB.Size = new Size(130, 70);
laB.Size = new Size(130, 20);
flP.Size = new Size(130, 90);
btn.Size = new Size(20, 20);
laB.Text = "Text";
laB.Name = "Name";
flP.Name = "Name";
btn.Text = "X";
btn.Name = "Name";
btn.Visible = false;
flP.Controls.Add(picB);
flP.Controls.Add(laB);
picB.Controls.Add(btn);
flP.Location = new System.Drawing.Point(3, 3);
laB.Location = new System.Drawing.Point(3, 70);
btn.Location = new System.Drawing.Point(100, 5);
mainFLP.Controls.Add(flP);
picB.MouseHover += picB_MouseHover;
picB.DoubleClick += picB_DoubleClick;
}
private void picB_MouseHover(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
Button bt = pb.Parent as Button;
//bt.Visible = true;
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
FlowLayoutPanel flP = new FlowLayoutPanel();
PictureBox picB = new PictureBox();
Label laB = new Label();
Button btn = new Button();
picB.Size = new Size(130, 70);
laB.Size = new Size(130, 20);
flP.Size = new Size(130, 90);
btn.Size = new Size(20, 20);
flP.Name = i.ToString();
laB.Name = "Link";
laB.Text = "Name";
btn.Text = "X";
btn.Name = "b" + i.ToString();
btn.Visible = false;
flP.Controls.Add(picB);
flP.Controls.Add(laB);
picB.Controls.Add(btn);
flP.Location = new System.Drawing.Point(3, 3);
laB.Location = new System.Drawing.Point(3, 70);
btn.Location = new System.Drawing.Point(100, 5);
mainFLP.Controls.Add(flP);
picB.MouseHover += picB_MouseHover;
picB.DoubleClick += picB_DoubleClick;
}
}
private void picB_DoubleClick(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
FlowLayoutPanel flp = pb.Parent as FlowLayoutPanel;
flp.Dispose();
}
One method can be to store the Button variable in tag property of your picture box:
PictureBox picB = new PictureBox();
Button btn = new Button();
picB.Tag = btn;
and later, in your MouseHover Hanlder
private void picB_MouseHover(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
Button bt = pb.Tag as Button;
//bt.Visible = true;
}