0

I've been working on making a project of mine more modular. Something I've wanted to do is have multiple buttons use the same function when they perform a similar action, but with different values. I've been stuck on trying to apply this to the following situation: "When this button is clicked, have the user select an image, and then have a PictureBox display the selected image". Each button has its own PictureBox. All Controls have been created before runtime. Hope that makes sense!

My last attempt can be seen in the code below- I have tried assigning the Controls(Button and PictureBox) to variables to be stored together in a class. There's 6 of these classes all included within a single List. I've also tried to store only the Control Names and then using this.Controls.Find to retrieve the Controls. I've tried quite a few smaller changes such as passing by reference, making the List static, and things such as that would (somehow)magically do the trick- I've gotten desperate.

    public class score_control
    {
        public Button score_button;
        public PictureBox score_picture;
        public int picture_index;
    }
    public List<string> score_boxes_names = new List<string>();
    public List<score_control> score_boxes = new List<score_control>();

    public void add_score_control(Button button, PictureBox pictureBox)
    {
        score_control new_score = new score_control();
        new_score.score_button = button;
        new_score.score_picture = pictureBox;
        new_score.picture_index = score_boxes.Count();
        score_boxes.Add(new_score);
        score_boxes_names.Add(button.Name);
    }

    public score_control find_score_control(string name)
    {
        int index = score_boxes_names.IndexOf(name);
        return score_boxes[index];
    }
    public frm_settings()
    {
        InitializeComponent();

        add_score_control(btn_score1_image1, pic_score1_image1);
        add_score_control(btn_score1_image2, pic_score1_image2);
        add_score_control(btn_score1_image3, pic_score1_image3);
        add_score_control(btn_score2_image1, pic_score2_image1);
        add_score_control(btn_score2_image2, pic_score2_image2);
        add_score_control(btn_score2_image3, pic_score2_image3);

    }
    private void score_button_Click(object sender, EventArgs e)
    {
        Button image_button = (Button)sender;
        if (ofd_png.ShowDialog() == DialogResult.OK)
        {
            score_control clicked_control = find_score_control(image_button.Name);
            score_image[clicked_control.picture_index] = ofd_png.FileName;

            clicked_control.score_picture.Image = Image.FromFile(ofd_png.FileName);               
        }
    }

The problem seems centered around this line:

clicked_control.score_picture.Image = Image.FromFile(ofd_png.FileName);

The program throws a NullReferenceException , but clickedcontrol is being recognized in the Local Watch, as well as score_image being noted to be a PictureBox(as it should be).

When I instead held the Control Names in the class, I had broke this line down into multiple lines, but the following line produced a NullReferenceException:

Control[] find_control = this.Controls.Find(clicked_control.score_picture, true);

In this case, clicked_control.score_picture would be a string containing the PictureBox Name. Again, the Local Watch showed that it clicked_control was not null, and neither was score_picture.

Any help figuring out how to properly store a Control within a variable to later be used to modify that Control's properties would be greatly appreciated.

  • Cannot reproduce, your code works fine here. Maybe the root cause is somewhere else? Btw, I have to comment out `score_image[clicked_control.picture_index] = ofd_png.FileName;` to make it work. – dontpanic Jan 22 '19 at 02:19
  • That actually helped a ton! Turns out the issue is entirely with the line you commented out. I'll be able to clean up that issue pretty easily and move on. I really appreciate it. – Dan Sanchez Jan 22 '19 at 02:28
  • @dontpanic could you consider making your comment the answer so it can be accepted here as the solution. – Matt Jan 22 '19 at 02:39

1 Answers1

0

dontpanic was able to help me out with this one. The issue was actually outside of this code - it had to do with the line score_image[clicked_control.picture_index] = ofd_png.FileName;. The way score_image was initialized as an array was incorrect. Fixing that made everything work fine.