简体   繁体   English

System.InvalidCastException C#

[英]System.InvalidCastException C#

I designed a Form that has Panels and Labels. 我设计了一个包含面板和标签的表格。 I made the cursor property of the panels by hand. 我手工制作了面板的cursor属性。 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' 无法将类型为“ System.Windows.Forms.Panel”的对象转换为类型为“ 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};

        }
    }
}

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. 我的建议是为每种控件类型创建一个处理程序。

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. 但是发件人可以是PanelLabel ,这就是为什么其中一个强制类型转换都会失败的原因。

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. 在面板上单击时, senderPanel ,而不是Label ,因此您不能像这样投射它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM