简体   繁体   English

getter setter 无法在 Winform 中更改 textBox.Text 控件值 c#

[英]getter setter can't change textBox.Text control value in Winform c#

Why textBox3.text do not shows value _TextBoxRequestMsg.为什么 textBox3.text 不显示值 _TextBoxRequestMsg。 MessageBox opens and shows _TextBoxRequestMsg value OK, console prints too. MessageBox 打开并显示 _TextBoxRequestMsg 值 OK,控制台也打印。

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

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 .您的问题表明您的目标是在 Winform 中更改 textBox.Text 控件值,并且您的代码表明您希望通过处理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.考虑拥有textBox3控件的Form可以等待响应,以便它可以有意义地处理其内容并将值分配给文本框。


For a minimal example, mock the API request:举一个最小的例子,模拟 API 请求:

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:拥有textBox3Form可以调用如下内容:

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

获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM