WebServices/Weather/

源码地址:http://download.csdn.net/detail/ericwuhk/9719157


 ///   <summary>   
 ///   调用webservice接口获取城市天气情况      
 ///   </summary>     


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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public WeatherWS.WeatherWS ws;
        public Form1()
        {
            InitializeComponent();
            ws = new WeatherWS.WeatherWS();
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            DataSet ds = ws.getRegionDataset();          
            DataTable  dt=ds.Tables[0];
            String str_temp;
            this.comboBox1.Items.Clear();
            //for (int i = dt.Rows.Count-1; i >=0; i--)
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                str_temp=dt.Rows[i][0].ToString() + dt.Rows[i][1].ToString();
                this.comboBox1.Items.Add(str_temp);                
            }
            this.comboBox1.SelectedIndex=0;             
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String provinceCode=GetNumbers(this.comboBox1.Text);
            DataSet ds = ws.getSupportCityDataset(provinceCode);
            DataTable dt = ds.Tables[0];
            String str_temp;
            this.comboBox2.Items.Clear();
            this.listBox1.Items.Clear();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                str_temp = dt.Rows[i][0].ToString() + dt.Rows[i][1].ToString();
                //listBox1.Items.Insert(0, str_temp);
                listBox1.Items.Add(str_temp);
                this.comboBox2.Items.Add(str_temp);               
            }

            this.comboBox2.SelectedIndex = 0;
            //this.comboBox2.SelectedIndex = dt.Rows.Count-1;

        }


        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            String theCityID = GetNumbers(this.comboBox2.Text);
            string[] str;
            string str_temp;
            str = ws.getWeather(theCityID, "");
            listBox2.Items.Clear();
            //for (int i = 0; i < str.Length; i++)
            for (int i = str.Length - 1; i >= 0; i--)
            {
                str_temp = str[i];
                listBox2.Items.Insert(0, str_temp);
            }
        }


        ///   <summary>   
        ///   从字符串中提取所有数字   
        ///     Returns:所有数字   
        ///   </summary>     
        ///   <param   name   =   "p_str">   需要提取的字符串   </param>   
        ///   <returns>   所有数字   </returns>  
        ///   
        public static string GetNumbers(string p_str)
        {
            string strReturn = string.Empty;
            if (p_str == null || p_str.Trim() == "")
            {
                strReturn = "";
            }

            foreach (char chrTemp in p_str)
            {
                if (!Char.IsNumber(chrTemp))
                {
                    strReturn += chrTemp.ToString();
                }
            }
            return strReturn;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.comboBox2.SelectedIndex=this.listBox1.SelectedIndex;
        }

    }
}

参考网址:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值