如何在textBox1更新方法中添加int参数?

时间:2015-10-21 01:48:13

标签: c# .net winforms

在form1中:

static MainWindow myform;

public Searcher(MainWindow form)
{
    myform = form;
}

在新课堂上:

foreach

if (m_pars.IncludeSubDirsChecked) { DirectoryInfo[] subDirInfos = dirInfo.GetDirectories(); int counter = subDirInfos.Length; foreach (DirectoryInfo subDirInfo in subDirInfos) { if (m_stop) { break; } // Recursion: SearchDirectory(subDirInfo); counter = counter - 1; myform.UpdateTextBox(subDirInfo.FullName,counter); } } 中的新课程:

form1

如果在int numberofdirsleft我正在移除int varNumbertextBox1并仅使用字符串,那么它正常工作。 另外在新类中不使用计数器变量。然后一切正常我看到textBox1中的文字。

例如在textBox1我看到:

C:\ C:\ TEMP C:\ ...的temp1

但现在我想在Application.Run(new MainWindow());

内的文字附近看到一个计数器
  

26 c:\
  25 c:\ temp
  24 c:\ temp1

但是我现在使用int变量的方式我添加了它在Program.cs中抛出异常:

System.Reflection.TargetParameterCountException was unhandled
      IsTransient=false
      Message=Parameter count mismatch.
      Source=mscorlib
      StackTrace:
           at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
           at System.Delegate.DynamicInvokeImpl(Object[] args)
           at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
           at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
           at System.Threading.ExecutionContext.runTryCode(Object userData)
           at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
           at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at FileSearcher.Program.Main() in e:\filesearch\FileSearcher\FileSearcher\Program.cs:line 17
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

参数计数不匹配

foreach

我在foreach首先看到的变量计数器是26 然后在foreach里面,我看到使用一个断点,计数器是1 然后下一次user_id2计数器是26

1 个答案:

答案 0 :(得分:0)

变化:

public void UpdateTextBox(string input, int numberofdirsleft)
{
    if (InvokeRequired)
    {
        textBox1.BeginInvoke(new NameCallBack(UpdateTextBox), new object[] { input });
    }
    else
    {
        textBox1.Text = input;
    }
}

要:

public void UpdateTextBox(string input, int numberofdirsleft)
{
    if (InvokeRequired)
    {
        textBox1.BeginInvoke(new NameCallBack(UpdateTextBox), new object[] { input, numberofdirsleft });
    }
    else
    {
        textBox1.Text = numberofdirsleft.ToString() + " " + input;
    }
}

注意我是如何将两个参数传递给Object Array的:

new object[] { input, numberofdirsleft }

然后在更新Textbox值时使用这两个值。

您也可以这样做:

    public void UpdateTextBox(string input, int numberofdirsleft)
    {
        this.BeginInvoke((MethodInvoker)delegate {
            this.textBox1.Text = numberofdirsleft.ToString() + " " + input;
        });
    }