Nerviie

Teste Revisão DragandDrop

Dec 3rd, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace _teste_2
  11. {
  12. public partial class Form1 : Form
  13.  {
  14.  int[] array = new int[9];
  15.  int wincount = 0;
  16.  public Form1()
  17.  {
  18.  InitializeComponent();
  19.  }
  20.  private void button1_Click(object sender, EventArgs e)
  21.  {
  22.  upButtons();
  23.  randoms();
  24.  downButtons();
  25.  button1.Enabled = false;
  26.  }
  27.  private void randoms()
  28.  {
  29.  Random rnd = new Random();
  30.  for (int i = 1; i <= 9; i++)
  31.  {
  32.  int x = rnd.Next(0, 9);
  33. while (array[x] > 0)
  34.  x = rnd.Next(0, 9);
  35.  array[x] = i;
  36.  }
  37.  }
  38.  private void upButtons()
  39.  {
  40.  for (int i = 1; i <= 9; i++)
  41.  {
  42.  Button upBtn = new Button();
  43.  upBtn.Name = "upBtn" + i;
  44.  upBtn.Text = "" + i;
  45.  upBtn.Size = new System.Drawing.Size(30, 30);
  46.  upBtn.Location = new System.Drawing.Point(120 + 40 * i, 70);
  47.  upBtn.MouseDown += new MouseEventHandler(this.upBtn_MouseDown);
  48.  upBtn.DragOver += new DragEventHandler(this.Btn_DragOver);
  49.  upBtn.DragDrop += new DragEventHandler(this.Btn_DragDrop);
  50.  upBtn.AllowDrop = true;
  51.  Controls.Add(upBtn);
  52.  }
  53.  }
  54.  private void downButtons()
  55.  {
  56.  int count = 1;
  57.  for (int i = 1; i <= 3; i++)
  58.  {
  59.  for (int h = 1; h <= 3; h++)
  60.  {
  61.  Button downBtn = new Button();
  62.  downBtn.Name = "downBtn" + array[count - 1];
  63.  downBtn.Size = new System.Drawing.Size(50, 50);
  64.  downBtn.Location = new System.Drawing.Point(220 + 60 * i, 100 +
  65. 60 * h);
  66.  downBtn.BackColor = Color.Orange;
  67.  downBtn.DragOver += new DragEventHandler(this.Btn_DragOver);
  68.  downBtn.DragDrop += new DragEventHandler(this.Btn_DragDrop);
  69.  downBtn.AllowDrop = true;
  70.  Controls.Add(downBtn);
  71.  count++;
  72.  }
  73.  }
  74.  }
  75.  private void upBtn_MouseDown(object sender, MouseEventArgs e)
  76.  {
  77.  DoDragDrop(sender, DragDropEffects.Move);
  78.  }
  79.  private void Btn_DragOver(object sender, DragEventArgs e)
  80.  {
  81.  if (e.Data.GetDataPresent(typeof(Button)))
  82.  e.Effect = DragDropEffects.Move;
  83.  else
  84.  e.Effect = DragDropEffects.None;
  85.  }
  86.  private void Btn_DragDrop(object sender, DragEventArgs e)
  87.  {
  88.  var upBtn = e.Data.GetData(typeof(Button)) as Button;
  89.  var downBtn = sender as Button;
  90.  if (upBtn.Text[0] == downBtn.Name[7])
  91.  {
  92.  downBtn.Text = upBtn.Text;
  93.  wincount++;
  94.  }
  95.  if (wincount == 9)
  96.  {
  97.  label1.Visible = true;
  98.  button2.Focus();
  99.  }
  100.  }
  101.  private void button2_Click(object sender, EventArgs e)
  102.  {
  103.  Environment.Exit(0);
  104.  }
  105.  }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment