天气预报的web服务的使用

来源:互联网 发布:editplus软件汉化包 编辑:程序博客网 时间:2024/05/18 05:36

添加Web引用:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

重命名为:weather

 public string[] GetWeather(string city)
       {
           weather.WeatherWebService webweather = newcheshi.weather.WeatherWebService();//实例化天气的服务
           string[] wcity = webweather.getWeatherbyCityName(city);
           return wcity;
       }

       private void weathers_Shown(object sender, EventArgs e)
       {
           try
           {
               weather.WeatherWebService wweather = newcheshi.weather.WeatherWebService();
               string[] area = wweather.getSupportProvince();
               int mount = area.Length - 1;
               comboBox1.Items.Clear();
               for (int im = 0; im < mount; im++)
               {
                   comboBox1.Items.Add(area[im].ToString());

               }
               comboBox1.SelectedIndex = 0;
           }
           catch { }
       }

       private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
       {
           try
           {
               weather.WeatherWebService weweather = newcheshi.weather.WeatherWebService();
               string[] mcity = weweather.getSupportCity(comboBox1.Text);
               int mcount = mcity.Length - 1;
               comboBox2.Items.Clear();
               for (int i = 0; i < mcount; i++)
               {
                   comboBox2.Items.Add(mcity[i].Remove(mcity[i].IndexOf("(")));
               }
               comboBox2.SelectedIndex = 0;
           }
           catch { }
       }

       private void comboBox2_SelectedIndexChanged(object sender,EventArgs e)
       {
           try
           {
               string[] cityweather =GetWeather(comboBox2.Items[comboBox2.SelectedIndex].ToString());
               label1.Text = cityweather[0].ToString();
               label2.Text = cityweather[1].ToString();
               label3.Text = cityweather[10].ToString();
               //今天
               pictureBox1.ImageLocation =@"images/weather/"+cityweather[8].ToString();
               pictureBox2.ImageLocation = @"images/weather/" +cityweather[9].ToString();
               label4.Text = cityweather[6].ToString() + cityweather[5].ToString()+ cityweather[7].ToString();
               label5.Text = cityweather[11].ToString();
               //明天
               label6.Text = cityweather[13].ToString() +cityweather[12].ToString() + cityweather[14].ToString();
               //后天
               label7.Text = cityweather[18].ToString() +cityweather[17].ToString() + cityweather[19].ToString();
               //城市说明及图片
               pictureBox3.ImageLocation = @"http://www.cma.gov.cn/tqyb/img/city/"+ cityweather[3].ToString();
               label8.Text = cityweather[22].ToString();
               //预计时间
               label9.Text = "预报时间:" + cityweather[4].ToString();
           }
           catch { }
       }

原创粉丝点击