2015年10月30日 星期五

C# 4*4 按按鈕出現數字不重複

em;
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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
     
        Button[,] Buttons = new System.Windows.Forms.Button[5, 5];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 5; i++)
            {
                for (int j = 1; j < 5; j++)
                {
                    Buttons[i, j] = new Button();

                    Buttons[i, j].Size = new Size(50, 50);
                    Buttons[i, j].Location = new Point(i * 50, j * 50);
                    this.Controls.Add(Buttons[i, j]);//出現在畫面中
                }
            }
        }

     
        private void button1_Click(object sender, EventArgs e)
        {
            int pro=0;
         
            for ( int i = 1; i < 5; i++)
            {
             
                for (int j = 1; j < 5; j++)
                {
                    pro = pro+1;
                    Buttons[i, j].Text = pro.ToString();
                 
                }
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }
    }
}

2015年10月23日 星期五

c# 9宮格亂數3格不重複

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;

namespace WindowsFormsApplication2
{

    public partial class Form1 : Form
    {
        int rndmoney1 = 100;
        int rndmoney2 = 100;
        int rndmoney3 = 100;
      //int rndmoney4 = 100;
     // int rndmoney5 = 100;
     // int rndmoney6 = 100;
     // int rndmoney7 = 100;
     // int rndmoney8 = 100;
     // int rndmoney9 = 100;
        public Form1()
        {
            InitializeComponent();
        }

        private void button10_Click(object sender, EventArgs e)
        {

        }

        private void button10_Click_1(object sender, EventArgs e)
        {
            button10.Enabled = true;
            Random rnd = new Random();
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            button7.Enabled = false;
            button8.Enabled = false;
            button9.Enabled = false;
            rndmoney1 = rnd.Next(0, 9);
         
            rndmoney2 = rnd.Next(0, 9);
            if((rndmoney1 == rndmoney2))
            {
             
              //MessageBox.Show("數字重複");
                rndmoney2 = rnd.Next(0, 9);
                button2.Text = rndmoney2.ToString();
             
            }
            rndmoney3 = rnd.Next(0, 9);
            do
            {
                //  MessageBox.Show("數字重複");
                rndmoney3 = rnd.Next(0, 9);
                button3.Text = rndmoney3.ToString();
            } while ((rndmoney1 == rndmoney3) || (rndmoney2 == rndmoney3));
            //rndmoney4 = rnd.Next(0, 9);
            //rndmoney5 = rnd.Next(0, 9);
            //rndmoney6 = rnd.Next(0, 9);
            //rndmoney7 = rnd.Next(0, 9);
            //rndmoney8 = rnd.Next(0, 9);
            //rndmoney9 = rnd.Next(0, 9);
            button1.Text = rndmoney1.ToString();
            button2.Text = rndmoney2.ToString();
            button3.Text = rndmoney3.ToString();
            //button4.Text = rndmoney4.ToString();
            //button5.Text = rndmoney5.ToString();
            //button6.Text = rndmoney6.ToString();
            //button7.Text = rndmoney7.ToString();
            //button8.Text = rndmoney8.ToString();
            //button9.Text = rndmoney9.ToString();
        }
    }
}