Note that there are some explanatory texts on larger screens.

plurals
  1. PODelegate with parameters... as a parameter
    primarykey
    data
    text
    <p>I have this method with its delegate that is used to append text to a multiline TextBox in the GUI from any of the threads in my WinForms application:</p> <pre><code>private delegate void TextAppendDelegate(TextBox txt, string text); public void TextAppend(TextBox txt, string text) { if(txt.InvokeRequired) txt.Invoke(new TextAppendDelegate(TextAppend), new object[] {txt, text }); else { if(txt.Lines.Length == 1000) { txt.SelectionStart = 0; txt.SelectionLength = txt.Text.IndexOf("\n", 0) + 1; txt.SelectedText = ""; } txt.AppendText(text + "\n"); txt.ScrollToCaret(); } } </code></pre> <p>It works great, I just call TextAppend(myTextBox1, "Hi Worldo!") from any thread and the GUI is updated. Now, is there some way to pass a delegate that invokes TextAppend to one of my utility methods in another project without sending any reference to the actual TextBox, something that might look like this from the caller:</p> <pre><code>Utilities.myUtilityMethod( new delegate(string str){ TextAppend(myTextBox1, str) }); </code></pre> <p>And in the callee, a definition similar to:</p> <pre><code>public static void myUtilityMethod(delegate del) { if(del != null) { del("Hi Worldo!"); } } </code></pre> <p>So that when this function is called, it invokes the TextAppend method with that string and the predefined TextBox the caller wants to use. Is this possible or am I crazy? I know there are way easier options like using interfaces or passing the TextBox and delegate, but I want to explore this solution because it seems more elegant and hides stuff from the callee. The problem is that I'm still too novice in C# and barely understand delegates, so please help me with the actual syntax that would work.</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload