System.InvalidCastException C#

时间:2016-12-27 19:01:59

标签: c# winforms

我设计了一个包含面板和标签的表单。 我手工制作了面板的光标属性。 但是当我点击面板时,我得到了这个例外:

  

无法转换类型为#System; Windows.Forms.Panel'的对象。输入' System.Windows.Forms.Label'

这是我的代码:

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 个答案:

答案 0 :(得分:2)

我认为你的错误是这一行:

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

你应该在进行演员表之前检查一下类型。

你可以这样做:

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

无论如何,将相同的处理程序分配给不同类型的对象似乎不是一个好主意。我的建议是为每种类型的控件创建一个处理程序。

答案 1 :(得分:1)

在您的事件处理程序中,您有:

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

但发件人可以是PanelLabel,这就是为什么其中一个演员会失败的原因。

答案 2 :(得分:0)

你的问题就在这一行:

Label templab = (Label)sender;

当您点击该面板时,senderPanel,而不是Label,因此您无法将其投射。