Jade Neoma
Jade Neoma

Reputation: 23

Won't load label text at start of windows form

I'm making a program that just displays the Code of the day at my school. but there is a problem with the way the label I'm using loads. The label starts off displaying "label1" and only changes to the Code of the day when I click on it. Can anyone figure out what is wrong. here is a snippet of the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string GetCOTD()
        {
            //a function for getting the the COTD
            string sourceString = new System.Net.WebClient().DownloadString("http://guestwifi.discoveryschool.org.uk/cotd/?id=01234");
            sourceString = sourceString.Substring(959, 8);
            return sourceString;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = GetCOTD();
        }
        private void label1_Click(object sender, EventArgs e)
        {
            label1.Text = GetCOTD();
        }

        private void label1_Click_1(object sender, EventArgs e)
        {
            label1.Text = GetCOTD();
        }
    }
}

Upvotes: 0

Views: 1417

Answers (2)

Nikolay
Nikolay

Reputation: 12235

You probably don't have the Form1_Load handler mapped (i.e. it's just a function, in C# it's not enough to just declare it, you should also bind it to the event). Check in the form events (Properties window, Events tab), if you have Load event actually associated with Form1_Load.

Upvotes: 2

ivamax9
ivamax9

Reputation: 2629

Try to put label1.Text = GetCOTD(); after InitializeComponent(); , it should help.

Upvotes: 3

Related Questions