开始加载标签时出现问题

本文关键字:问题 加载 标签 开始 | 更新日期: 2023-09-27 18:01:58

我正在编写一个程序,它只显示学校当天的代码。但是我使用标签的方式有一个问题。标签开始显示"label1",只有当我单击它时才会更改为当天的代码。有人能查出是什么问题吗?下面是代码片段:

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();
        }
    }
}

开始加载标签时出现问题

现在,在表单上创建一个文本控件,例如我的richtextbox,订阅load事件,调用转换后的函数,并享受:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.Text = (string)GetCOTD();
    }
    private object GetCOTD()
    {
        //a function for gettinthe the COTD
        string sourceString = new System.Net.WebClient().DownloadString("http://www.w3schools.com/");
        sourceString = sourceString.Substring(959, 8);
        return sourceString;
    }
}

转换器会给你奇怪的代码,因为原来的VB函数没有指定返回类型(因此它只是返回Object)。

在VB中应该是这样的:

Private Function GetCOTD() As String
    Dim sourceString As String = New System.Net.WebClient().DownloadString("http://guestwifi.discoveryschool.org.uk/cotd/?id=01234")
    Return sourceString.Substring(959, 8)
End Function
在c#:

    private string GetCOTD()
    {
        string sourceString = new System.Net.WebClient().DownloadString("http://guestwifi.discoveryschool.org.uk/cotd/?id=01234");
        return sourceString.Substring(959, 8);
    }