本文介绍了从activecontrol.name调用时我的自定义用户控件名称返回空.....请指教的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的自定义用户控件名称在从activecontrol.name调用时返回空.....请建议 受保护的 覆盖 函数 ProcessCmdKey( ByRef msg 作为 System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As 布尔 开 错误 GoTo my_error 如果 msg.WParam.ToInt32()= CInt (Keys.Enter)然后 如果 ActiveControl 没什么 然后 退出 功能 如果 ActiveControl.Tag<> 然后 退出 功能 如果 ActiveControl.Name = 或 ActiveControl.Name.ToUpper = dgvDetail .ToUpper 然后 SendKeys.Send( {Tab}):返回 True 结束 如果 结束 如果 返回 MyBase .ProcessC mdKey(msg,keyData) 退出 功能 my_error: MsgBox(Err.Description) 结束 功能 解决方案 属性 Control.Name 仅用于基础架构,实际上不会影响任何内容。你永远不应该依赖这个属性。由于很多原因,它可以是空字符串。只有在设计器中更改属性名称时才会修改这些名称;并为它们分配的字符串等于设计器代码生成的成员的名称。代码生成完成后,它们永远不会被使用,除非你尝试在你的代码中使用它们,你永远不应该这样做。 如果你解释你的最终目标,我可能会建议做什么。 -SA 你提到这是一个用户控制。在这种情况下,我使用两种不同的方法,我可以在将动态添加的用户控件添加到表单或其他控件后引用它。 在第一个场景中我设计了一个用户控件,我可以同时添加多个用户控件。 这里我声明它们: ' 为双视图和四视图声明其他查看器。当需要Dual或者时,需要声明才能将它们添加到 显示区域Quadview被选中。 公共 共享 imgViewer1 As 新 _ucImageViewer 公开 imgViewer2 正如 新 _ucImageViewer 公共 imgViewer3 正如 新 _ucImageViewer 公共 imgViewer4 作为 新 _ucImageViewer (同一用户控件的多个实例) ..在这里我将它们添加到表单或控件中 使用 imgViewer2 ' 为Viewer Control分配值 .Tag = 2 ' 更新标签以表明这是Viewer 1 .lblNumberTag.Text = 2 ' 禁用同步查看器,因为在Singleview我们没有其他查看器与 同步.cbSyncViewers.Enabled = True ' 禁用图像控制 .tabImageControls.Visible = 错误 ' 查看器1旁边的Poistion Viewer起始点 .Location = 新点(imgViewer1.Location.X + imgV iewer1.Width,imgViewer1.Location.Y) ' 将查看器宽度和高度(大小)设置为一半显示区域的水平尺寸 .Width = imgViewer1.Width .Height = imgViewer1.Height ' 禁用查看器工具栏选项 .tsMagnifier.Enabled = False .tsSnapshot.Enabled = False 结束 ' 添加imgViewer 2以显示 pnlMainDisplayArea.Controls.Add(imgViewer2) 好吧,这是在一个受控制的环境中,我确切地知道我将添加多少个用户控件。如果我不知道我需要多少,该怎么办? 在这种情况下,我广泛使用TAG属性。当我动态创建一个控件时,我在Tag属性中插入了唯一的信息,以便我在需要时解决该特定控件。 这里我添加了控件表格或其他控件: ' 缩略图图片框 Dim picbox 作为 PictureBox 在我的Sub或Function中我动态添加控件时执行以下操作: Dim picbox 作为 新 PictureBox picbox.Image = Image.FromStream(msThumb) ' 定位控件 如果 Left> pnlConsultationImages.Width - 138 然后 Left = 12 Top = Top + 134 结束 如果 ' 在添加控件之前准备控件 使用 picbox .Size = 新大小( 128 , 128 ) .SizeMode = PictureBoxSizeMode.Zoom .Top = Top .Left =左。可见= 真 ' 这里我们一起添加咨询ID和图像名称。将需要在#上删除字符串 .Tag = dbConID& #& clReadFromDB.tmbData.Rows(i).Item( 1 )。ToString ' 图像的文件名 结束 使用 ' 添加控件 pnlConsultationImages.Controls.Add(picbox) AddHandler picbox.Click,新 System.EventHandler( AddressOf PreviewImage) AddHandler picbox.DoubleClick,新 System.EventHandler( AddressOf AddtoSelection) AddHandler picbox.MouseEnter,新 System.Ev entHandler( AddressOf pnlImages_MouseEnter) AddHandler picbox.MouseLeave,新 System.EventHandler( AddressOf pnlImages_MouseLeave) Schweet ...现在我在我的表格上有一个动态添加的控件,但是如何解决它? ' 测试图像是否已经在选择中,如果是,则返回 对于 每个 img 作为控制在 pnlSelectedImages.Controls 如果 TypeOf img PictureBox 然后 如果 img.Tag sender.Tag 那么 返回 ' 停止添加图片,因为它已存在于所选图像中 结束 如果 结束 如果 下一步 My custom user control name return empty while calling from activecontrol.name.....please advise[EDIT - OP Code from Solution]Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean On Error GoTo my_error If msg.WParam.ToInt32() = CInt(Keys.Enter) Then If ActiveControl Is Nothing Then Exit Function If ActiveControl.Tag <> "" Then Exit Function If ActiveControl.Name = "" Or ActiveControl.Name.ToUpper = "dgvDetail".ToUpper Then SendKeys.Send("{Tab}") : Return True End If End If Return MyBase.ProcessCmdKey(msg, keyData) Exit Functionmy_error: MsgBox(Err.Description) End Function 解决方案 The property Control.Name is for infrastructure only and actually does't affect anything. You should never rely on this property. By many reasons, it can be empty string. These names are only modified when you change the property name in the designer; and they are assigned the string equal to the names of the members generated by the designer code. After the code generation is done, they are never used, unless you try to use them in your code, which you should never do.If you explain your ultimate goal, I'll probably can advise what to do instead.—SAYou mention this is a "User Control". In this case I use two different approaches in which I can reference my dynamically added user controls after they have been added to a form or another control.In the first scenario I designed a user control which I can add more than one of them at the same time.Here I declare them:'Declaring the additional viewers for Dual and Quad view. Declarations are needed to be able to add them to the 'display area when either Dual or Quadview is selected. Public Shared imgViewer1 As New _ucImageViewer Public imgViewer2 As New _ucImageViewer Public imgViewer3 As New _ucImageViewer Public imgViewer4 As New _ucImageViewer(multiple instances of the same user controls).. and here I add them to the form or controlWith imgViewer2 'Assign a value to the Viewer Control .Tag = "2" 'update label to indicate that this is Viewer 1 .lblNumberTag.Text = "2" 'disable Sync Viewer because in Singleview we have no other viewer to sync with .cbSyncViewers.Enabled = True 'disable Image Controls .tabImageControls.Visible = False 'Poistion Viewer start point inline next to Viewer 1 .Location = New Point(imgViewer1.Location.X + imgViewer1.Width, imgViewer1.Location.Y) 'set Viewer width and height (size) to half the horizontal size of Display Area .Width = imgViewer1.Width .Height = imgViewer1.Height 'disable Viewer toolbar options .tsMagnifier.Enabled = False .tsSnapshot.Enabled = False End With 'Add imgViewer 2 to display pnlMainDisplayArea.Controls.Add(imgViewer2)Alright, this is in a controlled environment where I know exactly how many user controls I will be adding. What if I don't know how many I will be needing ?In this scenario, I use the TAG property extensively. When I create a control dynamically, I insert unique information into the Tag property in order for me to address that specific control when I need to.Here I add the control to the form or another control:'Thumbnail pictureboxDim picbox As PictureBoxIn my Sub or Function I do the following when adding the control dynamically:Dim picbox As New PictureBox picbox.Image = Image.FromStream(msThumb) 'Position the control If Left > pnlConsultationImages.Width - 138 Then Left = 12 Top = Top + 134 End If 'Prepare the control before it is added With picbox .Size = New Size(128, 128) .SizeMode = PictureBoxSizeMode.Zoom .Top = Top .Left = Left .Visible = True 'Here we add the consultation ID and the Image Name together. Will need to plit the string on "#" .Tag = dbConID & "#" & clReadFromDB.tmbData.Rows(i).Item(1).ToString 'File name of the Image End With 'Add the control pnlConsultationImages.Controls.Add(picbox) AddHandler picbox.Click, New System.EventHandler(AddressOf PreviewImage) AddHandler picbox.DoubleClick, New System.EventHandler(AddressOf AddtoSelection) AddHandler picbox.MouseEnter, New System.EventHandler(AddressOf pnlImages_MouseEnter) AddHandler picbox.MouseLeave, New System.EventHandler(AddressOf pnlImages_MouseLeave)Schweet... now I have a dynamically added control somewhere on my form but how to I address it ?'test to see if image is already in the selection, if it is then Return For Each img As Control In pnlSelectedImages.Controls If TypeOf img Is PictureBox Then If img.Tag Is sender.Tag Then Return 'Stop adding the image since it already exist in the selected images End If End If Next 这篇关于从activecontrol.name调用时我的自定义用户控件名称返回空.....请指教的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 20:54