Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 刷新循环Winforms中的几个控件_C# - Fatal编程技术网

C# 刷新循环Winforms中的几个控件

C# 刷新循环Winforms中的几个控件,c#,C#,我想申请天气预报。当我在组合框中输入新城市时,我的标签和图片框不会刷新。我已经试过了Refresh()Update()和Invalidate(),但都没有成功。请告诉我,我该怎么办。提前谢谢你 private async void SetWeatherForecastDataToWeatherApp(string city) { try { var jsonData = string.Empty; var

我想申请天气预报。当我在组合框中输入新城市时,我的标签和图片框不会刷新。我已经试过了
Refresh()
Update()
Invalidate()
,但都没有成功。请告诉我,我该怎么办。提前谢谢你

 private async void SetWeatherForecastDataToWeatherApp(string city)
    {
        try
        {
            var jsonData = string.Empty;
            var url = string.Format("http://api.openweathermap.org/data/2.5/forecast?q={0}&APPID=a54961a05f7a1fc0cf9bd2bf1465dea5", city);
            var uri = new Uri(url);
            var request = WebRequest.Create(uri);
            var response = await request.GetResponseAsync();

            using (var stream = response.GetResponseStream())
            {
                using (var streamReader = new StreamReader(stream))
                {
                    jsonData = await streamReader.ReadToEndAsync();
                }
            }
            response.Close();

            _jsonFutureWeatherForecastData = jsonData;

            _weatherForecast = JsonConvert.DeserializeObject<WeatherForecast>(_jsonFutureWeatherForecastData);
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message, "Input Error", MessageBoxButtons.OK);
        }


        var dayNameLabelList = new List<Label>();
        var weatherDescriptionLablList = new List<Label>();
        var gradesLabel = new List<Label>();

        var pictureBoxList = new List<PictureBox>();

        int yLocation = 50;
        int xLocation = Width / 2;
        int cnt = 0;


        string currDayOfWeek = string.Empty;
        string CurrentDate = string.Empty;

        for (int i = 0; i < 9; i++)
        {

            currDayOfWeek = _dateConverter.ConvertMilisecondsToCurrentTime(_weatherForecast.WeatherList[i].CurrentDate).DayOfWeek.ToString();
            CurrentDate = _dateConverter.ConvertMilisecondsToCurrentTime(_weatherForecast.WeatherList[i].CurrentDate).Day.ToString();

            cnt++;
            pictureBoxList.Add(new PictureBox());
            pictureBoxList[i].Name = "WeatherForecastImage" + cnt.ToString();
            pictureBoxList[i].Location = new Point(xLocation, yLocation);
            pictureBoxList[i].Load($"Icons/{_weatherForecast.WeatherList[i].Weather[0].Icon}.png");
            Controls.Add(pictureBoxList[i]);
            pictureBoxList[i].Invalidate();

            dayNameLabelList.Add(new Label());
            dayNameLabelList[i].Text = currDayOfWeek + " " + CurrentDate;
            dayNameLabelList[i].Location = new Point(xLocation + 100, yLocation);
            dayNameLabelList[i].Size = new Size(100, 15);
            dayNameLabelList[i].Font = new Font("Lucida Sans", 10, FontStyle.Regular);
            Controls.Add(dayNameLabelList[i]);

            weatherDescriptionLablList.Add(new Label());
            weatherDescriptionLablList[i].Text = _weatherForecast.WeatherList[i].Weather[0].Description;
            weatherDescriptionLablList[i].Location = new Point(xLocation + 100, yLocation + 15);
            weatherDescriptionLablList[i].Font = new Font("Lucida Sans", 8, FontStyle.Regular);
            Controls.Add(weatherDescriptionLablList[i]);

            gradesLabel.Add(new Label());
            gradesLabel[i].Text = _weatherForecast.WeatherList[i].Main.Temperature.ToString("0") + " C°";
            gradesLabel[i].Location = new Point(xLocation + 200, yLocation);
            gradesLabel[i].Font = new Font("Lucida Sans", 10, FontStyle.Regular);
            Controls.Add(gradesLabel[i]);

            yLocation += 100;
        }

        for (int i = 0; i < dayNameLabelList.Count; i++)
        {
            dayNameLabelList[i].ForeColor = Color.White;
            weatherDescriptionLablList[i].ForeColor = Color.White;
            gradesLabel[i].ForeColor = Color.White;
        }
    }
