How to get instance of a container from the control inside

E

Efkas

I have a class extending PictureBox. I use it as container adding a
Label control inside the PictureBox.

I use this kind of "widget" in another class, and I want to catch the
event on mouse click on the label only. That works.

But in the event function, I want to call a function to modify the
container, but I don't know how to do that.

Example:
namespace xxx
class myPictureBox : System.Windows.Forms.PictureBox
{
private Label txt;
public myPictureBox()
{
txtTab = new Label();
this.Controls.Add(txtTab);
}
public void SetPictureBoxAttributes(//some stuff)
{
//set some stuff
}
public Label GetTabLabel()
{
return txt;
}
}
class myApp
{
public myApp()
{
xxx.MyPictureBox pb = new xxx.MyPictureBox();
pb.SetPictureBoxAttributes(//some stuff);

pb.GetTabLabel().MouseDown += new
System.Windows.Forms.MouseEventHandler(this.pblabMouseDown);
}
public void thetabMouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
Label currentlabel = (Label)sender;
currentlabel.????? // what to call to retreive the container pb
and
// call the function
SetPictureBoxAttributes();
// ???????????????????????
}
}


Thank you
François

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
N

Neil Stevens

You need to add the label to the picture boxes Controls collection, then you
can reference it with the Parent property

Label lbl = (Label)sender;
PictureBox pb = (PictureBox)lbl.Parent;

pb.XXXX = XXXX

Hope that helps.

Regards
Neil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top