Search code examples
c#labelclickpanel

Can you show the same label twice on different forms within a program?


I am creating a program that shows a list of Spanish verbs, and when clicked, shows a new panel with the verb conjugations. I'm using labels with click events. I want to have a panel that shows the 200 most common verbs, and another that shows, for instance, verbs that begin with the letter a. The panels wouldn't be shown at the same time. My question is, is there a way to show the same label with all of it's properties on more than one panel without having to create multiple labels with the exact same properties that when clicked, show the exact same conjugation panel.

For instance, a drop-down that has "200 most common", "Verbs that begin with A", "Verbs that begin with B", etc. A list would be shown in a panel that corresponds to the option clicked. If another option is clicked, that panel is hidden and another panel is shown with a list that likely would include labels that were also shown in the first panel.

I imagine I could define a class for each verb and call that to show on multiple panels. But it has been 10 years since I wrote code and I simple don't remember enough about creating classes to do that. I'm pretty much starting over with my knowledge.

I haven't come up with any solutions to try.


Solution

  • The same control can be displayed on different forms but not at the same time. You could set the Parent of the Label to move it from one container to another, on the same form or a different form, and then set the Parent again to move it back. Just note that the Location property will not change so the position of the Label in its new container will be the same as its position in its old container unless you change it. This may or may not be desirable.

    Having said that, moving the control around in this situation would be crazy. A Label simply displays whatever string you assign to its Text property. Why move the whole Label when you can simply get the string you assigned to the Text of one Label and assign it to the Text of the other? Moving data between forms is pretty fundamental to form-based applications so you shouldn't try to avoid doing it now because you're simply kicking the can down the road and not very far either. Besides, you'd have to pass the Label itself between forms so, if you can do that, why can't you pass a string?