-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsWindow.xaml.cs
More file actions
75 lines (72 loc) · 2.88 KB
/
SettingsWindow.xaml.cs
File metadata and controls
75 lines (72 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using DxTLabel.Properties;
namespace DxTLabel
{
/// <summary>
/// Interaction logic for SettingsWindow.xaml
/// </summary>
public partial class SettingsWindow : Window
{
public SettingsWindow()
{
InitializeComponent();
}
private void SaveSettings_Click(object sender, RoutedEventArgs e)
{
Settings.Default.Save();
Close();
}
private void ValidateReturnAddress_Click(object sender, RoutedEventArgs e)
{
if (!ReturnAddress.Text.Contains(Environment.NewLine))
{
MessageBox.Show("Invalid Address");
return;
}
if (USPSApiURL.Text.Length < 10 || USPSApiUserID.Text.Length < 5)
{
MessageBox.Show("Insufficient USPS API credentials.");
return;
}
var addrLines = ReturnAddress.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
USPSAddress address = new USPSAddress();
if (addrLines.Length > 1)
{
try
{
address.Address2 = addrLines[1];
if (addrLines.Length == 3 && addrLines[2].Contains(','))
{
var commaPos = addrLines[2].IndexOf(',');
address.City = addrLines[2].Substring(0, commaPos);
address.State = addrLines[2].Substring(commaPos + 1, addrLines[2].IndexOfAny("0123456789".ToCharArray(), commaPos + 2) - (commaPos + 1)).Trim();
}
else if (addrLines.Length == 4 && addrLines[3].Contains(','))
{
address.Address1 = addrLines[2];
var commaPos = addrLines[3].IndexOf(',');
address.City = addrLines[3].Substring(0, commaPos);
address.State = addrLines[3].Substring(commaPos + 1, addrLines[3].IndexOfAny("0123456789".ToCharArray(), commaPos + 2) - (commaPos + 1)).Trim();
}
}
catch (Exception ex)
{
MessageBox.Show(
"Return Address could not be parsed. Ensure that there is a comma between your City and State, " +
"and at least one number following the State.",
"Error Parsing Return Address",
MessageBoxButton.OK,
MessageBoxImage.Error
);
Debug.WriteLine(ex);
return;
}
}
address.FixAddress();
Settings.Default.ReturnAddress = addrLines[0] + Environment.NewLine + address;
}
}
}