如何在c#中使用继承从另一个类调用对象

本文关键字:另一个 调用 对象 继承 | 更新日期: 2023-09-27 18:03:54

我试图从另一个对象上的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();
}

如何在c#中使用继承从另一个类调用对象

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

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

为空,因为事件的发送者是图片,而不是按钮。您可以简单地在类级别声明按钮

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

,当你创建它们时,你也可以链接它们,

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

以便您可以使用像

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