-1

I currently have a windows form, this contains various user controls that can be shown in a click of a button, only showing the user control which has been selected, normally this works fine with the code

Checkworkcontrol1.Show() Gradescontrol1.Hide() Submissioncontrol1.Hide()

The code above is used in my windows form to display the specific user control, but i have one exception that i'm trying to implement, inside one of the user control i have a button that will show the another user control in the windows form but when i try to implement the same method it does nothing.

Mainpage.Submissioncontrol1.Show() Mainpage.Gradescontrol1.Hide() Mainpage.Checkworkcontrol1.Hide()

"Mainpage" being the windows form

Poro_doge
  • 3
  • 4
  • Does it nothing or does it not compile? Is `Mainpage` the name of the form or ist a property referencing the form? – Olivier Jacot-Descombes Mar 11 '18 at 00:02
  • @OlivierJacot-Descombes The button that i press to display the user control in the 'Mainpage' does nothing, the program compiles on the other hand, and 'Mainpage' is the name of the windows form. – Poro_doge Mar 11 '18 at 00:09

1 Answers1

0

In the user control you will need a reference to the form. From any control on the form you can call FindForm() to retrieve the form that the control is on. Also cast it to the right form type (form name) to be able to access specific members of it.

Dim frm = DirectCast(FindForm(), Mainpage) ' Where Mainpage is the name of the form.
frm.Submissioncontrol1.Show()
frm.Gradescontrol1.Hide()
frm.Checkworkcontrol1.Hide()

On the main form these controls must be Public (or Friend) to be accessible from the user control.

Also I strongly recommend to use Option Strict On and Option Explicit On as this will reveal a lot of errors at compile time. See: What do Option Strict and Option Explicit do?.

If Mainpage is the name of the form then your code cannot work, because your form is a Class, i.e. Mainpage is a type and not the form object.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188