添加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();
//城市说明及图片