问题描述
(我对C#还是很陌生)正在创建一个表单应用程序,目的是从Web API获取字符串,然后将该文本放在标签上.我已经成功地从Web上获取了数据,但是当我尝试更新标签时,我没有运气.我已经调试,发现我的类 Is 中的方法正在执行,但没有设置标签的文本.如下所示,我尝试使用this.resultLabel.text = str;
.这是课程:
(I am very new to C#) I am creating a forms application, and the purpose is to get a string from a Web API, then put that text onto a label. I have successfully gotten the data from the Web, but when I try to update the label, I have no luck.I have debugged and found that my method inside my class Is executing, but just not setting the label's text. As you can see below, I tried to use this.resultLabel.text = str;
. Here's the classes:
Program.cs(不是CS文件格式)
Program.cs (not the form cs file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using System.Net;
using System.IO;
namespace WebsiteAPITest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
class PostManager
{
public void setupClient()
{
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("https://yakovliam.com/phpApi/csTest.php"));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
string respStr;
using (Stream stream = WebResp.GetResponseStream()) //modified from your code since the using statement disposes the stream automatically when done
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
respStr = reader.ReadToEnd();
}
MessageBox.Show(respStr);
Form1 form = new Form1();
form.SetResultLabel(respStr);
}
}
}
实际表单类(Form1.cs)
Actual form class (Form1.cs)
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 WebsiteAPITest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void GetButton_Click(object sender, EventArgs e)
{
PostManager postManager = new PostManager();
postManager.setupClient();
}
public void SetResultLabel(string str)
{
this.resultLabel.Text = str;
this.resultLabel.Refresh();
}
}
标签名称证明:
推荐答案
在setupClient
内部,您调用Form1 form = new Form1()
;会创建另一个您永远不会显示的Form1
,然后在您从未显示的第二个表单中调用SetResultLabel(respStr)
,然后离开该方法并将其丢弃.
Inside setupClient
you call Form1 form = new Form1()
; that creates a second Form1
which you never display, then you call SetResultLabel(respStr)
inside this second form you never display, then you leave the method and discard it.
如果要致电呼叫表格的SetResultLabel
,则必须将呼叫表格传递给setupClient
:
If you want to call SetResultLabel
of your calling form, you have to pass the calling form to setupClient
:
public void setupClient(Form1 callingForm)
{
...
callingForm.SetResultLabel(respStr);
然后在您的Form1
内:
postManager.setupClient(this);
将表格传递给其他方法非常危险;更好的设计是让其他方法将数据返回到表单:
It's quite dangerous to pass forms to other methods; a better design is to have the other method return data to your form:
public string setupClient()
{
...
return respStr;
}
在Form1
内部:
SetResultLabel(postManager.setupClient());
这篇关于如何在C#表单中修改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!