我正在制作一个程序,只显示我学校当天的代码。但我使用标签的方式存在问题。标签开始显示" 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();
}
}
}
答案 0 :(得分:0)
在表单中,您可以创建一个文本控件,例如我的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;
}
}
希望这有帮助。
答案 1 :(得分:0)
转换器将为您提供时髦的代码,因为原始的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);
}