Advertisement
Festelo

C# code

Feb 28th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15.  
  16. namespace WpfApplication6
  17. {
  18.     /// <summary>
  19.     /// Interaction logic for MainWindow.xaml
  20.     /// </summary>
  21.     public partial class MainWindow : Window
  22.     {
  23.         private void StopTextBoxClick(object sender, MouseButtonEventArgs e)
  24.         {
  25.             TextBox text = sender as TextBox;
  26.             if (text != null && !text.IsKeyboardFocusWithin)
  27.             {
  28.                 e.Handled = true;
  29.                 text.Focus();
  30.             }
  31.         }
  32.         public MainWindow()
  33.         {
  34.             InitializeComponent();
  35.             string[] lines = System.IO.File.ReadAllLines(Environment.CurrentDirectory + @"\words.txt");
  36.             char[,] table = new char[9, 9];
  37.             Random rndA = new Random();
  38.             int rnd = rndA.Next(0, lines.Length/10);
  39.             TextBox[,] boxes = { { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9 } ,
  40.                                 { textBox10, textBox11, textBox12, textBox13, textBox14, textBox15, textBox16, textBox17, textBox18 },
  41.                                 { textBox19, textBox20, textBox21, textBox22, textBox23, textBox24, textBox25, textBox26, textBox27 },
  42.                                 { textBox28, textBox29, textBox30, textBox31, textBox32, textBox33, textBox34, textBox35, textBox36 },
  43.                                 { textBox37, textBox38, textBox39, textBox40, textBox41, textBox42, textBox43, textBox44, textBox45 },
  44.                                 { textBox46, textBox47, textBox48, textBox49, textBox50, textBox51, textBox52, textBox53, textBox54 },
  45.                                 { textBox55, textBox56, textBox57, textBox58, textBox59, textBox60, textBox61, textBox62, textBox63 },
  46.                                 { textBox64, textBox65, textBox66, textBox67, textBox68, textBox69, textBox70, textBox71, textBox72 },
  47.                                 { textBox73, textBox74, textBox75, textBox76, textBox77, textBox78, textBox79, textBox80, textBox81 }};
  48.             for (int i = 0; i<9; i++)
  49.             {
  50.                 char[] home = lines[rnd * 10 + i].ToCharArray();
  51.                 for (int j = 0; j<9; j++)
  52.                 {
  53.                     table[i, j] = home[j];
  54.                     if(Convert.ToString(home[j]) != " ") boxes[i, j].Text = Convert.ToString(home[j]);
  55.                 }
  56.             }
  57.         }
  58.  
  59.         private void TextChanged(object sender, TextChangedEventArgs e)
  60.         {
  61.             TextBox[,] boxes = { { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9 } ,
  62.                                 { textBox10, textBox11, textBox12, textBox13, textBox14, textBox15, textBox16, textBox17, textBox18 },
  63.                                 { textBox19, textBox20, textBox21, textBox22, textBox23, textBox24, textBox25, textBox26, textBox27 },
  64.                                 { textBox28, textBox29, textBox30, textBox31, textBox32, textBox33, textBox34, textBox35, textBox36 },
  65.                                 { textBox37, textBox38, textBox39, textBox40, textBox41, textBox42, textBox43, textBox44, textBox45 },
  66.                                 { textBox46, textBox47, textBox48, textBox49, textBox50, textBox51, textBox52, textBox53, textBox54 },
  67.                                 { textBox55, textBox56, textBox57, textBox58, textBox59, textBox60, textBox61, textBox62, textBox63 },
  68.                                 { textBox64, textBox65, textBox66, textBox67, textBox68, textBox69, textBox70, textBox71, textBox72 },
  69.                                 { textBox73, textBox74, textBox75, textBox76, textBox77, textBox78, textBox79, textBox80, textBox81 }};
  70.             // ТЫ СКАЗАЛ СДЕЛАТЬ ЭТО
  71.             for (int i = 0; i < 9; i++)
  72.             {
  73.                 for (int j = 0; j < 9; j++)
  74.                 {
  75.                     boxes[i, j].Background = Brushes.White;
  76.                 }
  77.             }
  78.             //
  79.             try { int G = Convert.ToInt32((sender as TextBox).Text);  }
  80.             catch { (sender as TextBox).Text = ""; return; }
  81.             if((sender as TextBox).Text == "0"){ (sender as TextBox).Text = ""; return;}
  82.             int hor = 0;
  83.             int ver = 0;
  84.             for (int i = 0; i < 9; i++)
  85.             {
  86.                 for (int j = 0; j < 9; j++)
  87.                 {
  88.                     if (boxes[i, j] == (sender as TextBox))
  89.                     {
  90.                         ver = i;
  91.                         hor = j;
  92.                     }
  93.                 }
  94.             }
  95.             for (int i = 0; i < 9; i++)
  96.             {
  97.                 if ((sender as TextBox).Text == "")
  98.                 {
  99.                     (sender as TextBox).Background = Brushes.White;
  100.  
  101.                     break;
  102.                 }
  103.                 if ((sender as TextBox).Text == boxes[i , hor].Text)
  104.                 {
  105.                     if (boxes[i, hor] != (sender as TextBox))
  106.                     {
  107.                         boxes[i, hor].Background = Brushes.Red;
  108.                         (sender as TextBox).Background = Brushes.Red;
  109.                     }
  110.                 }
  111.                 if ((sender as TextBox).Text == boxes[ver, i].Text)
  112.                 {
  113.                     if(boxes[ver, i] != (sender as TextBox))
  114.                     {
  115.                         boxes[ver, i].Background = Brushes.Red;
  116.                         (sender as TextBox).Background = Brushes.Red;
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement