locked
Get textbox value from a different thread RRS feed

  • Question

  • I have tried using Invoke and a delegate, but it isn't returning the proper value...How do you have a delegate return a value when you use invoke?

    Thanks.

    • Moved by CoolDadTx Monday, March 5, 2012 4:14 PM Winforms related (From:Visual C# General)
    Monday, March 5, 2012 4:00 PM

Answers

  • Hi,

    you can do this by declaring a return type for the delegete. Like this:

     delegate string GetText();

    then create a method to retrieve textbox text

      string GetTextBoxText()
            {
                return textBox1.Text;
            }

    then call the method using this code from a different thread:

    string textBoxText = (string) textBox1.Invoke(new GetText(GetTextBoxText));



    Bilhan silva

    • Proposed as answer by Konrad Neitzel Monday, March 5, 2012 4:11 PM
    • Marked as answer by DSabol Monday, March 5, 2012 4:16 PM
    Monday, March 5, 2012 4:10 PM

All replies

  • Could you show us your code?

    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva

    Monday, March 5, 2012 4:08 PM
  • Hi,

    you can do this by declaring a return type for the delegete. Like this:

     delegate string GetText();

    then create a method to retrieve textbox text

      string GetTextBoxText()
            {
                return textBox1.Text;
            }

    then call the method using this code from a different thread:

    string textBoxText = (string) textBox1.Invoke(new GetText(GetTextBoxText));



    Bilhan silva

    • Proposed as answer by Konrad Neitzel Monday, March 5, 2012 4:11 PM
    • Marked as answer by DSabol Monday, March 5, 2012 4:16 PM
    Monday, March 5, 2012 4:10 PM
  • Thank you.  That is basically what I was trying to use...but I guess I had somethign wrong, when I retyped it out it worked.  Thanks!
    Monday, March 5, 2012 4:17 PM
  • You're welcome DSabol.

    Bilhan silva

    Monday, March 5, 2012 4:18 PM
  • I'm glad to hear that the question is solved. 

    Have a good day!


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, March 7, 2012 2:27 AM