サーバーからクライアントに送信されたデータを表示しようとしています。クライアント スクリプトは Windows フォーム アプリケーションであり、サーバー クライアントから受信したデータとして表示しようとしている label1 という名前のラベルがありますが、label1 のテキストはまったく変更されません。これの理由は何ですか?以下はクライアント側のコードです。サーバー スクリプトはコンソール アプリケーションです。
Program.cs は空になり、Form1.cs は次のようになりますが、label1.text で同じエラーが発生します。
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{
GetDataFromUDP();
}
public static void SetTextForLabel(string myText)
{
label1.Text = myText;
}
private void GetDataFromUDP()
{
UdpClient subscriber = new UdpClient(8899);
IPAddress addr = IPAddress.Parse("230.0.0.1");
subscriber.JoinMulticastGroup(addr);
IPEndPoint ep = null;
for (int i = 0; i < 10; i++)
{
byte[] pdata = subscriber.Receive(ref ep);
string price = Encoding.ASCII.GetString(pdata);
//Write data to the label
SetTextForLabel(price);
}
subscriber.DropMulticastGroup(addr);
}
}
}
SetTextForLabel 内で次のエラーが表示されます。
An object reference is required for the non-static field, method, or property 'WindowsFormsApplication4.Form1.label1'
public static void SetTextForLabel(string myText)
{
label1.Text = myText;
}