System.invalidcastexception:'指定的强制转换无效。'标签 [英] System.invalidcastexception: 'specified cast is not valid.' with tags
本文介绍了System.invalidcastexception:'指定的强制转换无效。'标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用此标记属性但由于强制转换无法正常工作
我将所有标记放到property = Borderstyle.None
我尝试了什么:
这在这里工作
I'm trying to make use of this tag property but it isn't working properly because of cast is not valid
I put all tags to property = Borderstyle.None
What I have tried:
This works here
private void PictureBoxes_MouseEnter(object sender, EventArgs e)
{
PictureBox p = sender as PictureBox;
if (p != null)
{
p.BorderStyle = BorderStyle.Fixed3D;
}
}
private void PictureBoxes_MouseClick(object sender, MouseEventArgs e)
{
PictureBox pBox = sender as PictureBox;
if (e.Button == MouseButtons.Left)
{
pBox.MouseLeave -= PictureBoxes_MouseLeave;
pBox.ImageLocation = (startupPath + @"img\sd0.png");
}
else if (e.Button == MouseButtons.Right)
{
pBox.MouseLeave -= PictureBoxes_MouseLeave; // prevent double subscription
pBox.MouseLeave += PictureBoxes_MouseLeave;
}
}
这不起作用
This doesn't work
private void PictureBoxes_MouseLeave(object sender, EventArgs e)
{
PictureBox p = sender as PictureBox;
if (p != null)
{
try
{
p.BorderStyle = (BorderStyle)p.Tag; // <--- here comes the error
}
catch (Exception)
{
}
}
}
推荐答案
在某个地方,你很可能没有设置Tag属性。
使用调试器在错误发生时准确找出Tag中的内容:在catch语句中放置一个断点并查看p.Tag - 如果它是一个有效的BorderStyle,那么它将是一个包含0,1的整数,或2.任何其他值不可转换:参考源 [ ^ ]
一旦你知道它是什么,你可以看看为什么它是什么。
Somewhere, you haven't set the Tag property, most likely.
Use the debugger to find out exactly what is in the Tag when the error occurs: put a breakpoint in the catch statement and look at p.Tag - if it's a valid BorderStyle, then it'll be an integer containing 0, 1, or 2. Any other value is not castable: Reference Source[^]
Once you know what it is, you can look at why it is what it is.
OriginalGriff是正确的:你没有设置标签; BorderStyle的演员表是有效的。所以,投票给他的答案,而不是这一个:)
我认为你不需要'枚举因为......但是,首先,几句话关于'枚举:
所有C#枚举都存储一些数字类型的内部值:枚举的批准类型是byte,sbyte,short,ushort,int, uint,long或ulong:[ ^ ]。
当您设置WinForm Control的'Tag属性时,您将设置为的内容将被转换为一个物体(即盒装)。如果将其设置为可空或引用,则可以使用As运算符检索该值,这很方便,因为如果转换不起作用,它将返回null。 '枚举值不是引用类型,不可为空:所以,你不能使用'As。
现在,关于你的代码:考虑:
OriginalGriff is correct: you have not set the Tag; the cast to 'BorderStyle is valid. So, vote his answer up, not this one :)
I don't think you need an 'enum here because ... but, first, a few words about 'enum:
All C# Enumerations store an internal value of some numeric type: "The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong:" [^].
When you set a WinForm Control's 'Tag property, what you set it to is cast to an object (i.e., boxed). If you set it to a nullable, or reference, Type, you can retrieve the value using the 'As operator, which is convenient because it will return 'null if the cast did not work. An 'enum value is not a reference Type, not nullable: so, you can't use 'As.
Now, about your code: consider:
PictureBox pb;
private void PictureBoxes_MouseEnter(object sender, System.EventArgs e)
{
pb = sender as PictureBox;
pb.BorderStyle = BorderStyle.Fixed3D;
}
private void PictureBoxes_MouseLeave(object sender, System.EventArgs e)
{
// you can be sure that 'pb is current here
if(pb.ImageLocation == null)
{
pb.BorderStyle = BorderStyle.None;
}
}
然后你的'Click事件处理程序简化为只在左键下载时设置'ImageLocation属性。
Then your 'Click event handler simplifies to only setting the 'ImageLocation property on left-mouse down.
这篇关于System.invalidcastexception:'指定的强制转换无效。'标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文