Repository navigation
Expand file tree
/
Copy pathfrmWeatherAPI.cs
More file actions
156 lines (134 loc) · 4.27 KB
/
Copy pathfrmWeatherAPI.cs
File metadata and controls
156 lines (134 loc) · 4.27 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeatherAPI.Models;
using WeatherAPI.Services;
namespace WeatherAPI
{
public partial class frmWeatherAPI : Form
{
List<Weather> weather;
public frmWeatherAPI()
{
InitializeComponent();
}
private void frmWeatherAPI_Load(object sender, EventArgs e)
{
this.CenterToScreen();
this.SetControls();
}
private void SetControls()
{
//Form
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
//TextBox Settings
this.txtZipCode.MaxLength = 5;
this.txtZipCode.Text = "78758";
this.txtCity.Text = "";
this.txtClouds.Text = "";
this.txtFeelsLike.Text = "";
this.txtHigh.Text = "";
this.txtLatitude.Text = "";
this.txtLongitude.Text = "";
this.txtLow.Text = "";
this.txtTemperature.Text = "";
this.txtWind.Text = "";
this.txtCity.Enabled = false;
this.txtClouds.Enabled = false;
this.txtFeelsLike.Enabled = false;
this.txtHigh.Enabled = false;
this.txtLatitude.Enabled = false;
this.txtLongitude.Enabled = false;
this.txtLow.Enabled = false;
this.txtTemperature.Enabled = false;
this.txtWind.Enabled = false;
}
/*
private void btnStartLocation_Click(object sender, EventArgs e)
{
GEOService myLocation = new GEOService();
myLocation.GetLocationEvent();
Console.WriteLine("Enter any key to quit.");
Console.ReadLine();
}
*/
private void btnWeatherXML_Click(object sender, EventArgs e)
{
this.GetWeather(true);
}
private void btnWeatherJSON_Click(object sender, EventArgs e)
{
this.GetWeather(false);
}
private void GetWeather(bool isXML)
{
string sZip = this.txtZipCode.Text.Trim();
int iZip = Validation(sZip);
if (iZip == 0)
return;
try
{
weather = WeatherService.GetWeather(iZip, isXML);
this.PopulateWeatherData(weather);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
this.SetControls();
}
}
private void PopulateWeatherData(List<Weather> weather)
{
this.txtCity.Text = weather[0].City;
this.txtClouds.Text = weather[0].Clouds;
this.txtFeelsLike.Text = weather[0].FeelsLike;
this.txtHigh.Text = weather[0].HighTemp;
this.txtLatitude.Text = weather[0].Latitude;
this.txtLongitude.Text = weather[0].Longitude;
this.txtLow.Text = weather[0].LowTemp;
this.txtTemperature.Text = weather[0].CurrentTemp;
this.txtWind.Text = weather[0].Wind;
}
private int Validation(string sZip)
{
int iZip = 0;
bool result = int.TryParse(sZip, out iZip);
if (!result)
{
MessageBox.Show("A numeric value must be entered for zip code!");
return iZip;
}
else if (sZip.Length != 5)
{
MessageBox.Show("Zip code must be 5 numbers!");
iZip = 0;
return iZip;
}
return iZip;
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void btonGetWeatherXML_Click(object sender, EventArgs e)
{
this.GetWeather(true);
}
private void btonGetWeatherJSON_Click(object sender, EventArgs e)
{
this.GetWeather(true);
}
private void btnClose_Click_1(object sender, EventArgs e)
{
this.Dispose();
}
}
}