问题描述
privatevoidbutton1_Click(objectsender,EventArgse){if(listBox1.SelectedIndex>=0){try{stringurl="http://apis.baidu.com/apistore/weatherservice/recentweathers?cityname=";//HttpWebRequest不建议使用WebClientclient=newWebClient();client.Headers.Add("apikey","7d725e55539345a4979f3bcb79bce427");Strings=client.DownloadString(url+listBox1.SelectedItem.ToString());Weatherw=deserialize<Weather>(s);listBox2.Items.Clear();listBox3.Items.Clear();listBox4.Items.Clear();if(w.errNum==0){listBox2.Items.Add(w.retData.city);listBox2.Items.Add(w.retData.cityid);}elselistBox2.Items.Add(w.errMsg);}catch(Exceptionexp){MessageBox.Show(exp.Message);}}}privatevoidbutton1_Click(objectsender,EventArgse){if(listBox1.SelectedIndex>=0){try{stringurl="http://apis.baidu.com/apistore/weatherservice/recentweathers?cityname=";//HttpWebRequest不建议使用WebClientclient=newWebClient();client.Headers.Add("apikey","7d725e55539345a4979f3bcb79bce427");Strings=client.DownloadString(url+listBox1.SelectedItem.ToString());Weatherw=deserialize<Weather>(s);listBox2.Items.Clear();listBox3.Items.Clear();listBox4.Items.Clear();if(w.errNum==0){listBox2.Items.Add(w.retData.city);listBox2.Items.Add(w.retData.cityid);}elselistBox2.Items.Add(w.errMsg);}catch(Exceptionexp){MessageBox.Show(exp.Message);}}}为什么这样打就显示valuetype“system.int32”不能为null呢如果我设一个textbox,把Strings=client.DownloadString(url+listBox1.SelectedItem.ToString());改成Strings=client.DownloadString(url+textBox1.Text);就能正常用了是为什么呢?怎么样才能取得是listbox1选中的值,小白求解...
解决方案
解决方案二:
listBox1.SelectedItem.ToString()textBox1.Text不会自己看下两个值哪里不一样
解决方案三:
引用1楼shingoscar的回复:
listBox1.SelectedItem.ToString()textBox1.Text不会自己看下两个值哪里不一样
可我试了下把listBox1.SelectedItem.ToString()用MessageBox显示了下是我列表选中的城市啊。。所以不知道为什么Textbox可以listbox不可以
解决方案四:
client.Headers.Add("apikey","7d725e55539345a4979f3bcb79bce427");MessageBox.Show((listBox1.SelectedItem.ToString()==textBox1.Text).ToString());Strings=client.DownloadString(url+listBox1.SelectedItem.ToString());
不要靠目测