FormGetMaterial.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using static СУБД_Фабрика.FormGetMaterial;
  14. namespace СУБД_Фабрика
  15. {
  16. public partial class FormGetMaterial : Form
  17. {
  18. public FormGetMaterial()
  19. {
  20. InitializeComponent();
  21. }
  22. public struct Tkan
  23. {
  24. public string TitleTkan, ColorTkan, WidthTkan, LengthTkan, Comment;
  25. public Image PhotoTkan;
  26. }
  27. public struct Furn
  28. {
  29. public string TitleFurn, CountFurn;
  30. public Image PhotoFurn;
  31. }
  32. SqlConnection Con = new SqlConnection(FormMain.txtCon);
  33. List<Tkan> LstTkan = new List<Tkan>();
  34. List<Furn> LstFurn = new List<Furn>();
  35. private void BtnLoadPhoto_Click(object sender, EventArgs e)
  36. {
  37. try
  38. {
  39. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  40. PbxPhotoTkan.Image = Image.FromFile(openFileDialog1.FileName);
  41. }
  42. catch { }
  43. }
  44. private void BtnClearPhoto_Click(object sender, EventArgs e)
  45. {
  46. if (MessageBox.Show("Очистить фотографию?", "Внимание",
  47. MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
  48. PbxPhotoTkan.Image = null;
  49. }
  50. private void BtnAddTkan_Click(object sender, EventArgs e)
  51. {
  52. Tkan Tk1 = new Tkan();
  53. Tk1.TitleTkan = TbxTitleTkan.Text;
  54. Tk1.ColorTkan = TbxColorTkan.Text;
  55. Tk1.WidthTkan = TbxWidthTkan.Text;
  56. Tk1.LengthTkan = TbxLengthTkan.Text;
  57. Tk1.Comment = TbxCommentTkan.Text;
  58. Tk1.PhotoTkan = PbxPhotoTkan.Image;
  59. LstTkan.Add(Tk1);
  60. DgvTkan.Rows.Add(Tk1.TitleTkan, Tk1.ColorTkan, Tk1.WidthTkan, Tk1.LengthTkan, Tk1.Comment);
  61. }
  62. private void BtnDelTkan_Click(object sender, EventArgs e)
  63. {
  64. int NumTkan = DgvTkan.CurrentRow.Index;
  65. if (NumTkan >= 0)
  66. {
  67. if (MessageBox.Show("Удалить выделенную ткань?",
  68. "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  69. {
  70. LstTkan.RemoveAt(NumTkan);
  71. DgvTkan.Rows.RemoveAt(NumTkan);
  72. }
  73. }
  74. else
  75. MessageBox.Show("Ткань не выделена",
  76. "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  77. }
  78. private void BtnOK_Click(object sender, EventArgs e)
  79. {
  80. foreach (Tkan tkan in LstTkan)
  81. {
  82. Con.Open();
  83. if (tkan.PhotoTkan != null) // если есть изображение
  84. {
  85. string PhotoName = Guid.NewGuid().ToString() + ".jpg";
  86. string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Application.StartupPath + "\\Clothes", PhotoName);
  87. string txtQuery = $@"insert into Cloth(Title, Color, Width, Length, Comment, Photo) values('{tkan.TitleTkan}','{tkan.ColorTkan}',{tkan.WidthTkan},{tkan.LengthTkan},'{tkan.Comment}','{PhotoName}')";
  88. SqlCommand query = new SqlCommand(txtQuery, Con);
  89. query.ExecuteNonQuery();
  90. tkan.PhotoTkan.Save(fileName, ImageFormat.Jpeg);
  91. }
  92. else
  93. {
  94. string txtQuery = $@"insert into Cloth(Title, Color, Width, Length, Comment) values('{tkan.TitleTkan}','{tkan.ColorTkan}',{tkan.WidthTkan},{tkan.LengthTkan},'{tkan.Comment}')";
  95. SqlCommand query = new SqlCommand(txtQuery, Con);
  96. query.ExecuteNonQuery();
  97. }
  98. Con.Close();
  99. }
  100. foreach(Furn frn in LstFurn)
  101. {
  102. Con.Open();
  103. if(frn.PhotoFurn != null)
  104. {
  105. string PhotoName = Guid.NewGuid().ToString() + ".jpg";
  106. string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Application.StartupPath + "\\Clothes", PhotoName);
  107. string txtQuery = $@"insert into Furnitura(namefur, countfur, photo) values('{frn.TitleFurn}',{frn.CountFurn},'{PhotoName}')";
  108. SqlCommand query = new SqlCommand(txtQuery, Con);
  109. query.ExecuteNonQuery();
  110. frn.PhotoFurn.Save(fileName, ImageFormat.Jpeg);
  111. }
  112. else
  113. {
  114. string txtQuery = $@"insert into Furnitura(namefur, countfur) values('{frn.TitleFurn}',{frn.CountFurn})";
  115. SqlCommand query = new SqlCommand(txtQuery, Con);
  116. query.ExecuteNonQuery();
  117. }
  118. Con.Close();
  119. }
  120. MessageBox.Show("Документ принят к учету.", "Внимание!",
  121. MessageBoxButtons.OK, MessageBoxIcon.Information);
  122. this.Close();
  123. }
  124. private void BtnSavePhotoFurn_Click(object sender, EventArgs e)
  125. {
  126. try
  127. {
  128. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  129. PbxPhotoFurn.Image = Image.FromFile(openFileDialog1.FileName);
  130. }
  131. catch { }
  132. }
  133. private void BtnClearPhotoFurn_Click(object sender, EventArgs e)
  134. {
  135. if (MessageBox.Show("Очистить фотографию?", "Внимание",
  136. MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
  137. PbxPhotoFurn.Image = null;
  138. }
  139. private void BtnAddFur_Click(object sender, EventArgs e)
  140. {
  141. Furn frn = new Furn();
  142. frn.TitleFurn = TbxTitleFur.Text;
  143. frn.CountFurn = TbxCountFur.Text;
  144. frn.PhotoFurn = PbxPhotoFurn.Image;
  145. LstFurn.Add(frn);
  146. DgvFurn.Rows.Add(frn.TitleFurn, frn.CountFurn);
  147. }
  148. private void BtnDelFur_Click(object sender, EventArgs e)
  149. {
  150. int NumFurn = DgvFurn.CurrentRow.Index;
  151. if (NumFurn >= 0)
  152. {
  153. if (MessageBox.Show("Удалить выделенную фурнитуру?",
  154. "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  155. {
  156. LstFurn.RemoveAt(NumFurn);
  157. DgvFurn.Rows.RemoveAt(NumFurn);
  158. }
  159. }
  160. else
  161. MessageBox.Show("Фурнитура не выделена",
  162. "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  163. }
  164. }
  165. }