Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I invoke a control with parameters? I've googled this up, but nowhere to find!

invoke ui thread

This is the error i get:

Additional information: Parameter count mismatch.

And this happens when i do a simple check whether the text property of a textbox control is empty or not. This works in WinForms:

if (this.textboxlink.Text == string.Empty)
   SleepThreadThatIsntNavigating(5000);

It jumps from this if the line to the catch block and shows me that message.

This is how i try to invoke the control:

// the delegate:
private delegate void TBXTextChanger(string text);

private void WriteToTextBox(string text)
{
    if (this.textboxlink.Dispatcher.CheckAccess())
    {
        this.textboxlink.Text = text;
    }
    else
    {
        this.textboxlink.Dispatcher.Invoke(
            System.Windows.Threading.DispatcherPriority.Normal,
            new TBXTextChanger(this.WriteToTextBox));
    }
}

What am I doing wrong? And since when do i have to invoke a control when i just want to read its content?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
280 views
Welcome To Ask or Share your Answers For Others

1 Answer

When you call Invoke, you're not specifying your argument (text). When the Dispatcher tries to run your method, it doesn't have a parameter to supply, and you get an exception.

Try:

this.textboxlink.Dispatcher.Invoke(
     System.Windows.Threading.DispatcherPriority.Normal,
     new TBXTextChanger(this.WriteToTextBox), text);

If you want to read the value from a text box, one option is to use a lambda:

string textBoxValue = string.Empty;

this.textboxlink.Dispatcher.Invoke(DispatcherPriority.Normal, 
     new Action( () => { textBoxValue = this.textboxlink.Text; } ));

if (textBoxValue == string.Empty)
    Thread.Sleep(5000);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...