开发者

Adding controls at runtime not working

I'm working that uses multiple Weather API's. On Googles API's I'm rtryuing to load 3 day forecasrt. This is how it current looks: EDIT: This issue has been resolved

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开发者_JAVA技巧, 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);
        }
    }
}

It's much nicer if I say myself, here how attempt it but just cannot get i tworking:\

    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?


My WebForms is getting rusty, but I don't think you can new up UserControls and add them that way. I believe you need to use Page.LoadControl().

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

Page.Controls.Add(form);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