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










share|improve this question























  • 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














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










share|improve this question























  • 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












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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 22:36









Eray Balkanli

3,85741943




3,85741943










asked Nov 11 at 22:31









Caden Buckelew

217




217











  • 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
















  • 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















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












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.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 23:01









        Fabio

        19k22044




        19k22044



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線