0

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

        }


    }

}

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

proof of label name: Img of label

9
  • It normally is enough to just set the text. The redraw will happen Automagically. My only guess is that the "resultLabel" is not the label you are looking for? Or maybe you just made it way to small for the text? As a beginner with the designer it can be easy to add or modify the wrong element or set faulty values. Commented Jul 31, 2019 at 22:51
  • Where is the call to SetResultLabel? Are there multithread-related issues? Jacob is probably right and you are trying to set the wrong label. Post your Form1.Designer.cs Commented Jul 31, 2019 at 23:04
  • Sorry about the confusion. I was about to post it anyway before I read your comment. I forgot to mention that I am creating another instance of the class (is it an instance? I work with Python & Java, I call it that) (Form1 form = new Form1();). Commented Jul 31, 2019 at 23:06
  • I am pretty sure I'm setting the right label. In my form designer I titled it resultLabel. The text is N/A, but after the request I am trying to update it to the result. imgur.com/a/c2m5amN Commented Jul 31, 2019 at 23:09
  • 1
    The form being shown to the user in Application.Run(new Form1()); is not the same as the one you're using in setupClient when you do form.SetResultLabel(respStr); You should probably have setupClient accept a form as a parameter, and then pass in this when you call it. Commented Jul 31, 2019 at 23:10

1 Answer 1

1

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.

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

Then inside your 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;
}

And inside Form1:

SetResultLabel(postManager.setupClient());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.