CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2009
    Posts
    25

    How to load a picturebox from different classes?

    I have a mainform.

    On this main form are two controls: 1) a thumbnail panel and 2) a picturebox. I created them as separate class files so they I can use them with any programs. I just dropped both controls in a split panel on the mainform. Thumbnail on left and picturebox on right.

    When the user clicks the thumbnail I want the picture box to show.

    However the thumbnail click events are associated with the thumbnail. The picturebox is supposed to be loaded within the click event of the thumbnail but doesnt. I need help.

    Here is my code

    private void thumbnail_MouseClick(object sender, MouseEventArgs e)
    {


    ImageBox kimg = new ImageBox();

    int iImgNum = Convert.ToInt16(((PictureBox)(Control)sender).Name);
    string ImgNum = ((PictureBox)(Control)sender).Name.ToString();
    string ImgFileName = @"C:\Images\Image" + ImgNum + ".tif";



    kimg.GetImage(ImgFileName);
    }


    namespace ImageTest
    {
    public partial class ImageBox : UserControl
    {
    public ImageBox()
    {
    InitializeComponent();
    }


    public void GetImage(string file)
    {

    this.picturebox1.image= Image.FromFile( file
    this.picturebox1.show();


    }
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to load a picturebox from different classes?

    Besides some other problems with the code, you are simply creating a new ImageBox object which goes out of scope when the method returns, so it is destroyed. You need to either add an ImageBox control to your form in the designer or add it to teh form's Controls collection inside of that click event handler.

  3. #3
    Join Date
    Aug 2009
    Posts
    25

    Re: How to load a picturebox from different classes?

    Please forgive my lack of knowledge Im new to C#. I usually do VB.

    So if I want to load that picturebox from a thumbnail click event that is in another class what is the easiest way this can be done?

    I cant call a new instance of the mainform either. (Like Mainform m = new mainform() ; m.picturebox1 = Image.fromfile(path))

    That doesnt work either.

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: How to load a picturebox from different classes?

    Here's one technique for doing this kind of thing Synchronising [User]Controls).

    Create an SelectionChanged event in your ThumbnailControl. Fire it whenever the User selects a different Thumbnail. Have your Main Form subscribe to that event. In the event handler in your main form, call an ImageBox method (which has a class-scope member, as Ed pointed out) and update your image.

    Here's something to help you get started with the syntax for declaring an event in the thumbnail class. The ThumbnailEventArgs (lightweight) class should be derived from EventArgs and contain a 'String FileName' of the image. This can then be retrieved by the ImageBox class.
    Code:
            public event OnSelectionChangedEventHandler OnSelectionChanged;
            public delegate void OnSelectionChangedEventHandler(ThumbnailControl sender, ThumbnailEventArgs e);
    In the Thumbnail_MouseClick() fire the event something like this:
    Code:
            {
                if (OnSelectionChanged!= null)
                {
                    // fire the event
                    ThumbnailEventArgs eventArgs = new ThumbnailEventArgs (imageFileName);
                    OnSelectionChanged(this, eventArgs);
                }
            }
    subscribe to the event in your main forms constructor like this:
    Code:
                ucThumbnailControl1.OnSelectionChanged += new ucThumbnailControl.OnSelectionChangedEventHandler(ucThumbnailControl.OnSelectionChanged );
    in the main form...
    Code:
            void ucThumbnailControl.OnSelectionChanged (ThumbnailControl sender, ThumbnailEventArgs e)
            {
                ucImageControl1.UpdateImage(e.FileName);
            }
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured