Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
                {

                    
                }
                
            }
        }
Posted
Updated 16-Feb-18 16:10pm

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.
 
Share this answer
 
Comments
ThisandThatorWhat 16-Feb-18 14:54pm    
Sorry could you explain where this integer value shows up from the borderstyle. I know how to use the breakpoint and step forward with F11. I can't seem to find it in the local or watch panel when I step to it.
ThisandThatorWhat 16-Feb-18 15:26pm    
I see the tag value is always set to a type of string
ThisandThatorWhat 16-Feb-18 16:51pm    
I tried manually changing the string to int and it works. It's like the tag property sets by default all to string ? How do I change that much appreciated Thanks !
OriginalGriff 17-Feb-18 6:49am    
No, Tag doesn't default to anything.
It contains an object - which means anything declared as object, or any class which is derived from the object class. And since **everything** in C# is ultimately derived from the object class, that means that the Tag property can hold a single instance of any class that exists, or that can be created.

So you can happily do any of these:
myControl.Tag = 0;
myControl.Tag = 64.9;
myControl.Tag = "hello";
myControl.Tag = myDataSet;
myControl.Tag = myButton;
myControl.Tag = this;
myControl.Tag = new {Name = "OriginalGriff", Date = DateTime.Now};

(But that last isn't too useful unless you use dynamic variables later)
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:
C#
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;
    }
}
Then your 'Click event handler simplifies to only setting the 'ImageLocation property on left-mouse down.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900