簡體   English   中英

如何使用繼承從C#中的另一個類進行調用和對象

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

我試圖從另一個對象的MouseHover事件調用按鈕的可見性。 我要做的是,當我將鼠標懸停在pictureBox上以將附加到該pictureBox的按鈕設置為可見時,默認情況下,創建該按鈕時該按鈕是不可見的。 當我嘗試從MouseHover事件調用它時,它說該按鈕為null。 我對繼承不是很好,所以我有點被困在這里,任何幫助將不勝感激。 這是代碼(我想做的只是在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,因為事件的發送者是圖片,而不是按鈕。 您只需在類級別聲明按鈕

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

一種方法是將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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM