【问题标题】:How to modify label text in C# forms如何在 C# 表单中修改标签文本
【发布时间】:2019-07-31 22:45:41
【问题描述】:

(我对 C# 非常陌生)我正在创建一个表单应用程序,其目的是从 Web API 获取一个字符串,然后将该文本放到一个标签上。我已经成功地从网上获取了数据,但是当我尝试更新标签时,我没有运气。 我已经调试并发现我的类中的方法 Is 正在执行,但只是没有设置标签的文本。正如您在下面看到的,我尝试使用this.resultLabel.text = str;。这是课程:

Program.cs(不是表单 cs 文件)

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)

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

标签名称证明:

【问题讨论】:

  • 通常只设置文本就足够了。重绘将自动发生。我唯一的猜测是“resultLabel”不是您要查找的标签?或者,也许你只是把文字弄小了?作为设计师的初学者,添加或修改错误的元素或设置错误的值很容易。
  • SetResultLabel 的调用在哪里?是否存在与多线程相关的问题? Jacob 可能是对的,而您正试图设置错误的标签。发布您的 Form1.Designer.cs
  • 很抱歉给您带来了困惑。在我阅读你的评论之前,我正要发布它。我忘了提到我正在创建该类的另一个实例(它是一个实例吗?我使用 Python 和 Java,我称之为)(Form1 form = new Form1();)。
  • 我很确定我设置了正确的标签。在我的表单设计器中,我将其命名为 resultLabel。文本是N/A,但在请求之后我试图将其更新为结果。 imgur.com/a/c2m5amN
  • Application.Run(new Form1()); 中向用户显示的表单与您在 setupClient 中使用的表单不​​同 form.SetResultLabel(respStr); 您应该让 setupClient 接受表单作为参数,然后调用时传入this

标签: c# winforms label


【解决方案1】:

setupClient 内部你调用Form1 form = new Form1();这会创建第二个您从不显示的Form1,然后在您从不显示的第二个表单中调用SetResultLabel(respStr),然后您离开该方法并丢弃它。

如果你想调用你的调用表单的SetResultLabel,你必须将调用表单传递给setupClient

public void setupClient(Form1 callingForm)
{
    ...
    callingForm.SetResultLabel(respStr);

然后在你的Form1

postManager.setupClient(this);

将表单传递给其他方法是相当危险的;更好的设计是让其他方法将数据返回到您的表单:

public string setupClient()
{
    ...
    return respStr;
}

Form1里面:

SetResultLabel(postManager.setupClient());

【讨论】:

    猜你喜欢
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    相关资源
    最近更新 更多