如何在C#中使用继承来调用另一个类的调用和对象

时间:2016-08-24 08:01:19

标签: c# inheritance button picturebox mousehover

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

2 个答案:

答案 0 :(得分:0)

它为null,因为事件的发件人是图片,而不是按钮。 您只需在班级声明按钮

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

修改

或者您可以定义一个词典,将找到的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];

答案 1 :(得分:0)

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

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

以后,在你的MouseHover Hanlder

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