SMForm.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Windows.Forms;
  4. namespace UppPay.Forms
  5. {
  6. public partial class SMForm : Form
  7. {
  8. public JObject OutjObject { get; set; }
  9. public string authcode { get; set; }
  10. private bool isButtonClose = false;
  11. public SMForm()
  12. {
  13. InitializeComponent();
  14. OutjObject = new JObject();
  15. authcode = "";
  16. // 设置窗体在屏幕上居中显示
  17. this.StartPosition = FormStartPosition.CenterScreen;
  18. // 设置窗体始终显示在其他窗口的最前面
  19. this.TopMost = true;
  20. authcodeBox.Focus();
  21. // 为 TextBox 的 KeyDown 事件添加处理程序
  22. authcodeBox.KeyDown += authcodeBox_KeyDown;
  23. // 处理窗体关闭事件,阻止关闭操作
  24. this.FormClosing += MainForm_FormClosing;
  25. authcodeBox.Focus();
  26. }
  27. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  28. {
  29. if (!isButtonClose)
  30. {
  31. OutjObject = new JObject();
  32. OutjObject["errorCode"] = "-1";
  33. OutjObject["errorMessage"] = "收费员已取消操作";
  34. //this.DialogResult = DialogResult.OK;
  35. //this.Close();
  36. }
  37. }
  38. private void authcodeBox_KeyDown(object sender, KeyEventArgs e)
  39. {
  40. // 检查是否按下回车键
  41. if (e.KeyCode == Keys.Enter)
  42. {
  43. // 处理回车键按下事件
  44. SMPayLoading(authcodeBox.Text);
  45. // 阻止回车键的默认行为,避免在多行文本框中换行
  46. e.SuppressKeyPress = true;
  47. }
  48. }
  49. private async void SMPayLoading(string authcode)
  50. {
  51. try
  52. {
  53. if (string.IsNullOrWhiteSpace(authcode))
  54. {
  55. Tlabel.Text = "请输入支付码内容....";
  56. authcodeBox.Focus();
  57. }
  58. else
  59. {
  60. OutjObject = new JObject();
  61. OutjObject["errorCode"] = "0";
  62. OutjObject["errorMessage"] = "扫码成功";
  63. OutjObject["authcode"] = authcode;
  64. isButtonClose = true;
  65. this.DialogResult = DialogResult.OK;
  66. this.Close();
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. // 处理异常
  72. Tlabel.Text = $"发生错误: {ex.Message}";
  73. }
  74. }
  75. }
  76. }