private async void setWeatherForecastDataTowerAtherApp(字符串城市)
{
尝试
{
var jsonData=string.Empty;
var url=string.Format(“http://api.openweathermap.org/data/2.5/forecast?q={0}&APPID=a54961a05f7a1fc0cf9bd2bf1465dea5”,城市);
var uri=新的uri(url);
var request=WebRequest.Create(uri);
var response=wait request.GetResponseAsync();
使用(var stream=response.GetResponseStream())
{
使用(var streamReader=新streamReader(stream))
{
jsonData=wait streamReader.ReadToEndAsync();
}
}
response.Close();
_jsonFutureWeatherForecastData=jsonData;
_weatherForecast=JsonConvert.DeserializeObject(\u jsonFutureWeatherForecastData);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message,“输入错误”,MessageBox按钮.OK);
}
var dayNameLabelList=新列表();
var weatherDescriptionLabList=新列表();
var gradesLabel=新列表();
var pictureBoxList=新列表();
内部位置=50;
int xLocation=宽度/2;
int-cnt=0;
string currDayOfWeek=string.Empty;
string CurrentDate=string.Empty;
对于(int i=0;i<9;i++)
{
CurrdDayOfWeek=_dateConverter.ConvertMilisSecondsToCurrentTime(_weatherForecast.WeatherList[i].CurrentDayOfWeek.ToString();
CurrentDate=_dateConverter.ConvertMilisSecondsToCurrentTime(_weatherForecast.WeatherList[i].CurrentDate).Day.ToString();
cnt++;
添加(新的PictureBox());
pictureBoxList[i].Name=“WeatherForecastImage”+cnt.ToString();
pictureBoxList[i]。位置=新点(xLocation,yLocation);
pictureBoxList[i].Load($”图标/{{uWeatherForecast.WeatherList[i].Weather[0].Icon}.png”);
控件。添加(pictureBoxList[i]);
pictureBoxList[i].Invalidate();
添加(新标签());
dayNameLabelList[i]。Text=currDayOfWeek+“”+CurrentDate;
dayNameLabelList[i]。位置=新点(X位置+100,Y位置);
dayNameLabelList[i].大小=新大小(100,15);
dayNameLabelList[i].Font=新字体(“Lucida Sans”,10,FontStyle.Regular);
Add(dayNameLabelList[i]);
WeatherDescriptionLabList.Add(新标签());
WeatherDescriptionLabList[i]。Text=\u weatherForecast.WeatherList[i]。天气[0]。说明;
WeatherDescriptionLabList[i]。位置=新点(X位置+100,Y位置+15);
WeatherDescriptionLabList[i].Font=新字体(“Lucida Sans”,8,FontStyle.Regular);
添加(WeatherDescriptionLabList[i]);
gradesLabel.Add(新标签());
gradesLabel[i].Text=_weatherForecast.WeatherList[i].Main.Temperature.ToString(“0”)+“C°”;
等级标签[i]。位置=新点(X位置+200,Y位置);
gradesLabel[i].Font=新字体(“Lucida Sans”,10,FontStyle.Regular);
添加(gradesLabel[i]);
阳离子+=100;
}
for(int i=0;i
您需要为SelectedIndexChangedEvent(如果它是不可编辑的组合)或TextUpdateEvent(如果它是不可编辑的组合)的组合添加事件处理程序(在后一种情况下,很可能两者都是)。然后,这些事件处理程序会根据需要更改其他控件。

我建议您创建一个UserControl,而不是所有这些单独的控件。我还建议你最终阅读并采取行动,你希望他们什么时候更新?它们是在循环中添加的还是在方法完成后添加的?@Enigmative我尝试在循环中更新方法,还是在方法完成后更新方法,但没有成功。