C# Specified cast is not valid
-
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
switch (this.brojKlikova)
{
case 1:
{
//this.trenutneKontrole[0].Image = pictureBox.Image;
//this.pokusaj[0] = (int)pictureBox.Tag;this.trenutneKontrole\[0\].Image = button.Image; this.pokusaj\[0\] = (int)button.Tag; } break; case 2: { //this.trenutneKontrole\[1\].Image = pictureBox.Image; //this.pokusaj\[1\] = (int)pictureBox.Tag; this.trenutneKontrole\[1\].Image = button.Image; this.pokusaj\[1\] = (int)button.Tag; } break; case 3: { //this.trenutneKontrole\[2\].Image = pictureBox.Image; //this.pokusaj\[2\] = (int)pictureBox.Tag; this.trenutneKontrole\[2\].Image = button.Image; this.pokusaj\[2\] = (int)button.Tag; } break; case 4: { //this.trenutneKontrole\[3\].Image = pictureBox.Image; //this.pokusaj\[3\] = (int)pictureBox.Tag; this.trenutneKontrole\[3\].Image = button.Image; this.pokusaj\[3\] = (int)button.Tag; } break;
This is full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;using System.Threading;
namespace Slagalica
{
public partial class Skocko : Form
{
private GroupBox groupBox1;private PictureBox pictureBox6; private PictureBox pictureBox5; private PictureBox pictureBox4; private PictureBox pictureBox3; private PictureBox pictureBox2; private PictureBox pictureBox1; private GroupBox groupBox8; private PictureBox pictureBox55; private PictureBox pictureBox56; private PictureBox pictureBox57; private PictureBox pictureBo
-
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
switch (this.brojKlikova)
{
case 1:
{
//this.trenutneKontrole[0].Image = pictureBox.Image;
//this.pokusaj[0] = (int)pictureBox.Tag;this.trenutneKontrole\[0\].Image = button.Image; this.pokusaj\[0\] = (int)button.Tag; } break; case 2: { //this.trenutneKontrole\[1\].Image = pictureBox.Image; //this.pokusaj\[1\] = (int)pictureBox.Tag; this.trenutneKontrole\[1\].Image = button.Image; this.pokusaj\[1\] = (int)button.Tag; } break; case 3: { //this.trenutneKontrole\[2\].Image = pictureBox.Image; //this.pokusaj\[2\] = (int)pictureBox.Tag; this.trenutneKontrole\[2\].Image = button.Image; this.pokusaj\[2\] = (int)button.Tag; } break; case 4: { //this.trenutneKontrole\[3\].Image = pictureBox.Image; //this.pokusaj\[3\] = (int)pictureBox.Tag; this.trenutneKontrole\[3\].Image = button.Image; this.pokusaj\[3\] = (int)button.Tag; } break;
This is full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;using System.Threading;
namespace Slagalica
{
public partial class Skocko : Form
{
private GroupBox groupBox1;private PictureBox pictureBox6; private PictureBox pictureBox5; private PictureBox pictureBox4; private PictureBox pictureBox3; private PictureBox pictureBox2; private PictureBox pictureBox1; private GroupBox groupBox8; private PictureBox pictureBox55; private PictureBox pictureBox56; private PictureBox pictureBox57; private PictureBox pictureBo
-
See Control.Tag Property (System.Windows.Forms)[^]. You need to make more use of the documentation before you post questions here.
-
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
switch (this.brojKlikova)
{
case 1:
{
//this.trenutneKontrole[0].Image = pictureBox.Image;
//this.pokusaj[0] = (int)pictureBox.Tag;this.trenutneKontrole\[0\].Image = button.Image; this.pokusaj\[0\] = (int)button.Tag; } break; case 2: { //this.trenutneKontrole\[1\].Image = pictureBox.Image; //this.pokusaj\[1\] = (int)pictureBox.Tag; this.trenutneKontrole\[1\].Image = button.Image; this.pokusaj\[1\] = (int)button.Tag; } break; case 3: { //this.trenutneKontrole\[2\].Image = pictureBox.Image; //this.pokusaj\[2\] = (int)pictureBox.Tag; this.trenutneKontrole\[2\].Image = button.Image; this.pokusaj\[2\] = (int)button.Tag; } break; case 4: { //this.trenutneKontrole\[3\].Image = pictureBox.Image; //this.pokusaj\[3\] = (int)pictureBox.Tag; this.trenutneKontrole\[3\].Image = button.Image; this.pokusaj\[3\] = (int)button.Tag; } break;
This is full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;using System.Threading;
namespace Slagalica
{
public partial class Skocko : Form
{
private GroupBox groupBox1;private PictureBox pictureBox6; private PictureBox pictureBox5; private PictureBox pictureBox4; private PictureBox pictureBox3; private PictureBox pictureBox2; private PictureBox pictureBox1; private GroupBox groupBox8; private PictureBox pictureBox55; private PictureBox pictureBox56; private PictureBox pictureBox57; private PictureBox pictureBo
Please, start looking at your code, using the debugger, and thinking: your questions are showing increasing evidence that you are not doing any of these things. If the error is in this line:
this.pokusaj[0] = (int)button.Tag;
Then it obvious that whatever is stored in the Tag property of the button control is not an integer. What is it? We can't tell. Why isn't it an integer? Again, we can't tell - because we can't run your code and see what is going on. So for the unpteenth time: USE THE DEBUGGER and look at what is happening. It's the only way you are going to find out. Personally? I'd guess the Tag is null - which can't be cast to any value type.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
I'm beginning to suspect that nothing can actually help you... :sigh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
You cannot cast an
Object
to anint
; it makes no sense. You really need to understand the basics of C#, before trying a project such as this. -
You can if it's a boxed int though, and some ints were assigned to tags so it looks like that was the intention
-
The
Tag
property returns anObject
, so it can (as you imply) be cast to anIntxx
type, but not to a simpleint
. -
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
switch (this.brojKlikova)
{
case 1:
{
//this.trenutneKontrole[0].Image = pictureBox.Image;
//this.pokusaj[0] = (int)pictureBox.Tag;this.trenutneKontrole\[0\].Image = button.Image; this.pokusaj\[0\] = (int)button.Tag; } break; case 2: { //this.trenutneKontrole\[1\].Image = pictureBox.Image; //this.pokusaj\[1\] = (int)pictureBox.Tag; this.trenutneKontrole\[1\].Image = button.Image; this.pokusaj\[1\] = (int)button.Tag; } break; case 3: { //this.trenutneKontrole\[2\].Image = pictureBox.Image; //this.pokusaj\[2\] = (int)pictureBox.Tag; this.trenutneKontrole\[2\].Image = button.Image; this.pokusaj\[2\] = (int)button.Tag; } break; case 4: { //this.trenutneKontrole\[3\].Image = pictureBox.Image; //this.pokusaj\[3\] = (int)pictureBox.Tag; this.trenutneKontrole\[3\].Image = button.Image; this.pokusaj\[3\] = (int)button.Tag; } break;
This is full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;using System.Threading;
namespace Slagalica
{
public partial class Skocko : Form
{
private GroupBox groupBox1;private PictureBox pictureBox6; private PictureBox pictureBox5; private PictureBox pictureBox4; private PictureBox pictureBox3; private PictureBox pictureBox2; private PictureBox pictureBox1; private GroupBox groupBox8; private PictureBox pictureBox55; private PictureBox pictureBox56; private PictureBox pictureBox57; private PictureBox pictureBo
You cannot convert an object to int. Do some more research before asking for help
-
You cannot convert an object to int. Do some more research before asking for help
-
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
switch (this.brojKlikova)
{
case 1:
{
//this.trenutneKontrole[0].Image = pictureBox.Image;
//this.pokusaj[0] = (int)pictureBox.Tag;this.trenutneKontrole\[0\].Image = button.Image; this.pokusaj\[0\] = (int)button.Tag; } break; case 2: { //this.trenutneKontrole\[1\].Image = pictureBox.Image; //this.pokusaj\[1\] = (int)pictureBox.Tag; this.trenutneKontrole\[1\].Image = button.Image; this.pokusaj\[1\] = (int)button.Tag; } break; case 3: { //this.trenutneKontrole\[2\].Image = pictureBox.Image; //this.pokusaj\[2\] = (int)pictureBox.Tag; this.trenutneKontrole\[2\].Image = button.Image; this.pokusaj\[2\] = (int)button.Tag; } break; case 4: { //this.trenutneKontrole\[3\].Image = pictureBox.Image; //this.pokusaj\[3\] = (int)pictureBox.Tag; this.trenutneKontrole\[3\].Image = button.Image; this.pokusaj\[3\] = (int)button.Tag; } break;
This is full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;using System.Threading;
namespace Slagalica
{
public partial class Skocko : Form
{
private GroupBox groupBox1;private PictureBox pictureBox6; private PictureBox pictureBox5; private PictureBox pictureBox4; private PictureBox pictureBox3; private PictureBox pictureBox2; private PictureBox pictureBox1; private GroupBox groupBox8; private PictureBox pictureBox55; private PictureBox pictureBox56; private PictureBox pictureBox57; private PictureBox pictureBo
There are a couple of things I notice about your code. The first thing I see is that you use a switch statement unnecessarily. As each array item you drop into is 1 away from the value in brojKlikova, you can use a simple subtraction to allocate the correct value. The second issue you have is that you have no idea what's in Tag. It may, or may not, be an int. If I were you, I would use something that is suited to test and convert a value into an int. Specifically, I would use int.TryParse to see if I could actually convert from whatever is in Tag to an int.
int tagValue;
this.trenutneKontrole[brojKlikova-1] = button.Image;
if (!int.TryParse(button.Tag, out tagValue))
{
pokusaj[brojKlikova-1] = tagValue;
}
else
{
System.Diagnostics.Debug.WriteLine("The value in the Tag for {0} is not an integer", brojKlikova);
}Run the application and watch the Output window to see which one of your Tag values triggers this. It's that simple.
This space for rent
-
You cannot convert an object to int. Do some more research before asking for help
Depends on what the
object
contains. If you have a method:private void MyMethod(object o)
{
int i = (int) o;
...
}And you use it thus:
int x = 666;
MyMethod(x);It will work fine. If you do this:
MyMethod(null);
Or
MyMethod(new Button());
Then it will fail.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
switch (this.brojKlikova)
{
case 1:
{
//this.trenutneKontrole[0].Image = pictureBox.Image;
//this.pokusaj[0] = (int)pictureBox.Tag;this.trenutneKontrole\[0\].Image = button.Image; this.pokusaj\[0\] = (int)button.Tag; } break; case 2: { //this.trenutneKontrole\[1\].Image = pictureBox.Image; //this.pokusaj\[1\] = (int)pictureBox.Tag; this.trenutneKontrole\[1\].Image = button.Image; this.pokusaj\[1\] = (int)button.Tag; } break; case 3: { //this.trenutneKontrole\[2\].Image = pictureBox.Image; //this.pokusaj\[2\] = (int)pictureBox.Tag; this.trenutneKontrole\[2\].Image = button.Image; this.pokusaj\[2\] = (int)button.Tag; } break; case 4: { //this.trenutneKontrole\[3\].Image = pictureBox.Image; //this.pokusaj\[3\] = (int)pictureBox.Tag; this.trenutneKontrole\[3\].Image = button.Image; this.pokusaj\[3\] = (int)button.Tag; } break;
This is full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;using System.Threading;
namespace Slagalica
{
public partial class Skocko : Form
{
private GroupBox groupBox1;private PictureBox pictureBox6; private PictureBox pictureBox5; private PictureBox pictureBox4; private PictureBox pictureBox3; private PictureBox pictureBox2; private PictureBox pictureBox1; private GroupBox groupBox8; private PictureBox pictureBox55; private PictureBox pictureBox56; private PictureBox pictureBox57; private PictureBox pictureBo