1

I designed a Form that has Panels and Labels. I made the cursor property of the panels by hand. But when I click on the panel I get this Exception :

Unable to cast object of type 'System.Windows.Forms.Panel' to type 'System.Windows.Forms.Label'

This is my code:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Mancula
{

    public partial class Form2 : Form
    {  
        private System.Windows.Forms.Panel[] pan;
        private System.Windows.Forms.Label[] lab;
        Graphics g;
        Pen pen = new Pen(Color.Red);
        SolidBrush br = new SolidBrush(Color.Red);
        Rectangle rec = new Rectangle(10, 10, 30, 30);

        int player1 = 49;
        int player2 = 49;
        int playerturn = 0;

        public Form2()
        {
            InitializeComponent();

            label1.Text = "0";
            label2.Text = "0";

        }

        public void startgame()
        {
            if (radioButton1.Checked == true)
            {
                playerturn = 1;
            }
            else
            {
                playerturn = 2;
            }

        }

        private void button15_Click(object sender, EventArgs e)
        {
            button1.Enabled = button2.Enabled = true;
            button3.Enabled = button4.Enabled = true;
            button5.Enabled = button6.Enabled = true;
            button7.Enabled = button8.Enabled = true;
            button9.Enabled = button10.Enabled = true;
            button11.Enabled = button12.Enabled = true;
            button13.Enabled = button14.Enabled = true;

            panel1.Enabled = panel2.Enabled = true;
            panel3.Enabled = panel4.Enabled = true;
            panel5.Enabled = panel6.Enabled = true;
            panel7.Enabled = panel8.Enabled = true;
            panel9.Enabled = panel10.Enabled = true;
            panel11.Enabled = panel12.Enabled = true;
            panel13.Enabled = panel14.Enabled = true;

            label1.Text = player1.ToString();
            label2.Text = player2.ToString();
            startgame();
            panel15.Enabled = false;

            for (int i = 0; i < 14; i++)
            {
                 pan [i].Click += new System.EventHandler(this.ClickHandler);
            }

        }

        private void ClickHandler(object sender, EventArgs e)
        {
            Panel temppanel = (Panel)sender;             
            Label templab = (Label)sender;

            if (playerturn == 1)
            {
                if (player1 == 0)
                {
                    playerturn = 2;
                }
                else
                {
                    player1 = player1 - 1;
                    if (templab.Text != "7")
                    {
                        g = temppanel.CreateGraphics();
                        g.DrawRectangle(pen, rec);
                        label1.Text = player1.ToString();
                    }
                }
            }
            else
            {
                if (player2 == 0)
                {
                    playerturn = 1;
                }
                else
                {
                    player2 = player2 - 1;
                    if (templab.Text != "7")
                    {
                        g = temppanel.CreateGraphics();
                        g.DrawRectangle(pen, rec);
                        label2.Text = player2.ToString();
                    }
                }
            }
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            pan = new Panel[14] { panel1, panel2, panel3, panel4, panel5, panel6,panel7,
                                  panel8,panel9,panel10,panel11,panel12,panel13,panel14
                                };
            lab = new Label[14] { lab1,lab2,lab3,lab4,lab5,lab6,lab7,lab8,lab9,lab10,lab11,lab12,lab13,lab14};

        }
    }
}

3 Answers 3

3

I think your mistake is this line:

    Panel temppanel = (Panel)sender;             
    Label templab = (Label)sender;

You should check the type before doing the cast.

You can do this:

if(sender.GetType() == typeof(Panel))
{
     Panel temppanel = (Panel)sender; 
}
else
{
     Label templab = (Label)sender;
}

Anyway, it doesn´t seem a good idea to assign the same handler to different types of objects. My recommendation would be to make a handler for each type of control.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Can I use both (Panel) and (Label) ?
given the code above you can do that. You can also use an IF... ELSE IF condition to make sure it only would be casting for Labels and Panels.
Thank you so much.
You Got it . Thank you :) .
1

In your event handler you have:

Panel temppanel = (Panel)sender;             
Label templab = (Label)sender;

But sender can be either Panel or Label, that why one of this cast will fail anyway.

1 Comment

Thanks. How can I use both (Panel) and (Label) ?
0

Your problem is on this line:

Label templab = (Label)sender;

When you click on the panel, the sender is a Panel, not a Label, so you can't cast it as such.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.