|
| 1 | +using System.Net; |
| 2 | +using System.Text; |
| 3 | +using System.Text.Json; |
| 4 | +using System.Windows.Forms; |
| 5 | + |
| 6 | +public partial class ContactControl : UserControl, IUserControl |
| 7 | +{ |
| 8 | + |
| 9 | + public ContactControl() |
| 10 | + { |
| 11 | + InitializeComponent(); |
| 12 | + |
| 13 | + setEmailAnimation(); |
| 14 | + } |
| 15 | + |
| 16 | + #region IUserControl |
| 17 | + public string Title => "Need help ?"; |
| 18 | + |
| 19 | + public bool FeatureEnabled => true; |
| 20 | + |
| 21 | + public void PeriodicUpdate() { } |
| 22 | + |
| 23 | + public void OnClose() { } |
| 24 | + |
| 25 | + public void OnOpen() { setEmailAnimation(); } |
| 26 | + |
| 27 | + |
| 28 | + #endregion |
| 29 | + |
| 30 | + public void SetMessage(string message) |
| 31 | + { |
| 32 | + txtMessage.Text = message; |
| 33 | + } |
| 34 | + |
| 35 | + private async void btnSend_Click(object sender, System.EventArgs e) |
| 36 | + { |
| 37 | + var payloadObject = new |
| 38 | + { |
| 39 | + email = txtEmail.Text, |
| 40 | + subject = $"Yaskawa Desktop showcase - {DateTime.UtcNow.ToString("yyMMddHHmmss")}", |
| 41 | + message = txtMessage.Text |
| 42 | + }; |
| 43 | + |
| 44 | + string payload = JsonSerializer.Serialize(payloadObject); |
| 45 | + var content = new StringContent(payload, Encoding.UTF8, "application/json"); |
| 46 | + |
| 47 | + try |
| 48 | + { |
| 49 | + using (var client = new HttpClient()) |
| 50 | + { |
| 51 | + HttpResponseMessage response = await client.PostAsync("https://formspree.io/f/mleawvqb", content); |
| 52 | + |
| 53 | + if (response.IsSuccessStatusCode) |
| 54 | + { |
| 55 | + MessageBox.Show("Message sent! ", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + MessageBox.Show($"Error: {response.StatusCode}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + catch (HttpRequestException ex) |
| 64 | + { |
| 65 | + MessageBox.Show($"Request error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + private void txtEmail_TextChanged(object sender, System.EventArgs e) |
| 71 | + { |
| 72 | + setEmailAnimation(); |
| 73 | + } |
| 74 | + |
| 75 | + private void setEmailAnimation() |
| 76 | + { |
| 77 | + if (chkAnonymous.Checked || (txtEmail.Text.Contains("@") && txtEmail.Text.Contains("."))) |
| 78 | + { |
| 79 | + emailErrorProvider.Clear(); |
| 80 | + btnSend.Enabled = true; |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + emailErrorProvider.SetError(txtEmail, "please provide an email to get an answer"); |
| 85 | + btnSend.Enabled = false; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private void chkAnonymous_CheckedChanged(object sender, System.EventArgs e) |
| 90 | + { |
| 91 | + if (chkAnonymous.Checked) txtEmail.Text = ""; |
| 92 | + txtEmail.Enabled = !chkAnonymous.Checked; |
| 93 | + setEmailAnimation(); |
| 94 | + } |
| 95 | +} |
0 commit comments