发生了类型为“System.InvalidCastException”的未处理异常 在Cheff2000.exe中
附加信息:无法投射类型的对象 'System.Windows.Forms.Button'类型'System.Windows.Forms.PictureBox'。
我的空白是:
private void CapturaRecetasBtnFam() // SOLO Captura las primeras Recetas de la Fam cuando se hace Click en el BtnFam-n
{
BlanquearRec();
oListaFamRec = objFamRec.Listar();
var miFami = oListaFamRec.Find(DescFam => DescFam.Descripcion == NomFam);
// The cod captured the ongoing fam
string CodFam = oListaFamRec[miFami.Posicion - 1].Codigo;
oListaReceta = objRec.ListarPosicionEnFam();
var Result = from sigreceta in oListaReceta where sigreceta.Familia == CodFam select sigreceta;
// We assign text and images to objects (Button and PictureBox) of Recipes
ContRec1 = 1;
ContRec2 = 1;
foreach (Vi_Receta Rec in Result)
{
// Textos a los Button
nomBoton = "BtnRec" + Convert.ToString(ContRec1);
nomPicture = "PicRec" + Convert.ToString(ContRec2);
foreach (Control miObjeto in this.Controls)
{
if (miObjeto is Button && miObjeto.Name == nomBoton)
{
miObjeto.Text = Rec.Descripcion;
ContRec1++;
}
}
// PictureBox pictures
foreach (PictureBox miPicture in this.Controls)
{
if (miPicture.Name == nomPicture && miPicture.Name == nomPicture)
{
miPicture.Image = Rec.Foto == null ? null : ConvertByteArrayToImage(Rec.Foto);
}
}
}
}
答案 0 :(得分:0)
问题出在你的foreach
循环中。你现在的方式,它枚举所有控件,包括按钮,导致错误。它应该看起来像这样(未经测试):
foreach (Control cPicture in this.Controls)
{
if (cPicture is PictureBox)
{
PictureBox miPicture = (PictureBox)cPicture;
if (miPicture.Name == nomPicture && miPicture.Name == nomPicture)
{
miPicture.Image = Rec.Foto == null ? null : ConvertByteArrayToImage(Rec.Foto);
}
}
}