Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
My custom user control name return empty while calling from activecontrol.name.....please advise

[EDIT - OP Code from Solution]
VB
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 Function
my_error:
        MsgBox(Err.Description)
    End Function
Posted
Updated 15-Jul-14 2:55am
v2
Comments
Maciej Los 14-Jul-14 17:53pm    
Are we forecasters?
Please, be more specific and provide more details. Sample code would be grateful (this part which isn't works).
Sergey Alexandrovich Kryukov 14-Jul-14 18:08pm    
The property simply should not be used. Please see my answer.
—SA

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.

—SA
 
Share this answer
 
v3
Comments
Maciej Los 14-Jul-14 18:12pm    
Reference link would be very helpful (if it is possible).
+5!
Sergey Alexandrovich Kryukov 14-Jul-14 18:23pm    
Thank you, Maciej.

Unfortunately, MSDN does not provide sufficient explanation of this property; see, for example, http://msdn.microsoft.com/en-us/library/system.windows.forms.control.name%28v=vs.110%29.aspx.

There is no usual "Infrastructure" comment. You know, some library features are underdocumented in Microsoft, fortunately, not too many. I know that this "Name" property confused a number of the beginners, by the class "PictureBox" and the term "overriding" confused a lot more.

—SA
Maciej Los 15-Jul-14 1:43am    
Thank you for explanation, Sergey.
By The Way: your answer helped me to realize that there is a way to return "the name" of control. See my answer. I hope, i'm not wrong.
Sergey Alexandrovich Kryukov 15-Jul-14 11:07am    
OP posted an "answer" and fully confirmed my assumption: this is nothing but a pure abuse, pretty bad one. Remember, I wrote about the trend to use string representing data instead data itself? OP's attempt is exactly that, as bad as it can be.
—SA
CHill60 15-Jul-14 8:54am    
Good answer 5'd. By the way the OP is attempting to respond to you via solution 3 - I'm about to update his post with his code
You 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:
VB
'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 control
VB
With 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:

VB
'Thumbnail picturebox
Dim picbox As PictureBox


In my Sub or Function I do the following when adding the control dynamically:
VB
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 ?
VB
'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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900