Click here to Skip to main content
15,887,453 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Read from text file Pin
Pavlex423-Dec-16 21:40
Pavlex423-Dec-16 21:40 
AnswerRe: C# Read from text file Pin
Midi_Mick23-Dec-16 21:41
professionalMidi_Mick23-Dec-16 21:41 
GeneralRe: C# Read from text file Pin
Pavlex423-Dec-16 21:53
Pavlex423-Dec-16 21:53 
GeneralRe: C# Read from text file Pin
Midi_Mick23-Dec-16 22:09
professionalMidi_Mick23-Dec-16 22:09 
GeneralRe: C# Read from text file Pin
OriginalGriff23-Dec-16 22:56
mveOriginalGriff23-Dec-16 22:56 
GeneralRe: C# Read from text file Pin
Dave Kreskowiak24-Dec-16 4:38
mveDave Kreskowiak24-Dec-16 4:38 
AnswerRe: C# Read from text file Pin
Gerry Schmitz26-Dec-16 5:42
mveGerry Schmitz26-Dec-16 5:42 
QuestionC# Specified cast is not valid Pin
Pavlex422-Dec-16 20:48
Pavlex422-Dec-16 20:48 
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
C#
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:

C#
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 pictureBox58;

        private GroupBox groupBox9;

        private Label label1;

        private MenuStrip menuStrip1;

        private ToolStripMenuItem igraToolStripMenuItem;

        private GroupBox groupBox2;

        private PictureBox pictureBox47;

        private PictureBox pictureBox48;

        private PictureBox pictureBox49;

        private PictureBox pictureBox50;

        private PictureBox pictureBox51;

        private PictureBox pictureBox52;

        private PictureBox pictureBox53;

        private PictureBox pictureBox54;

        private PictureBox pictureBox39;

        private PictureBox pictureBox40;

        private PictureBox pictureBox41;

        private PictureBox pictureBox42;

        private PictureBox pictureBox43;

        private PictureBox pictureBox44;

        private PictureBox pictureBox45;

        private PictureBox pictureBox46;

        private PictureBox pictureBox31;

        private PictureBox pictureBox32;

        private PictureBox pictureBox33;

        private PictureBox pictureBox34;

        private PictureBox pictureBox35;

        private PictureBox pictureBox36;

        private PictureBox pictureBox37;

        private PictureBox pictureBox38;

        private PictureBox pictureBox23;

        private PictureBox pictureBox24;

        private PictureBox pictureBox25;

        private PictureBox pictureBox26;

        private PictureBox pictureBox27;

        private PictureBox pictureBox28;

        private PictureBox pictureBox29;

        private PictureBox pictureBox30;

        private PictureBox pictureBox15;

        private PictureBox pictureBox16;

        private PictureBox pictureBox17;

        private PictureBox pictureBox18;

        private PictureBox pictureBox19;

        private PictureBox pictureBox20;

        private PictureBox pictureBox21;

        private PictureBox pictureBox22;

        private PictureBox pictureBox11;

        private PictureBox pictureBox12;

        private PictureBox pictureBox13;

        private PictureBox pictureBox14;

        private PictureBox pictureBox10;

        private PictureBox pictureBox9;

        private PictureBox pictureBox8;

        private PictureBox pictureBox7;

        private ToolStripMenuItem prikaziTabeluToolStripMenuItem;

        private ToolStripMenuItem infoToolStripMenuItem;

        private ToolStripMenuItem vratiPotezToolStripMenuItem;

        private int brojKlikova = 0;

        private int brojPoena = 0;

        private bool pocetakIgre = true;

        private bool igraZavrsena = false;

        private int[] pokusaj;

        private SkockoClass skocko;

        public PictureBox[] trenutneKontrole;

        public PictureBox[] rezultatPokusajaKontrole;
        private Button button1;
        private Button button2;
        private Button button3;
        private Button button4;
        private Button button5;
        private Button button6;
        public int rbrPokusaja;

        public Skocko()
        {
            InitializeComponent();

            int[] array = new int[4];
            this.pokusaj = array;
            this.skocko= null;
            this.trenutneKontrole = null;
            this.rezultatPokusajaKontrole = null;
            this.rbrPokusaja = 1;
            this.InitializeComponent();
            this.pictureBox1.Tag = 0;
            this.pictureBox2.Tag = 1;
            this.pictureBox3.Tag = 2;
            this.pictureBox4.Tag = 3;
            this.pictureBox5.Tag = 4;
            this.pictureBox6.Tag = 5;

            this.button1.Tag = 0;
            this.button2.Tag = 1;
            this.button3.Tag = 2;
            this.button4.Tag = 3;
            this.button5.Tag = 4;
            this.button6.Tag = 5;
        }
        private void SkloniSlike()
        {
            for (int i = 7; i < 59; i++)
            {
                Control[] array = base.Controls.Find("pictureBox" + i.ToString(), true);
                PictureBox pictureBox = array[0] as PictureBox;
                pictureBox.Image = null;
                Application.DoEvents();
                Thread.Sleep(3);
            }
        }

        private void PrikaziBodove()
        {
            this.label1.Text = this.brojPoena.ToString();
        }

        private void PrikaziRezultatPokusaja(PictureBox[] resPicBoxs, Bitmap[] resPictures)
        {
            for (int i = 0; i < resPicBoxs.Length; i++)
            {
                resPicBoxs[i].Image = resPictures[i];
            }
        }

        private void igraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!this.pocetakIgre && !this.igraZavrsena)
            {
                if (MessageBox.Show("Da li ste sigurni da zelite da pocnete novu partuju?\nBodovi ce biti smanjeni za 10 poena", "Info", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
                this.brojPoena -= 10;
                this.label1.Text = this.brojPoena.ToString();
            }
            this.skocko= new SkockoClass();
            this.brojKlikova = 0;
            this.rbrPokusaja = 1;
            if (!this.pocetakIgre)
            {
                this.SkloniSlike();
            }
            else
            {
                this.pocetakIgre = false;
            }
            this.igraZavrsena = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void prikaziTabeluToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void infoToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.Z)
            {
                if (this.brojKlikova > 0 && this.brojKlikova < 4)
                {
                    this.brojKlikova--;
                    this.trenutneKontrole[this.brojKlikova].Image = null;
                }
            }
        }

        private void vratiPotezToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.brojKlikova > 0 && this.brojKlikova < 4)
            {
                this.brojKlikova--;
                this.trenutneKontrole[this.brojKlikova].Image = null;
            }
        }

        private void Command_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            PictureBox pictureBox = sender as PictureBox;
            this.brojKlikova++;
            switch (this.rbrPokusaja)
            {
                case 1:
                    this.trenutneKontrole = new PictureBox[]
                    {
                    this.pictureBox7,
                    this.pictureBox8,
                    this.pictureBox9,
                    this.pictureBox10
                };
                    this.rezultatPokusajaKontrole = new PictureBox[]
                    {
                    this.pictureBox11,
                    this.pictureBox12,
                    this.pictureBox13,
                    this.pictureBox14
                };
                    break;
                case 2:
                    this.trenutneKontrole = new PictureBox[]
                    {
                    this.pictureBox15,
                    this.pictureBox16,
                    this.pictureBox17,
                    this.pictureBox18
                    };
                    this.rezultatPokusajaKontrole = new PictureBox[]
                    {
                    this.pictureBox19,
                    this.pictureBox20,
                    this.pictureBox21,
                    this.pictureBox22
                    };
                    break;
                case 3:
                    this.trenutneKontrole = new PictureBox[]
                    {
                    this.pictureBox23,
                    this.pictureBox24,
                    this.pictureBox25,
                    this.pictureBox26
                    };
                    this.rezultatPokusajaKontrole = new PictureBox[]
                    {
                    this.pictureBox27,
                    this.pictureBox28,
                    this.pictureBox29,
                    this.pictureBox30
                    };
                    break;
                case 4:
                    this.trenutneKontrole = new PictureBox[]
                    {
                    this.pictureBox31,
                    this.pictureBox32,
                    this.pictureBox33,
                    this.pictureBox34
                    };
                    this.rezultatPokusajaKontrole = new PictureBox[]
                    {
                    this.pictureBox35,
                    this.pictureBox36,
                    this.pictureBox37,
                    this.pictureBox38
                    };
                    break;
                case 5:
                    this.trenutneKontrole = new PictureBox[]
                    {
                    this.pictureBox39,
                    this.pictureBox40,
                    this.pictureBox41,
                    this.pictureBox42
                    };
                    this.rezultatPokusajaKontrole = new PictureBox[]
                    {
                    this.pictureBox43,
                    this.pictureBox44,
                    this.pictureBox45,
                    this.pictureBox46
                    };
                    break;
                case 6:
                    this.trenutneKontrole = new PictureBox[]
                    {
                    this.pictureBox47,
                    this.pictureBox48,
                    this.pictureBox49,
                    this.pictureBox50
                    };
                    this.rezultatPokusajaKontrole = new PictureBox[]
                    {
                    this.pictureBox51,
                    this.pictureBox52,
                    this.pictureBox53,
                    this.pictureBox54
                    };
                    break;
            }
            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;
            }
            if (this.brojKlikova == 4)
            {
                this.brojKlikova = 0;
                this.PrikaziRezultatPokusaja(this.rezultatPokusajaKontrole, this.skocko.ProveriPokusaj(this.pokusaj));
                if (this.skocko.reseno)
                {
                    switch (this.rbrPokusaja)
                    {
                        case 1:
                            this.brojPoena += 100;
                            break;
                        case 2:
                            this.brojPoena += 90;
                            break;
                        case 3:
                            this.brojPoena += 80;
                            break;
                        case 4:
                            this.brojPoena += 70;
                            break;
                        case 5:
                            this.brojPoena += 60;
                            break;
                        case 6:
                            this.brojPoena += 50;
                            break;
                    }
                    this.rbrPokusaja = 1;
                    this.igraZavrsena = true;
                    this.PrikaziBodove();
                    this.PrikaziResenje();
                }
                else if (this.rbrPokusaja == 6)
                {
                    this.igraZavrsena = true;
                    this.rbrPokusaja = 1;
                    this.brojPoena -= 10;
                    this.PrikaziBodove();
                    this.PrikaziResenje();
                }
                else
                {
                    this.rbrPokusaja++;
                }
            }
        }

        private void PrikaziResenje()
        {
            this.PrikaziRezultatPokusaja(new PictureBox[]
            {
                this.pictureBox55,
                this.pictureBox56,
                this.pictureBox57,
                this.pictureBox58
            }, this.skocko.DajKombinacijuKaoNizSlika());
        }

        private void pictureBox47_Click(object sender, EventArgs e)
        {
            if (this.brojKlikova > 0 && this.brojKlikova < 4)
            {
                this.brojKlikova--;
                this.trenutneKontrole[this.brojKlikova].Image = null;
            }
        }
    }
}


Other class with code:

C#
using Skocko5.Properties;
using System;
using System.Drawing;

namespace Skocko5
{
    public class Igra
    {
        private int[] kombinacija;

        public bool reseno;

        public Igra()
        {
            int[] array = new int[4];
            this.kombinacija = array;
            this.reseno = false;
            Random random = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < 4; i++)
            {
                this.kombinacija[i] = random.Next(0, 6);
            }
        }

        public int[] DajKombinacijuKaoNizInt()
        {
            return this.kombinacija;
        }

        public Bitmap[] DajKombinacijuKaoNizSlika()
        {
            Bitmap[] array = new Bitmap[4];
            for (int i = 0; i < this.kombinacija.Length; i++)
            {
                switch (this.kombinacija[i])
                {
                    case 0:
                        array[i] = new Bitmap(Resources.herc);
                        break;
                    case 1:
                        array[i] = new Bitmap(Resources.pik);
                        break;
                    case 2:
                        array[i] = new Bitmap(Resources.karo);
                        break;
                    case 3:
                        array[i] = new Bitmap(Resources.tref);
                        break;
                    case 4:
                        array[i] = new Bitmap(Resources.skocko);
                        break;
                    case 5:
                        array[i] = new Bitmap(Resources.zvezda);
                        break;
                }
            }
            return array;
        }

        public Bitmap[] ProveriPokusaj(int[] pokusaj)
        {
            int num = 0;
            int num2 = 0;
            int[] array = (int[])this.kombinacija.Clone();
            for (int i = 0; i < pokusaj.Length; i++)
            {
                for (int j = 0; j < array.Length; j++)
                {
                    if (pokusaj[i] == array[j])
                    {
                        num++;
                        array[j] = -1;
                        break;
                    }
                }
            }
            for (int i = 0; i < this.kombinacija.Length; i++)
            {
                if (pokusaj[i] == this.kombinacija[i])
                {
                    num2++;
                }
            }
            int num3 = num - num2;
            Bitmap[] array2 = new Bitmap[4];
            for (int i = 0; i < num2; i++)
            {
                array2[i] = new Bitmap(Resources.pogodakPravoMesto);
            }
            for (int i = num2; i < num2 + num3; i++)
            {
                array2[i] = new Bitmap(Resources.pogodakPogresnoMesto);
            }
            for (int i = num2 + num3; i < this.kombinacija.Length; i++)
            {
                array2[i] = new Bitmap(Resources.promasaj);
            }
            if (num2 == 4)
            {
                this.reseno = true;
            }
            return array2;
        }

        private void PostaviPrvoPojavljivanjeBrojaNaMinusJedan(int broj, ref int[] niz)
        {
            for (int i = 0; i < niz.Length; i++)
            {
                if (niz[i] == broj)
                {
                    niz[i] = -1;
                    break;
                }
            }
        }
    }
}

