How to Update a WinForm GUI From a Route? #65
-
Hello, I know this is not a Grapevine problem, but how can I update a Winform GUI? [RestResource]
public class MyResource
{
[RestRoute("Get", "/api/test")]
public async Task Test(IHttpContext context)
{
await context.Response.SendResponseAsync("Successfully hit the test route!");
//UpdateGUI
}
private void UpdateGui(string text)
{
labelx.Text = text;
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You are correct, this is absolutely outside the scope of Grapevine. That being said, I'm sure many people will have this same question, so it's a good idea to address this here. Keep in mind that this is not necessarily the most optimal way to approach doing this, but rather a concise, working example to get you up and running on the right path. In order to update your WinForms GUI, you have to get a handle to the open form and the specific control you want to update on it. If, for example, you have a form named var form = Application.OpenForms["Form1"] as Form1;
var tb = form?.Controls["textBox1"] ?? null; If you try to interact with that control directly, you will get an Instead, you want to invoke a delegate: tb?.Invoke(new MethodInvoker(delegate { tb.Text = "I have updated the value in this text box"; })); That should get you headed in the right direction! |
Beta Was this translation helpful? Give feedback.
-
Thanks for tips. I have trying another method you give who work good (with v4): private void Form1_Load(object sender, EventArgs e)
{
mServer = new RestServer();
mServer.Properties.Add("CurrentForm", this);
}
private void UpdateGuil(string newText)
{
this.Invoke(new MethodInvoker(() => textBox1.Text = newText));
} and var form = context.Server.GetPropertyValueAs<Form1>("CurrentForm");
form.UpdateGui("message"); |
Beta Was this translation helpful? Give feedback.
You are correct, this is absolutely outside the scope of Grapevine. That being said, I'm sure many people will have this same question, so it's a good idea to address this here. Keep in mind that this is not necessarily the most optimal way to approach doing this, but rather a concise, working example to get you up and running on the right path.
In order to update your WinForms GUI, you have to get a handle to the open form and the specific control you want to update on it. If, for example, you have a form named
Form1
that contains a text box namedtextBox1
, and you want to update the textbox, you would importSystem.Windows.Forms
into your route class, and use something like this: