Advertisement
AJBF

Código de ventana de configuración en C#

Nov 23rd, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 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.Windows.Forms;
  9. using System.IO.Ports;
  10.  
  11. namespace Camsecurity
  12. {
  13. public partial class fmConfig : Form
  14. {
  15. private SerialPort port;
  16. public string portcomu = "";
  17. public bool verif = false;
  18.  
  19. public fmConfig()
  20. {
  21. InitializeComponent();
  22. cmbPort.SelectedIndex = 0;
  23. }
  24.  
  25. private void fmConfig_Load(object sender, EventArgs e)
  26. {
  27. chbMovCam.Checked = true;
  28. chbMovCam.Checked = Properties.Settings.Default.chbMVRecord;
  29. chbPIR.Checked = false;
  30. chbPIR.Checked = Properties.Settings.Default.chbPIRRecord;
  31. btnAceptar.Enabled = false;
  32. }
  33.  
  34. /** LISTO **/
  35. private void btnProbar_Click(object sender, EventArgs e)
  36. {
  37. string puerto = Convert.ToString(cmbPort.SelectedItem);
  38. try
  39. {
  40. port = new SerialPort(puerto, 9600);
  41.  
  42. if (!port.IsOpen)
  43. {
  44. port.Open();
  45. if (chbMovCam.Checked == true)
  46. {
  47. port.WriteLine("3");
  48. }
  49. if (chbPIR.Checked == true)
  50. {
  51. port.WriteLine("4");
  52. contSegui.Enabled = true;
  53. }
  54. }
  55.  
  56. if (port.IsOpen)
  57. {
  58. if (chbMovCam.Checked == true)
  59. {
  60. port.WriteLine("3");
  61. }
  62. if (chbPIR.Checked == true)
  63. {
  64. port.WriteLine("4");
  65. }
  66. port.Close();
  67. verif = true;
  68. btnAceptar.Enabled = true;
  69. }
  70. }
  71. catch (Exception a)
  72. {
  73. MessageBox.Show("A ocurrido un error durante la inicialización de la comunicación del programa con arduino, el error es el siguiente: \n\r\n\r" + a + "\n\r\n\rSi Este mensaje le aparece repetidas comuniquese con el EstaQueArreglamosSuProblema_Kpo@gmail.com \n\rgracias por su atención. ", "No se ha podido iniciar la comunicación de puerto");
  74. }
  75. portcomu = Convert.ToString(cmbPort.SelectedItem);
  76. }
  77.  
  78. /** LISTO **/
  79. private void btnAceptar_Click(object sender, EventArgs e)
  80. {
  81. if (MessageBox.Show("¿Está seguro?\n\rSi se módifica la configuración puede haber errores", "Aceptar configuración", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
  82. {
  83. bool probando = false;
  84.  
  85. if (verif == true)
  86. {
  87. contMov.Enabled = true;
  88. this.Hide();
  89. portcomu = Convert.ToString(cmbPort.SelectedItem);
  90. }
  91. else
  92. {
  93. string puerto = Convert.ToString(cmbPort.SelectedItem);
  94. port = new SerialPort(puerto, 9600);
  95.  
  96. try
  97. {
  98. if (!port.IsOpen)
  99. {
  100. port.Open();
  101. }
  102.  
  103. if (port.IsOpen)
  104. {
  105. port.Close();
  106. probando = true;
  107. }
  108.  
  109. if (probando == true)
  110. {
  111. this.Hide();
  112. portcomu = Convert.ToString(cmbPort.SelectedItem);
  113. }
  114. else
  115. {
  116. MessageBox.Show("A ocurrido un error en la configuración, por favor, revice que todo este corretamente puesto.", "Error en la configuración");
  117. }
  118. }
  119. catch (Exception n)
  120. {
  121. MessageBox.Show("A ocurrido un error durante la inicialización de la comunicación del programa con arduino, el error es el siguiente: \n\r\n\r" + n + "\n\r\n\rSi Este mensaje le aparece repetidas comuniquese con el EstaQueArreglamosSuProblema_Kpo@gmail.com \n\rgracias por su atención. ", "No se ha podido iniciar la comunicación de puerto");
  122. }
  123. }
  124. }
  125. }
  126.  
  127. /** LISTO **/
  128. private void btnCancelar_Click(object sender, EventArgs e)
  129. {
  130. if (MessageBox.Show("¿Está seguro de cancelar?", "Cancelar", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
  131. {
  132. this.Hide();
  133. portcomu = Convert.ToString(cmbPort.SelectedItem);
  134. }
  135.  
  136. }
  137.  
  138. /** LISTO **/
  139. private void contMov_Tick(object sender, EventArgs e)
  140. {
  141. if (chbMovCam.Checked == true)
  142. {
  143. string puerto = Convert.ToString(cmbPort.SelectedItem);
  144. try
  145. {
  146. port = new SerialPort(puerto, 9600);
  147. if (!port.IsOpen)
  148. {
  149. port.Open();
  150. Console.WriteLine("3");
  151. port.Close();
  152. }
  153. else
  154. {
  155. Console.WriteLine("3");
  156. port.Close();
  157. }
  158. }
  159. catch (Exception a)
  160. {
  161. MessageBox.Show("A ocurrido un error durante la inicialización de la comunicación del programa con arduino, el error es el siguiente: \n\r\n\r" + a + "\n\r\n\rSi Este mensaje le aparece repetidas comuniquese con el EstaQueArreglamosSuProblema_Kpo@gmail.com \n\rgracias por su atención. ", "No se ha podido iniciar la comunicación de puerto");
  162. }
  163. }
  164. }
  165.  
  166. /** LISTO **/
  167. private string puertocomu()
  168. {
  169. return portcomu;
  170. }
  171.  
  172. private void fmConfig_FormClosing(object sender, FormClosingEventArgs e)
  173. {
  174. Properties.Settings.Default.chbMVRecord = chbMovCam.Checked;
  175. Properties.Settings.Default.chbPIRRecord = chbPIR.Checked;
  176. Properties.Settings.Default.Save();
  177. }
  178.  
  179. private void contSegui_Tick(object sender, EventArgs e)
  180. {
  181. if (chbPIR.Checked == true)
  182. {
  183. string puerto = Convert.ToString(cmbPort.SelectedItem);
  184. try
  185. {
  186. port = new SerialPort(puerto, 9600);
  187. if (!port.IsOpen)
  188. {
  189. port.Open();
  190. Console.WriteLine("4");
  191. port.Close();
  192. }
  193. else
  194. {
  195. Console.WriteLine("4");
  196. port.Close();
  197. }
  198. }
  199. catch (Exception a)
  200. {
  201. MessageBox.Show("A ocurrido un error durante la inicialización de la comunicación del programa con arduino, el error es el siguiente: \n\r\n\r" + a + "\n\r\n\rSi Este mensaje le aparece repetidas comuniquese con el EstaQueArreglamosSuProblema_Kpo@gmail.com \n\rgracias por su atención. ", "No se ha podido iniciar la comunicación de puerto");
  202. }
  203. }
  204. }
  205.  
  206. public override string ToString()
  207. {
  208. if (chbMovCam.Checked == true)
  209. {
  210. return String.Concat(Convert.ToString(chbMovCam.Checked));
  211. }
  212. else
  213. {
  214. if (chbPIR.Checked == true)
  215. {
  216. return String.Concat(Convert.ToString(chbMovCam.Checked));
  217. }
  218. else
  219. {
  220. return String.Concat(Convert.ToString(false));
  221. }
  222. }
  223. }
  224.  
  225. /*private void chbMovCam_CheckedChanged(object sender, EventArgs e)
  226. {
  227. if (chbPIR.Checked == true)
  228. {
  229. chbPIR.Checked = false;
  230. chbMovCam.Checked = true;
  231. }
  232. else
  233. {
  234. chbMovCam.Checked = true;
  235. }
  236. }
  237.  
  238. private void chbPIR_CheckedChanged(object sender, EventArgs e)
  239. {
  240. if (chbMovCam.Checked == true)
  241. {
  242. chbMovCam.Checked = false;
  243. chbPIR.Checked = true;
  244. }
  245. else
  246. {
  247. chbPIR.Checked = true;
  248. }
  249. }*/
  250. }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement