如何在 C# 中使用继承从另一个 class 调用和对象

How to use inheritance for calling and object from another class in C#

我正在尝试从另一个对象上的 MouseHover 事件调用按钮的可见性。 我想要做的是当我将鼠标悬停在 pictureBox 上以将附加到该 pictureBox 的按钮设置为可见时,默认情况下创建按钮时它是不可见的。 当我尝试从 MouseHover 事件调用它时,它说该按钮为空。我不太擅长继承,所以我有点被困在这里,我们将不胜感激。 这是代码(我想做的只是在 MouseHover 事件上):

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();
}

为null,因为事件的发送者是图片,不是按钮。 您可以简单地在 class 级别

声明按钮
private  Button btn;
private void Button1_Click(object sender, EventArgs e)
{
    FlowLayoutPanel flP = new FlowLayoutPanel();
    PictureBox picB = new PictureBox();
    Label laB = new Label();
    btn = new Button();

然后直接让它可见

private void picB_MouseHover(object sender, EventArgs e)
{
    bt.Visible = true;
}

编辑

或者你可以定义一个Dictionary,将找到的PictureBox与相应的Button相关联

private var btnDict = new Dictionary<PictureBox,Button>();

并且当您创建它们时,您还可以 link 它们,

PictureBox picB = new PictureBox();
Label laB = new Label();
Button btn = new Button();
btnDict.Add(picB,btn);

以便您可以使用类似

的命令检索按钮
PictureBox pb = (PictureBox)sender;
var btn = btnDict[pb];

一种方法是将 Button 变量存储在图片框的标记 属性 中:

PictureBox picB = new PictureBox();
Button btn = new Button();
picB.Tag = btn;

及以后,在您的鼠标悬停处理程序中

private void picB_MouseHover(object sender, EventArgs e)
{
    PictureBox pb = (PictureBox)sender;
    Button bt = pb.Tag as Button;
    //bt.Visible = true;
}