1
votes

My code requires to get the control name in user control Load event, problem is i get the control name in assembly, not the name i assign in the form, but if i run the project i get the control name properly.

To test, just add following code to any user control

Private Sub GridX_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MsgBox(Me.Name)
End Sub

At runtime this gives ControlName1

At design time this gives ControlName

how can I get the ControlNameX at runtime?


To make it more clear, add two controls in the form, you will see that name of the control in design mode are the same.. unlike in runtime mode..

1
I run the code to get the Me.Name value, and it shows my name of my form. But, you may be looking for the text on the form border, which is Me.Text.Kat
are you confusing the control.Name with the object reference (ie the variable name used in code)?Ňɏssa Pøngjǣrdenlarp
I see the same behavior as hassan. @sparkysword: Probably you have the Load event of the form implemented; you need to implement the Load event of the user control. The name of the control (in the designer) shows up before I get an opportunity to specify the name. I think it is not possible.Sjips
Ah, I see what you're saying @Sjips.Kat
Thanks all, to make it more clear, add two controls in the form, you will see that name of the control in design mode are the same.. unlike in runtime mode...hassan

1 Answers

1
votes

If you add a property to your user control like this:

// Note: code in C#
public string ControlName
{
    get
    {
        return Name;
    }
    set
    {
        Name = value;
        MessageBox.Show(value);
    }
}

You compile it and you add it to the form, you have a ControlName property in your designer. If you change the name in the designer, the new name shows up. Instead of the MessageBox.Show() you want to call an eventhandler, so you can do something with the new name.