PictureBox Typecast

  • Thread starter Thread starter Larry
  • Start date Start date
L

Larry

I have several picturebox controls on a WindowsForm in
VS.NET 2003.

On the Click event, the object sender argument is
available to the event.

I want to be able to pass the picturebox object to a
general function to change the bmp associated.

How do I pass the sender object to another function and
then have access to the PictureBox Image property?
 
Larry -

Casting can be a bit different in each language. You did not mention
your language. In VB it might look like this...

Private Function AnotherFunction(ByRef pb As PictureBox) As Boolean
' Now you can reference pb.Image()
End Function

Private Sub PictureBox1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles PictureBox1.Click
Call AnotherFunction(CType(sender, PictureBox))
End Sub

HTH - Best Regards, Lee Gillie - Spokane, WA
 
Code below.

[C#]
private void pictureBox1_Click(object sender, System.EventArgs e)
{
ChangeImage((PictureBox)sender);
}
private void ChangeImage(PictureBox picBox)
{
// picBox.Image = (pic)
}


[VB]
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles PictureBox1.Click
ChangeImage(CType(sender, PictureBox))
End Sub
Private Sub ChangeImage(ByVal picBox As PictureBox)
' picBox.Image = (pic)
End Sub
 
* "Lee Gillie said:
Call AnotherFunction(CType(sender, PictureBox))

In this case, using 'DirectCast' instead of 'CType' is the preferred
method.
 
Back
Top