当前位置: 首页 > 工具软件 > weather > 使用案例 >

WebServices/Weather/

冀鸿才
2023-12-01

源码地址: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

 类似资料: