MainWindow.xaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using word = Microsoft.Office.Interop.Word;
  17. namespace WpfApp2
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. }
  28. bool CheckVvod(string input)
  29. {
  30. return !string.IsNullOrEmpty(input)&&!Regex.IsMatch(input, @"[а-яА-Я]");
  31. }
  32. private void Button_Click(object sender, RoutedEventArgs e)
  33. {
  34. try
  35. {
  36. string q1 = Kol1.Text;
  37. string q2 = Kol2.Text;
  38. string SumZa = SumZach.Text;
  39. int sum;
  40. int number1, number2;
  41. bool num1 = int.TryParse(q1, out number1);
  42. bool num2 = int.TryParse(q2, out number2);
  43. if (!CheckVvod(q1)&&!CheckVvod(q2)&&!CheckVvod(SumZa))
  44. {
  45. MessageBox.Show("Не все поля корректно заполнены!");
  46. }
  47. else
  48. {
  49. if (num1 && num2)
  50. {
  51. if (int.TryParse(SumZa, out sum))
  52. {
  53. int summa = number1 * 45 + number2 * 115;
  54. int sdacha = sum - summa;
  55. SumZak.Text = summa.ToString();
  56. if (sdacha < 0)
  57. {
  58. MessageBox.Show("Недостаточно средств!");
  59. }
  60. else
  61. {
  62. CashBack.Text = sdacha.ToString();
  63. }
  64. }
  65. }
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. MessageBox.Show(ex.Message);
  71. }
  72. }
  73. private void Button_Click_1(object sender, RoutedEventArgs e)
  74. {
  75. word.Document document = null;
  76. word.Application app = new word.Application();
  77. string putword = Environment.CurrentDirectory.ToString() + @"\Квитанция.docx";
  78. document = app.Documents.Add(putword);
  79. document.Activate();
  80. word.Bookmarks bookm = document.Bookmarks;
  81. word.Range range;
  82. string dateNow = DateTime.Now.ToString();
  83. string q1 = Kol1.Text;
  84. string q2 = Kol2.Text;
  85. string SumZa = SumZach.Text;
  86. string number = "1";
  87. string sumZakaz = SumZak.Text;
  88. string sdacha = CashBack.Text;
  89. string[] data = new string[7] { SumZa, dateNow, sumZakaz, number, q2, q1, sdacha };
  90. int i = 0;
  91. foreach (word.Bookmark mark in bookm)
  92. {
  93. range = mark.Range;
  94. range.Text = data[i];
  95. i++;
  96. }
  97. document.SaveAs2(Environment.CurrentDirectory.ToString() + @"\Квитанция.docx");
  98. MessageBox.Show("Чек сохранен по пути: " + Environment.CurrentDirectory.ToString());
  99. document.Close();
  100. document = null;
  101. }
  102. }
  103. }