Passing label to method not updating label.text?
up vote
-1
down vote
favorite
I have a label in a winform, and I am trying to pass the label from a start button. Here is my code:
public void DirectLinkTask1(string DirectLinkText, string status)
{
{
Start(status);
string url = driver.Url;
LogIn(status);
while (string.IsNullOrEmpty(DirectLinkText))
{
status = "Waiting for direct link input...";
}
driver.Url = $"{DirectLinkText}";
status = "Direct Link opened, adding to cart...";
try
{
AddToCartDirectLink(DirectLinkText, status);
}
catch
{
status = "Direct Link Error";
}
}
}
Button code:
string status1 = labelStatus1.Text;
if (TaskTypeBox.Text.Contains("Keyword"))
{
worker = new BackgroundWorker();
worker.DoWork += (obj, ea) => KeywordTask1(txtKWDL1.Text, status1);
worker.RunWorkerAsync();
}
else if (TaskTypeBox.Text.Contains("DirectLink"))
{
worker = new BackgroundWorker();
worker.DoWork += (obj, ea) => DirectLinkTask1(txtKWDL1.Text, status1);
worker.RunWorkerAsync();
}
else
{
labelStatus1.Text = "Please select task type";
}
The label isn't updating at all, not sure why. I'm new to C#, and I'm sure it's a simple mistake. It will update to "Please select task type" in the else statement, but that is it. Thank you in advance for the help. please let me know if I can provide anything else to help :)
c# winforms
add a comment |
up vote
-1
down vote
favorite
I have a label in a winform, and I am trying to pass the label from a start button. Here is my code:
public void DirectLinkTask1(string DirectLinkText, string status)
{
{
Start(status);
string url = driver.Url;
LogIn(status);
while (string.IsNullOrEmpty(DirectLinkText))
{
status = "Waiting for direct link input...";
}
driver.Url = $"{DirectLinkText}";
status = "Direct Link opened, adding to cart...";
try
{
AddToCartDirectLink(DirectLinkText, status);
}
catch
{
status = "Direct Link Error";
}
}
}
Button code:
string status1 = labelStatus1.Text;
if (TaskTypeBox.Text.Contains("Keyword"))
{
worker = new BackgroundWorker();
worker.DoWork += (obj, ea) => KeywordTask1(txtKWDL1.Text, status1);
worker.RunWorkerAsync();
}
else if (TaskTypeBox.Text.Contains("DirectLink"))
{
worker = new BackgroundWorker();
worker.DoWork += (obj, ea) => DirectLinkTask1(txtKWDL1.Text, status1);
worker.RunWorkerAsync();
}
else
{
labelStatus1.Text = "Please select task type";
}
The label isn't updating at all, not sure why. I'm new to C#, and I'm sure it's a simple mistake. It will update to "Please select task type" in the else statement, but that is it. Thank you in advance for the help. please let me know if I can provide anything else to help :)
c# winforms
Looks likeelse
branch never get hit.
– Fabio
Nov 11 at 22:39
1
First issue is that when you pass the label text to the task method the argument will never change inside the task method after that, even if the label text is changed. The argument will be a snapshot of what the label said, so nothing will change it inside the while loop.
– Anders Forsgren
Nov 11 at 22:39
Is there a way to change it, so it will be able to change?
– Caden Buckelew
Nov 11 at 22:42
You could pass the label instead of its current text. But it seems like an odd design to use UI-elements for flow control at all. You should look into ”viewmodels”.
– Anders Forsgren
Nov 11 at 23:01
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have a label in a winform, and I am trying to pass the label from a start button. Here is my code:
public void DirectLinkTask1(string DirectLinkText, string status)
{
{
Start(status);
string url = driver.Url;
LogIn(status);
while (string.IsNullOrEmpty(DirectLinkText))
{
status = "Waiting for direct link input...";
}
driver.Url = $"{DirectLinkText}";
status = "Direct Link opened, adding to cart...";
try
{
AddToCartDirectLink(DirectLinkText, status);
}
catch
{
status = "Direct Link Error";
}
}
}
Button code:
string status1 = labelStatus1.Text;
if (TaskTypeBox.Text.Contains("Keyword"))
{
worker = new BackgroundWorker();
worker.DoWork += (obj, ea) => KeywordTask1(txtKWDL1.Text, status1);
worker.RunWorkerAsync();
}
else if (TaskTypeBox.Text.Contains("DirectLink"))
{
worker = new BackgroundWorker();
worker.DoWork += (obj, ea) => DirectLinkTask1(txtKWDL1.Text, status1);
worker.RunWorkerAsync();
}
else
{
labelStatus1.Text = "Please select task type";
}
The label isn't updating at all, not sure why. I'm new to C#, and I'm sure it's a simple mistake. It will update to "Please select task type" in the else statement, but that is it. Thank you in advance for the help. please let me know if I can provide anything else to help :)
c# winforms
I have a label in a winform, and I am trying to pass the label from a start button. Here is my code:
public void DirectLinkTask1(string DirectLinkText, string status)
{
{
Start(status);
string url = driver.Url;
LogIn(status);
while (string.IsNullOrEmpty(DirectLinkText))
{
status = "Waiting for direct link input...";
}
driver.Url = $"{DirectLinkText}";
status = "Direct Link opened, adding to cart...";
try
{
AddToCartDirectLink(DirectLinkText, status);
}
catch
{
status = "Direct Link Error";
}
}
}
Button code:
string status1 = labelStatus1.Text;
if (TaskTypeBox.Text.Contains("Keyword"))
{
worker = new BackgroundWorker();
worker.DoWork += (obj, ea) => KeywordTask1(txtKWDL1.Text, status1);
worker.RunWorkerAsync();
}
else if (TaskTypeBox.Text.Contains("DirectLink"))
{
worker = new BackgroundWorker();
worker.DoWork += (obj, ea) => DirectLinkTask1(txtKWDL1.Text, status1);
worker.RunWorkerAsync();
}
else
{
labelStatus1.Text = "Please select task type";
}
The label isn't updating at all, not sure why. I'm new to C#, and I'm sure it's a simple mistake. It will update to "Please select task type" in the else statement, but that is it. Thank you in advance for the help. please let me know if I can provide anything else to help :)
c# winforms
c# winforms
edited Nov 11 at 22:36
Eray Balkanli
3,85741943
3,85741943
asked Nov 11 at 22:31
Caden Buckelew
217
217
Looks likeelse
branch never get hit.
– Fabio
Nov 11 at 22:39
1
First issue is that when you pass the label text to the task method the argument will never change inside the task method after that, even if the label text is changed. The argument will be a snapshot of what the label said, so nothing will change it inside the while loop.
– Anders Forsgren
Nov 11 at 22:39
Is there a way to change it, so it will be able to change?
– Caden Buckelew
Nov 11 at 22:42
You could pass the label instead of its current text. But it seems like an odd design to use UI-elements for flow control at all. You should look into ”viewmodels”.
– Anders Forsgren
Nov 11 at 23:01
add a comment |
Looks likeelse
branch never get hit.
– Fabio
Nov 11 at 22:39
1
First issue is that when you pass the label text to the task method the argument will never change inside the task method after that, even if the label text is changed. The argument will be a snapshot of what the label said, so nothing will change it inside the while loop.
– Anders Forsgren
Nov 11 at 22:39
Is there a way to change it, so it will be able to change?
– Caden Buckelew
Nov 11 at 22:42
You could pass the label instead of its current text. But it seems like an odd design to use UI-elements for flow control at all. You should look into ”viewmodels”.
– Anders Forsgren
Nov 11 at 23:01
Looks like
else
branch never get hit.– Fabio
Nov 11 at 22:39
Looks like
else
branch never get hit.– Fabio
Nov 11 at 22:39
1
1
First issue is that when you pass the label text to the task method the argument will never change inside the task method after that, even if the label text is changed. The argument will be a snapshot of what the label said, so nothing will change it inside the while loop.
– Anders Forsgren
Nov 11 at 22:39
First issue is that when you pass the label text to the task method the argument will never change inside the task method after that, even if the label text is changed. The argument will be a snapshot of what the label said, so nothing will change it inside the while loop.
– Anders Forsgren
Nov 11 at 22:39
Is there a way to change it, so it will be able to change?
– Caden Buckelew
Nov 11 at 22:42
Is there a way to change it, so it will be able to change?
– Caden Buckelew
Nov 11 at 22:42
You could pass the label instead of its current text. But it seems like an odd design to use UI-elements for flow control at all. You should look into ”viewmodels”.
– Anders Forsgren
Nov 11 at 23:01
You could pass the label instead of its current text. But it seems like an odd design to use UI-elements for flow control at all. You should look into ”viewmodels”.
– Anders Forsgren
Nov 11 at 23:01
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
If you trying to change label text within BackgroundWorker
then you need introduce ProgressChanged
or RunWorkerCompleted
eventhandlers
So even BackgroundWorker executing on the different thread those two eventhandlers will be executed on the main thread and will have proper access to the form controls.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
If you trying to change label text within BackgroundWorker
then you need introduce ProgressChanged
or RunWorkerCompleted
eventhandlers
So even BackgroundWorker executing on the different thread those two eventhandlers will be executed on the main thread and will have proper access to the form controls.
add a comment |
up vote
0
down vote
If you trying to change label text within BackgroundWorker
then you need introduce ProgressChanged
or RunWorkerCompleted
eventhandlers
So even BackgroundWorker executing on the different thread those two eventhandlers will be executed on the main thread and will have proper access to the form controls.
add a comment |
up vote
0
down vote
up vote
0
down vote
If you trying to change label text within BackgroundWorker
then you need introduce ProgressChanged
or RunWorkerCompleted
eventhandlers
So even BackgroundWorker executing on the different thread those two eventhandlers will be executed on the main thread and will have proper access to the form controls.
If you trying to change label text within BackgroundWorker
then you need introduce ProgressChanged
or RunWorkerCompleted
eventhandlers
So even BackgroundWorker executing on the different thread those two eventhandlers will be executed on the main thread and will have proper access to the form controls.
answered Nov 11 at 23:01
Fabio
18.9k22044
18.9k22044
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253903%2fpassing-label-to-method-not-updating-label-text%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Looks like
else
branch never get hit.– Fabio
Nov 11 at 22:39
1
First issue is that when you pass the label text to the task method the argument will never change inside the task method after that, even if the label text is changed. The argument will be a snapshot of what the label said, so nothing will change it inside the while loop.
– Anders Forsgren
Nov 11 at 22:39
Is there a way to change it, so it will be able to change?
– Caden Buckelew
Nov 11 at 22:42
You could pass the label instead of its current text. But it seems like an odd design to use UI-elements for flow control at all. You should look into ”viewmodels”.
– Anders Forsgren
Nov 11 at 23:01