AnswerRe: C# Specified cast is not valid Pin
Richard MacCutchan22-Dec-16 21:23
mveRichard MacCutchan22-Dec-16 21:23 
GeneralRe: C# Specified cast is not valid Pin
Pavlex422-Dec-16 21:33
Pavlex422-Dec-16 21:33 
GeneralRe: C# Specified cast is not valid Pin
Richard MacCutchan22-Dec-16 22:09
mveRichard MacCutchan22-Dec-16 22:09 
GeneralRe: C# Specified cast is not valid Pin
harold aptroot22-Dec-16 22:26
harold aptroot22-Dec-16 22:26 
GeneralRe: C# Specified cast is not valid Pin
Richard MacCutchan22-Dec-16 22:33
mveRichard MacCutchan22-Dec-16 22:33 
GeneralRe: C# Specified cast is not valid Pin
harold aptroot22-Dec-16 22:39
harold aptroot22-Dec-16 22:39 
GeneralRe: C# Specified cast is not valid Pin
Richard MacCutchan22-Dec-16 22:54
mveRichard MacCutchan22-Dec-16 22:54 
GeneralRe: C# Specified cast is not valid Pin
OriginalGriff22-Dec-16 22:12
mveOriginalGriff22-Dec-16 22:12 
AnswerRe: C# Specified cast is not valid Pin
OriginalGriff22-Dec-16 22:11
mveOriginalGriff22-Dec-16 22:11 
AnswerRe: C# Specified cast is not valid Pin
Nelson Costa Inácio22-Dec-16 22:42
Nelson Costa Inácio22-Dec-16 22:42 
GeneralRe: C# Specified cast is not valid Pin
harold aptroot22-Dec-16 22:45
harold aptroot22-Dec-16 22:45 
GeneralRe: C# Specified cast is not valid Pin
OriginalGriff23-Dec-16 0:05
mveOriginalGriff23-Dec-16 0:05 
AnswerRe: C# Specified cast is not valid Pin
Pete O'Hanlon23-Dec-16 0:00
mvePete O'Hanlon23-Dec-16 0:00 
AnswerRe: C# Specified cast is not valid Pin
Gerry Schmitz23-Dec-16 6:50
mveGerry Schmitz23-Dec-16 6:50 
GeneralRe: C# Specified cast is not valid Pin
Richard MacCutchan23-Dec-16 6:59
mveRichard MacCutchan23-Dec-16 6:59 
QuestionWhat is this JWT Code Doing? Pin
TheOnlyRealTodd21-Dec-16 15:46
professionalTheOnlyRealTodd21-Dec-16 15:46 
GeneralRe: What is this JWT Code Doing? Pin
Richard MacCutchan21-Dec-16 21:54
mveRichard MacCutchan21-Dec-16 21:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.