在运行时添加控件不起作用

时间:2011-06-23 05:32:35

标签: c#-4.0

我正在使用多个Weather API。在Googles API上,我正在加载3天的预测。这就是当前的样子: 编辑:此问题已得到解决

private void LoadForecast(WeatherSet set)
        {
            RemoveControls();

            form = new forecastView(7, 159, 1, set);
            form1 = new forecastView(161, 159, 2, set);
            form2 = new forecastView(7, 254, 3, set);

            this.Controls.Add(form);
            this.Controls.Add(form1);
            this.Controls.Add(form2);
        }




using System.Windows.Forms;
using WeatherVaneGoogleWrapper.Forecast;
using WeatherVaneGoogleWrapper.Weather;

namespace WeatherVane
{
    public partial class forecastView : UserControl
    {
        public forecastView()
        {
            InitializeComponent();
        }

        public forecastView(int x, int y, int index,WeatherSet set)
        {
            InitializeComponent();

            label7.Text = string.Format("High:{0}", set.Forecast[index].High);
            label8.Text = string.Format("Low: {0}", set.Forecast[index].Low);
            pictureBox3.Load(string.Format("http://www.google.com/{0}", set.Forecast[index].Icon));
            groupBox1.Text = set.Forecast[index].DayOfTheWeek;
            label9.Text = string.Format("Conditions: {0}", set.Forecast[index].Condition);
            this.Location = new System.Drawing.Point(x, y);
        }
    }
}

如果我说自己,这里更好,尝试它但是不能让我工作:\

    private void LoadCurrentWeatherData(string loc)
            {

                WeatherSet set = WeatherService.Response(GoogleWeatherRequest.RequestData(new WeatherService(loc)));

                LowLabelOne.Text = string.Format("Current Temp: {0}{1}", set.Current.TemperatureFahrenheit, "°");
                groupBox1.Text = string.Format("Current Conditions for {0}", set.Information.City);
                HumidityLabel.Text = set.Current.Humidity;
                windConditionsLabel.Text = string.Format("Wind Conditions: {0}", set.Current.Wind);
                label3.Text = string.Format("Condition: {0}", set.Current.Condition);
                pictureBox1.Load(string.Format("http://www.google.com/{0}", set.Current.Icon));

                LoadForecastControls(set);

                dateTimeLabel.Text = string.Format("Last Check: {0} {1}", Convert.ToDateTime(set.Information.CurrentDateTime).ToShortDateString(),
                    Convert.ToDateTime(set.Information.CurrentDateTime).ToShortTimeString());

                set = null;
            }

            private void LoadForecastControls(WeatherSet set)
            {
                RemoveControls();

                form = new forecastView(12, 136, 1, set);
                form1 = new forecastView(155, 136, 2, set);
                form2 = new forecastView(12, 218, 3, set);

                this.Controls.Add(form);
                this.Controls.Add(form1);
                this.Controls.Add(form2);
            }

            private void RemoveControls()
            {
                this.Controls.Remove(form);
                this.Controls.Remove(form1);
                this.Controls.Remove(form2);
            }

Can someone help?

1 个答案:

答案 0 :(得分:0)

我的WebForms正在变得生疏,但我不认为你可以新建UserControls并以这种方式添加它们。我相信你需要使用Page.LoadControl()

var form = Page.LoadControl(typeof(forecastView), new { 12, 136, 1, set });

Page.Controls.Add(form);
相关问题