0

In my form, I have a panel with picture boxes on it. When the mouse hovers over the panel the picture boxes should show. When the mouse leaves, the picture boxes should hide.

At first I tried

panel1.visible = true;   

and

panel1.visible = false;

I thought this would do, but it doesn't work. Then I tried the following method

namespace Drawing_Program
{
    public partial class Form1 : Form
    {

        PictureBox[] Boxes = new PictureBox[12];

        public Form1()
        {           
            InitializeComponent();
            int i = 0;
            foreach (var pb in Controls.OfType<PictureBox>())
            {
                Boxes[i] = pb;                
                i++;
            }           

        }

        private void panel1_MouseHover(object sender, EventArgs e)
        {
            for (int i = 0; i < Boxes.Length; i++) {
                this.Boxes[i].Visible = true;  // error gives here
            }



        }

        private void panel1_MouseLeave(object sender, EventArgs e)
        {
            for (int i = 0; i < Boxes.Length; i++)
            {
                this.Boxes[i].Visible = false;
            }


        }

but I am getting the following error:

nullreferenceException was unhandled: Object reference not set to an instance of an object.

Please tell me what is wrong or how to do this properly.

NotMe
  • 87,343
  • 27
  • 171
  • 245
asdfkjasdfjk
  • 3,784
  • 18
  • 64
  • 104

3 Answers3

3

Your Form is not the direct parent of the PictureBox controls. So I'm betting your Boxes array is { null, null, null,... }. If panel1 is the direct parent of all of the PictureBox controls, you should be able to populate your Boxes array as follows:

Boxes = panel1.Controls.OfType<PictureBox>().ToArray();
JosephHirn
  • 720
  • 3
  • 9
  • +1 this also resolves the problem with the constant number in the array initializer `= new PictureBox[12]` which lead to the error IMO. – pascalhein Apr 04 '13 at 18:12
1

Probably one of Boxes[i] is null. Use the debugger to check this, and see this thread.

My guess is that you have less than 12 picture boxes and not all of your array elements are references to one of those, but rather remain uninitialized (null).

Your foreach-loop will only run as often as you have pictureboxes. If you only have 11 (or less), Boxes[11] will be null.

When changing the visibility and i reaches 11, with Boxes[i].Visible you are dereferencing null which probably causes your exception.

Community
  • 1
  • 1
pascalhein
  • 5,700
  • 4
  • 31
  • 44
0

try this
place your controls in a panel say(panel2) and sets it visibility to false . place this panel in another panel say(panel1) and on the mouse events of panel1 try this code

   private void panel1_MouseHover(object sender, EventArgs e)
        {
            panel2.Visible = true;
        }

   private void panel1_MouseLeave(object sender, EventArgs e)
       {
            panel2.Visible = false;
       }

i tried this it is working.

pascalhein
  • 5,700
  • 4
  • 31
  • 44
XTGX
  • 114
  • 9
  • it works but for my case panel1 and panel2 should be same height and width and it don't work since one panel is covered by another one – asdfkjasdfjk Apr 04 '13 at 18:15
  • @user1478137: Then you now have a layout issue that you need to resolve by repositioning the panels. – NotMe Apr 04 '13 at 18:16