【问题标题】:How do i add a int parameter to my textBox1 update method?如何将 int 参数添加到我的 textBox1 更新方法?
【发布时间】:2015-10-21 01:48:13
【问题描述】:

在表格1中:

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 numberofdirsleftint varNumber,并且只使用它工作正常的字符串。 同样在不使用计数器变量的新类中。然后一切正常我看到textBox1中的文字。

例如在textBox1 我看到了:

c:\ c:\温度 c:\temp1....

但现在我想在textBox1 内的文本附近看到一个计数器:

26 c:\
25 c:\温度
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

【问题讨论】:

  • 您能否编辑您的问题以包含整个Main 方法的代码?堆栈跟踪似乎没有准确地表明 Program.cs 中存在问题的代码行是您引用的代码行。

标签: c# .net winforms


【解决方案1】:

变化:

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;
        });
    }

【讨论】:

    猜你喜欢
    • 2015-12-15
    • 2011-08-06
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多