Today’s project is to create an app that will display weather information from a city, including the forecast.  We will be using an extra API, the animaonline weather API using google weather, which is included with the source code.
Tasks demonstrated:

  • Populating text and image fields
  • Saving user input / settings   :rules:  (finally figured this one out)
  • Automatically refreshing data at a specified interval

This is not a quick project, it does take a little bit of time, but it is also a project anyone should be able to do and learn something from.
For an example of the finished product, download mine here:

OK, let’s begin
Download the source file (attached, or go to: cweather – Project Hosting on Google Code)  Included in this source is of course the code, but also the animaonline Weather API, and an icon. 

Step 1: Create our project, and reference the needed API

1. Create a new project, a winforms app of course.  Name it Weather.
2. Rename Form1 to weatherForm
3. Go ahead and copy the API.dll and the icon into the root folder of your project, just to make things easier.
4. Go to the references for your project, and add the animaonline weather api.
5.  Add the following code to the top of the source for your weatherForm:

Step 2: Create user settings

1. Right click on your project, and go to properties.  Then select the settings tab.
2. Create the following settings:
Posted Image
windowPosition is of type System.Drawing.Point
3. Save.

Step 3: Start declaring some variables

1. Open the code for weatherForm.designer.cs
2. Add these line in right after #endregion:

Now before we can go much further, we have to design our form.  This part takes a little more time, since we have to create all of our labels and imageboxes.

Step 4: Design the form

1. Resize the form itself. Make it 696, 231
2. This picture will show you how to make the form look, but don’t actually put the text into the label’s and the textbox:  that’s there so you know what to NAME them.  leave the text blank on all of them EXCEPT Current Conditions and AutoUpdate:  (name those whatever you want, they are static)
The Boxes are PictureBox’s.. name them icnCurrent, icnToday, icnTomorrow, icnDay2, and icnDay3
For your comboBox, name it comboBoxEdit1.  I’ll give you the list items after the picture.
Posted Image
3. The comboBox:
list items are:

  • Never
  • 1 Minute
  • 2 Minutes
  • 5 Minutes
  • 10 Minutes
  • 30 Minutes
  • 1 Hour
  • 2 Hours
  • 4 Hours

Go into the properties for it, and under application settings, property binding, change the Text option to intervalText.  Change dropdown style (near the top of properties) to DropDownList.
4. txtCity: Once again, application settings, property bindings: Text = defaultCity
5. Under components, add a timer.  You can leave this named timer1.  However, once again, go to the property bindings, and set interval to intervalTime, enabled to timerOn.
6. The form itself: data binding again, set the location to windowLocation

Step 5: Let’s start coding

1.  First, lets tell the program when to load and save our settings.
Under initialize component, add the following code:

This loads our settings.
2. We need to write a function to get the weather data and populate the fields.  Here’s the code:

Notice we use a try / catch statement.  This way, if there is an error, the program won’t crash, it will just tell us what happened.
3. Now go back to where we loaded the settings, and add this:

Now, when the app loads, it will automatically get the weather data.
4. Now we need a function to handle the intervals

5. Now go back and doubleclick on the button named getW.
Add the following code to that button:

6. Now double click on timer1 and add this code to it:

7. Now, we need the program to save the users settings.
Under the design of the form, go to the forms events, double click where it says FormClosed, and add this code to the new section:

Step 6: Finishing Touches

1. Form Properties: MaximizeBox and MinimizeBox need to both be False
2. SizeGrip should be set to Hide. 
3. Set the icon to the provided icon.

Just to recap, here’s what your code should look like:

go ahead and give it a run, it should work fine.

What Do You Think on This ? Say Here