我如何添加一个int参数到我的textBox1更新方法
本文关键字:参数 我的 textBox1 新方法 更新 int 何添加 添加 一个 | 更新日期: 2023-09-27 18:18:39
In form1:
public MainWindow()
{
InitializeComponent();
Searcher searcher = new Searcher(this);
}
private delegate void NameCallBack(string varText, int varNumber);
public void UpdateTextBox(string input, int numberofdirsleft)
{
if (InvokeRequired)
{
textBox1.BeginInvoke(new NameCallBack(UpdateTextBox), new object[] { input });
}
else
{
textBox1.Text = input;
}
}
在新类中:
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 varNumber
并仅使用字符串,则工作正常。同样在新类中不使用计数器变量。然后一切正常,我看到textBox1
中的文本。
例如在textBox1
我看到:
c: 'c: ' tempc: ' temp1…
但是现在我想在textBox1
内的文本附近看到一个计数器:
26 c: '
25 c: ' temp
24 c:'temp1
但是我现在用我添加的int变量的方式它在program。cs中抛出了一个异常:
Application.Run(new MainWindow());
参数计数不匹配
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
中首先看到的变量计数器是26然后在foreach
中,我看到使用一个断点,计数器为1那么下一次在foreach
计数器中的时间是26
变化:
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;
}
}
注意我是如何将这两个参数传递给对象数组的:
new object[] { input, numberofdirsleft }
然后在更新文本框值时使用这两个值。
你也可以这样做:
public void UpdateTextBox(string input, int numberofdirsleft)
{
this.BeginInvoke((MethodInvoker)delegate {
this.textBox1.Text = numberofdirsleft.ToString() + " " + input;
});
}