+ Reply to Thread
Results 1 to 3 of 3

Thread: C# Tutorial: Creating a personal weather app

  1. #1
    Programming Professional Parabola will become famous soon enough Parabola will become famous soon enough Parabola's Avatar
    Join Date
    Jul 2009
    Location
    Texas
    Age
    26
    Posts
    305
    Blog Entries
    3

    C# Tutorial: Creating a personal weather app

    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 (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:
    http://jmangum.codecall.net/weather/setup.exe

    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:
    Code:
    using Animaonline.WeatherAPI;
    using System.Threading;
    using Weather.Properties;
    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:

    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:
    Code:
            private Animaonline.WeatherAPI.WeatherData wD;
            private string City;
            private int delay;
    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.

    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:
    Code:
                delay = Settings.Default.intervalTime;
                txtCity.Text = Settings.Default.defaultCity;
                this.Location = Settings.Default.windowPosition;
                comboBoxEdit1.Text = Settings.Default.intervalText;
                timer1.Enabled = Settings.Default.timerOn;
    This loads our settings.
    2. We need to write a function to get the weather data and populate the fields. Here's the code:
    Code:
    private void getWeather(string City)
            {
                try
                {
                    //Get the data
                    wD = WeatherAPI.GetWeather(LanguageCode.en_US, City);
    
                    //Set image locations
                    string baseURL = "http://www.google.com";
                    string iconToday = baseURL + wD.iconTODAY;
                    string icon = baseURL + wD.icon;
                    string iconTOMORROW = baseURL + wD.iconTOMORROW;
                    string iconDAY2 = baseURL + wD.iconDAY2;
                    string iconDAY3 = baseURL + wD.iconDAY3;
    
                    //Set images
                    icnCurrent.ImageLocation = icon;
                    icnDay2.ImageLocation = iconDAY2;
                    icnDay3.ImageLocation = iconDAY3;
                    icnToday.ImageLocation = iconToday;
                    icnTomorrow.ImageLocation = iconTOMORROW;
    
                    //Current Conditions
                    lblCity.Text = wD.city;
                    lblCurrentCond.Text = wD.condition;
                    lblCurrentF.Text = "Temperature: " + wD.temp_f.ToString() + "°F";
                    lblWind.Text = wD.wind_condition;
    
                    //Day 2's Conditions
                    lblDay2.Text = wD.day_of_weekDAY2;
                    lblDay2Cond.Text = wD.conditionDAY2;
                    lblDay2High.Text = "High:  " + wD.highDAY2.ToString() + "°F";
                    lblDay2Low.Text = "Low:  " + wD.lowDAY2.ToString() + "°F";
    
                    //Day 3's Conditions
                    lblDay3.Text = wD.day_of_weekDAY3;
                    lblDay3Cond.Text = wD.conditionDAY3;
                    lblDay3High.Text = "High:  " + wD.highDAY3.ToString() + "°F";
                    lblDay3Low.Text = "Low:  " + wD.lowDAY3.ToString() + "°F";
    
                    //Today's Conditions
                    lblToday.Text = wD.day_of_weekTODAY;
                    lblTodayCond.Text = wD.conditionTODAY;
                    lblTodayHigh.Text = "High:  " + wD.highTODAY.ToString() + "°F";
                    lblTodayLow.Text = "Low:  " + wD.lowTODAY.ToString() + "°F";
    
                    //Tomorrow's Conditions
                    lblTomorrow.Text = wD.day_of_weekTOMORROW;
                    lblTomorrowCond.Text = wD.conditionTOMORROW;
                    lblTomorrowHigh.Text = "High:  " + wD.highTOMORROW.ToString() + "°F";
                    lblTomorrowLow.Text = "Low:  " + wD.lowTOMORROW.ToString() + "°F";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
    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:
    Code:
                //Get Initial Weather
                City = txtCity.Text;
                getWeather(City);
    Now, when the app loads, it will automatically get the weather data.
    4. Now we need a function to handle the intervals
    Code:
            private void getDelay(string Selection)
            {
                //Set the delay based on User Selection
                if (Selection == "Never")
                    delay = 1;
                if (Selection == "1 Minute")
                    delay = 1;
                if (Selection == "2 Minutes")
                    delay = 2;
                if (Selection == "5 Minutes")
                    delay = 5;
                if (Selection == "10 Minute")
                    delay = 10;
                if (Selection == "30 Minutes")
                    delay = 30;
                if (Selection == "1 Hour")
                    delay = 1 * 60;
                if (Selection == "2 Hours")
                    delay = 2 * 60;
                if (Selection == "4 Hours")
                    delay = 4 * 60;
                if (Selection == "Select AutoUpdate Interval")
                    delay = 1;
                delay = (delay * 60000);
                
            }
    5. Now go back and doubleclick on the button named getW.
    Add the following code to that button:
    Code:
    //Set delay from minutes to milliseconds
                getDelay(comboBoxEdit1.Text);
                
                //Get Weather
                City = txtCity.Text;
                getWeather(City);
    
                //Set timer1
                if (comboBoxEdit1.Text == "Never")
                {
                    timer1.Enabled = false;
                }
                else
                {
                    timer1.Interval = delay;
                    timer1.Enabled = true;
                }
    6. Now double click on timer1 and add this code to it:
    Code:
                //Refresh Weather Data
                getWeather(City);
    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:
    Code:
                //Save Settings
                Settings.Default.defaultCity = txtCity.Text;
                Settings.Default.intervalText = comboBoxEdit1.Text;
                Settings.Default.windowPosition = this.Location;
                Settings.Default.timerOn = timer1.Enabled;
                Settings.Default.intervalTime = delay;
                Settings.Default.Save();
    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:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Animaonline.WeatherAPI;
    using System.Threading;
    using Weather.Properties;
    
    namespace Weather
    {
        public partial class weatherForm : Form
        {
            public weatherForm()
            {
                InitializeComponent();
    
                //Load settings
                delay = Settings.Default.intervalTime;
                txtCity.Text = Settings.Default.defaultCity;
                this.Location = Settings.Default.windowPosition;
                comboBoxEdit1.Text = Settings.Default.intervalText;
                timer1.Enabled = Settings.Default.timerOn;
    
                //Get Initial Weather
                City = txtCity.Text;
                getWeather(City);
                
            }
    
            private void weatherForm_Load(object sender, EventArgs e)
            {
    
            }
    
            private void getWeather(string City)
            {
                try
                {
                    //Get the data
                    wD = WeatherAPI.GetWeather(LanguageCode.en_US, City);
    
                    //Set image locations
                    string baseURL = "http://www.google.com";
                    string iconToday = baseURL + wD.iconTODAY;
                    string icon = baseURL + wD.icon;
                    string iconTOMORROW = baseURL + wD.iconTOMORROW;
                    string iconDAY2 = baseURL + wD.iconDAY2;
                    string iconDAY3 = baseURL + wD.iconDAY3;
    
                    //Set images
                    icnCurrent.ImageLocation = icon;
                    icnDay2.ImageLocation = iconDAY2;
                    icnDay3.ImageLocation = iconDAY3;
                    icnToday.ImageLocation = iconToday;
                    icnTomorrow.ImageLocation = iconTOMORROW;
    
                    //Current Conditions
                    lblCity.Text = wD.city;
                    lblCurrentCond.Text = wD.condition;
                    lblCurrentF.Text = "Temperature: " + wD.temp_f.ToString() + "°F";
                    lblWind.Text = wD.wind_condition;
    
                    //Day 2's Conditions
                    lblDay2.Text = wD.day_of_weekDAY2;
                    lblDay2Cond.Text = wD.conditionDAY2;
                    lblDay2High.Text = "High:  " + wD.highDAY2.ToString() + "°F";
                    lblDay2Low.Text = "Low:  " + wD.lowDAY2.ToString() + "°F";
    
                    //Day 3's Conditions
                    lblDay3.Text = wD.day_of_weekDAY3;
                    lblDay3Cond.Text = wD.conditionDAY3;
                    lblDay3High.Text = "High:  " + wD.highDAY3.ToString() + "°F";
                    lblDay3Low.Text = "Low:  " + wD.lowDAY3.ToString() + "°F";
    
                    //Today's Conditions
                    lblToday.Text = wD.day_of_weekTODAY;
                    lblTodayCond.Text = wD.conditionTODAY;
                    lblTodayHigh.Text = "High:  " + wD.highTODAY.ToString() + "°F";
                    lblTodayLow.Text = "Low:  " + wD.lowTODAY.ToString() + "°F";
    
                    //Tomorrow's Conditions
                    lblTomorrow.Text = wD.day_of_weekTOMORROW;
                    lblTomorrowCond.Text = wD.conditionTOMORROW;
                    lblTomorrowHigh.Text = "High:  " + wD.highTOMORROW.ToString() + "°F";
                    lblTomorrowLow.Text = "Low:  " + wD.lowTOMORROW.ToString() + "°F";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
    
            private void getDelay(string Selection)
            {
                //Set the delay based on User Selection
                if (Selection == "Never")
                    delay = 1;
                if (Selection == "1 Minute")
                    delay = 1;
                if (Selection == "2 Minutes")
                    delay = 2;
                if (Selection == "5 Minutes")
                    delay = 5;
                if (Selection == "10 Minute")
                    delay = 10;
                if (Selection == "30 Minutes")
                    delay = 30;
                if (Selection == "1 Hour")
                    delay = 1 * 60;
                if (Selection == "2 Hours")
                    delay = 2 * 60;
                if (Selection == "4 Hours")
                    delay = 4 * 60;
                if (Selection == "Select AutoUpdate Interval")
                    delay = 1;
                delay = (delay * 60000);
    
            }
    
            private void getW_Click(object sender, EventArgs e)
            {
                //Set delay from minutes to milliseconds
                getDelay(comboBoxEdit1.Text);
    
                //Get Weather
                City = txtCity.Text;
                getWeather(City);
    
                //Set timer1
                if (comboBoxEdit1.Text == "Never")
                {
                    timer1.Enabled = false;
                }
                else
                {
                    timer1.Interval = delay;
                    timer1.Enabled = true;
                }
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                //Refresh Weather Data
                getWeather(City);
            }
    
            private void weatherForm_FormClosed(object sender, FormClosedEventArgs e)
            {
                //Save Settings
                Settings.Default.defaultCity = txtCity.Text;
                Settings.Default.intervalText = comboBoxEdit1.Text;
                Settings.Default.windowPosition = this.Location;
                Settings.Default.timerOn = timer1.Enabled;
                Settings.Default.intervalTime = delay;
                Settings.Default.Save();
            }
        }
    }
    go ahead and give it a run, it should work fine.
    Bijgevoegde bestanden
    Programmer (n): An organism that can turn caffeine into code.
    Programming would be so much easier without all the users.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    Re: C# Tutorial: Creating a personal weather app

    Wow, very cool tutorial/project! +rep

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    13,155
    Blog Entries
    59

    Re: C# Tutorial: Creating a personal weather app

    Very well done. +rep
    CodeCall Blog | CodeCall Wiki
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Tutorial: Storing Images in MySQL with PHP
    By Jordan in forum PHP Tutorials
    Replies: 47
    Last Post: 08-13-2010, 04:42 PM
  2. C# Tutorial: Creating a report using a Date Picker
    By Parabola in forum CSharp Tutorials
    Replies: 5
    Last Post: 08-05-2009, 10:21 AM
  3. Replies: 6
    Last Post: 07-20-2009, 02:27 PM
  4. CodeCall Tutorial Contest #4
    By Jordan in forum Announcements
    Replies: 29
    Last Post: 02-25-2008, 09:25 AM
  5. John's Java Tutorial Index
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 01:05 PM