获取动态UserControl标记名称VB.NET

时间:2018-06-02 22:57:20

标签: .net vb.net user-controls

问题:我试图通过创建控件内的按钮获取动态创建的用户控件的标记名称。screenshot to understand

我尝试了以下代码来获取标记名称:

dbplyr

以下用于创建动态控件的代码:

Private Sub DownloadViewDelClick(sender As Object, e As EventArgs)
    Dim i As Integer = downloadContainer.Controls.Count - 1
    While i >= 0
        If downloadContainer.Controls(i).Tag = sender Then
            MsgBox(downloadContainer.Controls(i).Tag)
        End If
        i -= 1
    End While
End Sub

当我运行应用程序并单击任何动态控件上的垃圾桶按钮时,我收到以下错误:

 Sub create_download_list()
    Dim counter As Integer = 0
    Dim downloadview As New videoviewcontrol
    For Each link As String In downloadList
        AddHandler downloadview.btnDel.Click, AddressOf Me.DownloadViewDelClick
        downloadview.Tag = "downloadview" & counter
        downloadview.TextBox1.Text = link
        downloadContainer.Controls.Add(downloadview)
        counter += 1
    Next
End Sub

以下是' locals'中的$ exception值。输出跟踪窗口中的选项卡:

 System.InvalidCastException
  HResult=0x80004002
  Message=Overload resolution failed because no Public '=' can be called with these arguments:
    'Public Shared Operator =(a As String, b As String) As Boolean':
        Argument matching parameter 'b' cannot convert from 'Button' to 'String'.
  Source=Microsoft.VisualBasic
  StackTrace:
   at Microsoft.VisualBasic.CompilerServices.OverloadResolution.ResolveOverloadedCall(String MethodName, List`1 Candidates, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, BindingFlags LookupFlags, Boolean ReportErrors, ResolutionFailure& Failure)
   at Microsoft.VisualBasic.CompilerServices.Operators.ResolveUserDefinedOperator(UserDefinedOperator Op, Object[] Arguments, Boolean ReportErrors)
   at Microsoft.VisualBasic.CompilerServices.Operators.InvokeObjectUserDefinedOperator(UserDefinedOperator Op, Object[] Arguments)
   at Microsoft.VisualBasic.CompilerServices.Operators.InvokeUserDefinedOperator(UserDefinedOperator Op, Object[] Arguments)
   at Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(Object Left, Object Right, Boolean TextCompare)
   at Bhavesh_YouTube_Downloader.mainWindow.DownloadViewDelClick(Object sender, EventArgs e) in C:\Users\TP043287\source\repos\Bhavesh YouTube Downloader\Bhavesh YouTube Downloader\mainWindow.vb:line 28
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(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(IntPtr 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 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at Bhavesh_YouTube_Downloader.My.MyApplication.Main(String[] Args) in :line 81

我试图理解这个问题,但上面的例外值是“无法转换”按钮' b'字符串,我没有任何名为' b'任何地方。 我很感激解决方案。对不起这个菜鸟问题..谢谢!

0 个答案:

没有答案