Form1.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Microsoft.Office.Interop.Word;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using word = Microsoft.Office.Interop.Word;
  13. namespace WindowsFormsApp1
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public double RateNorm = 0.7;
  18. public double RateNorm2 = 0.3;
  19. public double ExcessTariff = 1.6;
  20. public int MinutesTariff1 = 200;
  21. public int MinutesTariff2=100;
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. }
  26. private void Calculate_Click(object sender, EventArgs e)
  27. {
  28. try
  29. {
  30. string Minute1 = Minutes.Text;
  31. if (!ANGLTEXT(Minute1))
  32. {
  33. throw new Exception("Некорректное значение. Введите целое число.");
  34. }
  35. int Minute = Convert.ToInt32(Minutes.Text);
  36. if (Minute > 0 && Minute <= 4000)
  37. {
  38. if(radioButton1.Checked==true)
  39. {
  40. if (Minute > MinutesTariff1)
  41. {
  42. double MinuteFact = MinutesTariff1 * RateNorm + ((Minute- MinutesTariff1) * ExcessTariff);
  43. int MinuteUp = Minute - MinutesTariff1;
  44. Summ.Text = "Минут свверх установленной нормы: "+ MinuteUp;
  45. SummIt.Text = "К оплате: " + MinuteFact;
  46. Summ.Visible = true;
  47. }
  48. else
  49. {
  50. double MinuteFact = Minute * RateNorm;
  51. SummIt.Text = "К оплате: " + MinuteFact;
  52. }
  53. }
  54. if(radioButton2.Checked == true)
  55. {
  56. if (Minute > MinutesTariff2)
  57. {
  58. double MinuteFact = MinutesTariff2 * RateNorm2 + ((Minute - MinutesTariff2) * ExcessTariff);
  59. int MinuteUp = Minute - MinutesTariff2;
  60. Summ.Text = "Минут свверх установленной нормы: " + MinuteUp;
  61. SummIt.Text = "К оплате: " + MinuteFact;
  62. Summ.Visible = true;
  63. }
  64. else
  65. {
  66. double MinuteFact = Minute * RateNorm2;
  67. SummIt.Text = "К оплате: " + MinuteFact;
  68. }
  69. }
  70. else if(radioButton1.Checked == false&& radioButton2.Checked == true)
  71. {
  72. MessageBox.Show("Выберите тариф!");
  73. }
  74. }
  75. else
  76. {
  77. MessageBox.Show("Значение должно быть между 0 и 4000.");
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. MessageBox.Show(ex.Message);
  83. Close();
  84. }
  85. }
  86. public bool ANGLTEXT(string input)
  87. {
  88. return !string.IsNullOrEmpty(input)&& Regex.IsMatch(input,@"^\d+$")&&!Regex.IsMatch(input, @"[а-яА-Я]");
  89. }
  90. private void Minutes_KeyPress(object sender, KeyPressEventArgs e)
  91. {
  92. }
  93. private void button2_Click(object sender, EventArgs e)
  94. {
  95. DateTime Date1 = DateTime.Now;
  96. string Tariff = "";
  97. if (radioButton1.Checked == true)
  98. {
  99. Tariff = "Тариф 1";
  100. }
  101. if (radioButton2.Checked == true)
  102. {
  103. Tariff = "Тариф 2";
  104. }
  105. string summ = SummIt.Text;
  106. word.Document document = null;
  107. word.Application app = new word.Application();
  108. string putword = Environment.CurrentDirectory.ToString() + @"C:\Users\Евгений\source\repos\WindowsFormsApp1\WindowsFormsApp1\res\Шаблон.docx";
  109. document = app.Documents.Add(putword);
  110. document.Activate();
  111. word.Bookmarks bookm = document.Bookmarks;
  112. word.Range range;
  113. string[] data = new string[3] { Convert.ToString(Date1), Tariff, summ };
  114. int i = 0;
  115. foreach (word.Bookmark mark in bookm)
  116. {
  117. range = mark.Range;
  118. range.Text = data[i];
  119. i++;
  120. }
  121. document.SaveAs2(Environment.CurrentDirectory.ToString() + @"\Сертификат.docx");
  122. MessageBox.Show("Сертификат сохранен по пути: " + Environment.CurrentDirectory.ToString());
  123. document.Close();
  124. document = null;
  125. }
  126. }
  127. }