0

Why textBox3.text do not shows value _TextBoxRequestMsg. MessageBox opens and shows _TextBoxRequestMsg value OK, console prints too.

public partial class F_Main : Form
{
    private string _TextBoxRequestMsg;

    public string TextBoxRequestMsg
    {
        get { return textBox3.Text; }
        set
        {
            _TextBoxRequestMsg = value;
            MessageBox.Show(_TextBoxRequestMsg);       
            Console.WriteLine(_TextBoxRequestMsg);    
            textBox3.Text = _TextBoxRequestMsg;         
        }
    }

    public F_Main()
    {
        InitializeComponent();
    }
}

public class CdataController : ApiController
{
    F_Main mainForm = new F_Main();

    public async Task<HttpResponseMessage> PostPayloadEventsOp(string SN, string table, string OpStamp)
    {
        using (var contentStream = await this.Request.Content.ReadAsStreamAsync())
        {
            contentStream.Seek(0, SeekOrigin.Begin);
            using (var sr = new StreamReader(contentStream))
            {
                string results = sr.ReadToEnd();
                mainForm.TextBoxRequestMsg = results;                    
            }
        }
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
        response.Content = new StringContent("OK", System.Text.Encoding.UTF8);
        response.Headers.CacheControl = new CacheControlHeaderValue()
        {
            MaxAge = TimeSpan.FromMinutes(2)
        };
        return response;
    }
}
Peter B
  • 22,460
  • 5
  • 32
  • 69
Papol
  • 1
  • 1
  • "fails" is not a sufficient description of the problem you're seeing. What *actually happens*? Can you provide a [mcve]? Is the setter being executed on the right thread? – Jon Skeet Feb 01 '23 at 12:16
  • textBox3.Text is not set and new value is not visible on the Winform – Papol Feb 01 '23 at 12:17
  • MessageBox opens and shows new value, that is works – Papol Feb 01 '23 at 12:18
  • explain more. is textbox3 accessible in your class? what do you mean exactly "fails"? – Hamid Mohammadi Feb 01 '23 at 12:21
  • Please edit the question to provide the information, instead of just using comments. (And make sure you give information about the threading aspect.) – Jon Skeet Feb 01 '23 at 12:21
  • This setter is executed inside ApiController. – Papol Feb 01 '23 at 12:25
  • 1
    `This setter is executed inside ApiController` your api knows the TextBox of an winforms ui? – nilsK Feb 01 '23 at 12:34
  • 1
    You may want to read this very thoroughly: [Hosting ASP.NET Core API in a Windows Forms Application](https://stackoverflow.com/q/60033762/1220550). It seems you want to do the same thing, and in it you'll find an Answer that explains how it can be done. Just creating a random `F_Main` object isn't going to cause it to magically appear anywhere, and neither will it magically connect to an `F_Main` instance that may already be visible. – Peter B Feb 01 '23 at 13:00
  • But where is magic that Console.WriteLine(_TextBoxRequestMsg); and MessageBox.Show(_TextBoxRequestMsg); shows value correctly ? – Papol Feb 01 '23 at 13:24
  • Well, the console is effectively a singleton, and MessageBox.Show creates a *new* message box. So in neither case are we creating a new, unseen window where the change could be effective but unobserved - which you *are* doing with the `F_Main` form. – Jon Skeet Feb 01 '23 at 13:50

1 Answers1

0

Your question states that your goal is to change textBox.Text control value in Winform and your code indicates that you want to do this by processing an HttpResponseMessage. Consider that the Form that owns the textBox3 control could await the response so that it can meaningfully process its content and assign the value to the text box.


For a minimal example, mock the API request:

public class MockCdataController : ApiController
{
    public async Task<HttpResponseMessage> MockPostPayloadEventsOp(string SN, string table, string OpStamp)
    {
        using (HttpClient client = new HttpClient())
        {
            HttpResponseMessage response = await client.GetAsync("https://stackoverflow.com/q/75310027/5438626"); 
            response.Content = new StringContent("OK", System.Text.Encoding.UTF8);
            response.Headers.CacheControl = new CacheControlHeaderValue()
            {
                MaxAge = TimeSpan.FromMinutes(2)
            };
            return response;
        }
    }
}

The Form that is in possession of textBox3 could invoke something like this:

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        buttonPost.Click += onPost;
    }
    private async void onPost(object? sender, EventArgs e)
    {
        try
        {
            UseWaitCursor = true;
            buttonPost.BackColor = Color.LightGreen;
            var response = await _controller.MockPostPayloadEventsOp("38D6FF5-F89C", "records", "Asgard");
            if((response.Headers != null) && (response.Headers.CacheControl != null))
            {
                textBox3.Text = $"{response.Headers.CacheControl.MaxAge}";
            }  
        }
        finally
        {
            UseWaitCursor = false;
            Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y);
        }
    }
    MockCdataController _controller = new MockCdataController();
}

acquire

IVSoftware
  • 5,732
  • 2
  • 12
  • 23