views:

44

answers:

3

Here is the source of a game I saw on the Internet. I want to copy it into my application, but it doesn't work. Any button is not made. How can I fix it?

namespace backgammon1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        Button []chipmove=new Button[24];
        Button choice;
        int[] counterplace = {749,689,629,569,509,449,339,279,219,159,99,39,
                                 40,100,160,220,280,340,450,510,570,630,690,750 };//30 540
        List<int> stonetotal = new List<int>(); 

        //int[]stonestotal= {1,1,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0};
        PictureBox[] darkstone = new PictureBox[3];
        Random dice = new Random();
        public int stoneloc,stotal,y;
        public int die1, die2,counterindex,die1used,die2used,whichstone,psender,mouseclick=0;
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Point[]points={new Point(30,30),new Point(60,200),new Point(90,30),
                           new Point(90,30),new Point(120,200),new Point(150,30),
                           new Point(180,200),new Point(210,30),new Point(240,200),
                           new Point(270,30),new Point(300,200),new Point(330,30),
                           new Point(360,200),new Point(390,30),new Point(440,30),
                           new Point(470,200),new Point(500,30),new Point(530,200),
                           new Point(560,30),new Point(590,200),new Point(620,30),
                           new Point(650,200),new Point(680,30),new Point(710,200),
                           new Point(740,30),new Point(770,200),new Point(800,30)

            };
            Point[] points2 ={new Point(30,576),new Point(60,376),new Point(90,576),
                           new Point(120,376),new Point(150,576),
                           new Point(180,376),new Point(210,576),new Point(240,376),
                           new Point(270,576),new Point(300,376),new Point(330,576),
                           new Point(360,376),new Point(390,576),new Point(440,576),
                           new Point(470,376),new Point(500,576),new Point(530,376),
                           new Point(560,576),new Point(590,376),new Point(620,576),
                           new Point(650,376),new Point(680,576),new Point(710,376),
                           new Point(740,576),new Point(770,376),new Point(800,576)

            };

            SolidBrush brush = new SolidBrush(Color.Gold);
            SolidBrush brush2 = new SolidBrush(Color.SaddleBrown);
            Pen blackPen = new Pen(Color.SaddleBrown, 40);
            e.Graphics.DrawRectangle(blackPen,10,10,810, 586);
            e.Graphics.FillRectangle(brush, 30, 30, 390, 546);
            e.Graphics.FillRectangle(brush, 400, 30, 400, 546);
            e.Graphics.DrawRectangle(blackPen, 410, 10, 10, 586);
            Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));
         //   e.Graphics.DrawLine(pen, 30, 30,60,200);
           // e.Graphics.DrawLine(pen, 90, 30, 60, 200);
            System.Drawing.Drawing2D.GraphicsPath path= new  System.Drawing.Drawing2D.GraphicsPath();
            path.AddLines(points);
            e.Graphics.DrawPath(pen, path);
            e.Graphics.FillPath(brush2, path);
            path.Reset(); path.AddLines(points2);
            e.Graphics.DrawPath(pen, path);
            e.Graphics.FillPath(brush2, path);

            brush.Dispose(); brush2.Dispose(); pen.Dispose();


        }
        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
           e.Effect = DragDropEffects.Move;

        }
        public void Form1_Load(object sender, EventArgs e)
        {

            int x = 750; int y = 0; int no = 1;
            for (int i = 0; i < 6; i++)
            {
                chipmove[i] = new Button();
                this.Controls.Add(chipmove[i]);

                chipmove[i].Location = new Point(x, y);
                chipmove[i].Size = new Size(30, 20);
                chipmove[i].BackColor = Color.Cornsilk;

                chipmove[i].Text = no.ToString();
                chipmove[i].Enabled = false;
                chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
                x -= 60; no++;
            }
            x = 340;
            for (int i = 6; i < 12; i++)
            {
                chipmove[i] = new Button();
                this.Controls.Add(chipmove[i]);

                chipmove[i].Location = new Point(x, y);
                chipmove[i].Size = new Size(30, 20);
                chipmove[i].BackColor = Color.Cornsilk;

                chipmove[i].Text = no.ToString();
                chipmove[i].Enabled = false;
                chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
                x -= 60; no++;
            }
            x=40;y=576;
            for (int i = 12; i < 18; i++)
            {
                chipmove[i] = new Button();
                this.Controls.Add(chipmove[i]);

                chipmove[i].Location = new Point(x, y);
                chipmove[i].Size = new Size(30, 20);
                chipmove[i].BackColor = Color.Cornsilk;

                chipmove[i].Text = no.ToString();
                chipmove[i].Enabled = false;
                chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
                x += 60; no++;
            }
            x = 455;
            for (int i = 18; i < 24; i++)
            {
                chipmove[i] = new Button();
                this.Controls.Add(chipmove[i]);

                chipmove[i].Location = new Point(x, y);
                chipmove[i].Size = new Size(30, 20);
                chipmove[i].BackColor = Color.Cornsilk;

                chipmove[i].Text = no.ToString();
                chipmove[i].Enabled = false;
                chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
                x += 60; no++;
            }

            x=749;y=30;
            for (int s = 0; s < 3; s++)
            {
                darkstone[s] = new PictureBox();
                this.Controls.Add(darkstone[s]);
                darkstone[s].Location = new Point(x,y);
                darkstone[s].BackColor = Color.Transparent;
                x -= 60;
                darkstone[s].Paint+=new PaintEventHandler(stone_Paint  );               
                darkstone[s].MouseDown += new System.Windows.Forms.MouseEventHandler(stone_MouseDown);

            }
            stonetotal.AddRange(new int[]{1,1,1,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0});
        }
        private void stone_Paint(object sender, PaintEventArgs e)
        {
         SolidBrush brush = new SolidBrush(Color.SandyBrown);
          e.Graphics.FillEllipse(brush, 0, 0, 35, 35);
          brush.Dispose();
        }



        private void stone_MouseDown(object sender, MouseEventArgs e)
        {
            mouseclick++;
            //stoneloc = location of clicked stone before move. whichstone = which stone has been clicked
            if (mouseclick == 1)
            {
                foreach (PictureBox p in darkstone)
                {
                    if (p == sender)
                    {
                        psender = Array.IndexOf(darkstone, sender);
                        stoneloc =Convert.ToInt32( darkstone[psender].Location.X.ToString()); //+ darkstone[psender].Location.Y.ToString());
                        counterindex = Array.IndexOf(counterplace, stoneloc);
                        whichstone = psender;
                        stotal = stonetotal[counterindex];
                        stonetotal.RemoveAt(counterindex); stonetotal.Insert(counterindex,stotal - 1);
                        label1.Text = stonetotal[counterindex].ToString();   
                    }
                }
                //if (die1used != 1 && die2used != 1)
               // {
                    if (die1 + counterindex < 24)
                    {
                        chipmove[die1 + counterindex].Enabled = true;
                        chipmove[die1 + counterindex].BackColor = Color.Yellow;
                    }  else{ mouseclick=0; }


                    if (die2 + counterindex < 24)
                    {
                        chipmove[die2 + counterindex].Enabled = true;
                        chipmove[die2 + counterindex].BackColor = Color.Yellow;
                    }else{mouseclick=0;  }


                if (die1used == 1 && die2used != 1 && die2 + counterindex < 24)
                {
                    chipmove[die2 + counterindex].Enabled = true;
                    chipmove[die2 + counterindex].BackColor = Color.Yellow;
                }

            }
            if(mouseclick==2)
            {
                for (int i = 0; i < 24; i++)
                {
                    chipmove[i].BackColor = Color.Cornsilk;
                    chipmove[i].Enabled = false;
                }
 foreach (PictureBox p in darkstone)
                {
                    if (p == sender)
                    {
                        psender = Array.IndexOf(darkstone, sender);
                        stoneloc = Convert.ToInt32(darkstone[psender].Location.X.ToString()); //+ darkstone[psender].Location.Y.ToString());
                        counterindex = Array.IndexOf(counterplace, stoneloc);
                        whichstone = psender;
                        stotal = stonetotal[counterindex];
                        stonetotal.RemoveAt(counterindex); stonetotal.Insert(counterindex, stotal - 1);
                        label1.Text = stonetotal[counterindex].ToString();   
                    }
                }

                if (die1used == 1 && die2used != 1 && die2 + counterindex < 24)
                {

                    chipmove[die2 + counterindex].Enabled = true;
                    chipmove[die2 + counterindex].BackColor = Color.Yellow;
                }
                if (die1used == 1 && die2used != 1 && die2 + counterindex > 24)
                {
                    mouseclick = 1;

                }
                if (die1used != 1 && die2used == 1 && die1 + counterindex < 24)
                {
                    chipmove[die1 + counterindex].Enabled = true;
                    chipmove[die1 + counterindex].BackColor = Color.Yellow;
                }
                if (die1used != 1 && die2used == 1 && die1 + counterindex > 24)
                {
                    mouseclick = 1;
                }
            }
        }



        private void label1_Click(object sender, EventArgs e)
        {
          //using a label for dice for test purposes.when working ok will use graphics for dice
            die1 = dice.Next(6)+1;
            die2 = dice.Next(6)+1;
            //for test purposes make dice 1 and 2
            die1 = 2; die2 =1;
            label1.Text = die1.ToString() + ":" + die2.ToString();
            label1.Enabled = false; darkstone[0].Enabled = true;
            darkstone[1].Enabled = true;
        }

        private void btnfinishmove_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 24; i++)
            {
                chipmove[i].BackColor = Color.Cornsilk;
                chipmove[i].Enabled = false;
            }
            darkstone[0].Enabled = false;
            darkstone[1].Enabled = false;
            die1used = 0; die2used = 0; mouseclick = 0;
            label1.Enabled = true; 
        }


        private void chip_MouseClick(object sender, MouseEventArgs e)
        {

            choice = (Button)sender;

            int chipsender;
            chipsender = Array.IndexOf(chipmove, sender);    //.ToString();

            if( die1+counterindex<24)
            {
                if (choice.Equals(chipmove[die1 + counterindex]))
                {
                    for (int i = 0; i < 24; i++)
                    {
                        chipmove[i].BackColor = Color.Cornsilk;
                        chipmove[i].Enabled = false;
                    }
                    die1used = 1;

                }
            }
            if(die2+counterindex<24)
            {
                if (choice.Equals(chipmove[die2 + counterindex]))
                {
                    for (int i = 0; i < 24; i++)
                    {
                        chipmove[i].BackColor = Color.Cornsilk;
                        chipmove[i].Enabled = false;
                    }
                    die2used = 1;

                }
            }
            #region chipsender
            switch (chipsender)
            {
                case 0:
                    darkstone[whichstone].Location=new Point(749, 30);                   
                    counterindex = 0;
                    break;
                case 1:                                
                    counterindex = 1;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(689,y);

                    break;
                case 2:
                    counterindex = 2;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(629,y);                  

                    break;
                case 3:
                    counterindex = 3 ;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(569,y);                

                    break;
                case 4:
                    counterindex = 4;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(509,y);                

                    break;
                case 5:
                    counterindex = 5;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(449,y);                 

                    break;
                case 6:
                   counterindex = 6;
                   nodarkstones();
                    darkstone[whichstone].Location = new Point(339,y);                

                    break;
                case 7:
                    counterindex = 7;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(279,y); 

                    break;
                case 8:
                    counterindex = 8;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(219,y);                  

                    break;
                case 9:
                   counterindex = 9;
                   nodarkstones();
                    darkstone[whichstone].Location = new Point(159,y);                

                    break;
                case 10:
                    darkstone[whichstone].Location = new Point(99, 30);                 
                    counterindex = 10;
                    break;
                case 11:
                    darkstone[whichstone].Location = new Point(39, 30);                  
                    counterindex = 11;
                    break;
                case 12:
                    darkstone[whichstone].Location = new Point(40,540);                   
                    counterindex = 12;
                    break;
                case 13:
                    darkstone[whichstone].Location = new Point(100,540);                    
                    counterindex = 13;
                    break;
                case 14:
                    darkstone[whichstone].Location = new Point(160,540);                  
                    counterindex = 14;
                    break;
                case 15:
                    darkstone[whichstone].Location = new Point(220,540);                  
                    counterindex = 15;
                    break;
                case 16:
                    darkstone[whichstone].Location = new Point(280,540);                 
                    counterindex = 16;
                    break;
                case 17:
                    darkstone[whichstone].Location = new Point(340,540);                  
                    counterindex = 17;
                    break;
                case 18:
                    darkstone[whichstone].Location = new Point(450, 540);                   
                    counterindex = 18;
                    break;
                case 19:
                    darkstone[whichstone].Location = new Point(510, 540);                   
                    counterindex = 19;
                    break;
                case 20:
                    darkstone[whichstone].Location = new Point(570, 540);                   
                    counterindex = 20;
                    break;
                case 21:
                    darkstone[whichstone].Location = new Point(630, 540);                  
                    counterindex = 21;
                    break;
                case 22:
                    darkstone[whichstone].Location = new Point(690, 540);                    
                    counterindex = 22;
                    break;
                case 23:
                    darkstone[whichstone].Location = new Point(750, 540);                   
                    counterindex = 23;
                    break;

                 default:
                    break;
            }
            #endregion


                if (die1used == 1 && die2used != 1 && die2 + counterindex < 24)
                {
                    chipmove[die2 + counterindex].Enabled = true;
                    chipmove[die2 + counterindex].BackColor = Color.Yellow;
                    //die2used = 1;
                }
                if (die1used != 1 && die2used == 1 && die1 + counterindex < 24)
                {
                    chipmove[die1 + counterindex].Enabled = true;
                    chipmove[die1 + counterindex].BackColor = Color.Yellow;
                    //die1used = 1;
                }

        foreach (PictureBox p in darkstone)
            {
                if (p == sender)
                {
                    psender = Array.IndexOf(darkstone, sender);
                    stoneloc =Convert.ToInt32( darkstone[psender].Location.X.ToString() + darkstone[psender].Location.Y.ToString());
                    counterindex = Array.IndexOf(counterplace, stoneloc);
                    whichstone = psender;

                }
         }
                for (int i = 0; i < 24; i++)
                {
                    chipmove[i].BackColor = Color.Cornsilk;
                    chipmove[i].Enabled = false;
                }     



}//chipmouseclick

        private void nodarkstones()
        {
            stotal = stonetotal[counterindex]; stotal += 1;
            stonetotal.RemoveAt(counterindex); stonetotal.Insert(counterindex, stotal);
            label1.Text = stonetotal[counterindex].ToString();
            if (stotal == 1)
            {
                y = 30;
            }
            if (stotal ==2)
            {
                y = 65;
            }
            if (stotal == 3)
            {
                y = 100;
            }
        }

    }
}
A: 

You will need the form. This is just the code behind the form. The form itself is stored in the formname.designer.cs file.

Sjuul Janssen
A: 

I am going to assume that you are not seeing anything created. If this is the case then check to make sure that the form is wired up to use the load and paint event handlers. I had to go in and manually select the form and specify the paint method to use.

spinon
A: 

Create a new WinForms project. Add a label to the form. Then open Form1.cs and delete everything except the using statements on the top.

Then take the code from the website, and paste it into Form1.cs. Finally, change the namespace in Form1.cs from backgammon1 to WindowsFormsApplication1.

If you then run it you'll see some controls on the screen, including some circles that you can click on.

ho1