C# 在开始时加载labvel时出现问题

C# 在开始时加载labvel时出现问题,c#,logic,C#,Logic,我正在制作一个程序,只显示我学校当天的代码。但是我使用标签的方式有一个问题。标签开始显示“label1”,当我点击它时,它只会更改当天的代码。有人能找出什么地方不对吗。以下是代码片段: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; u

我正在制作一个程序,只显示我学校当天的代码。但是我使用标签的方式有一个问题。标签开始显示“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();
        }
    }
}

在表单上,您可以创建一个文本控件,如my richtextbox,订阅加载事件,调用转换后的函数,然后享受:

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函数没有指定返回类型(因此它只是返回对象)

下面是它在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);
    }