-1

Is there any method that can access or control a panel or any like label etc. in a different form?

For example :

Form1

panel.visible = true

Form2

Form1.panel.visible = false 
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 3
    Possible duplicate of [How to access a form control for another form?](https://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form) (and half a dozen others) – ADyson Nov 04 '19 at 13:32
  • You can achieve this without difficulty, by either exposing as `public` the control, or you could use a property to access it. **However**, access of controls in another form is usually considered bad practice. There are much more elegant ways of sharing information between forms – Martin Nov 04 '19 at 13:33
  • 1
    Forms are classes. When you need to set an internal value of a class object, you expose public properties or methods (overridable, if required) that allow other classes to alter these values. In a *managed* way (it's the class that exposes the public properties/methods that actually perform the change). Try to avoid to directly expose internal objects (controls, for example), setting their access modifier as `public` (bad things happen and you don't have a unique *entry point* - the public property/method - to verify what's going on). – Jimi Nov 04 '19 at 14:57
  • 1
    You won't get a lot of answers to this question because the simple solution (as of right now the only answer below) is anti-OOP and a solution using OOP is too involved because it should use non-UI classes to contain your logic and state, and use both properties/methods AND events. Yes, fine, the forms can have references to each other and the OP can continue coding VB as if it's 1991. But this solution will ultimately cause more problems than it solves if it's allowed to exist in a larger scale solution trying to OOP. But it's also a dupe of innumerable questions so voting to close. – djv Nov 04 '19 at 16:39
  • @djv: I see you pointing at my answer :-). While you have more sofisticated ways, you need to consider what you're doing. Is it a lunar vehicle, is it an app for thousands of various users, or is it an ap for a given environment inside your company? In the latter case, you have typically an hour or few to do it, or it won't be done. Simple solutions are the key, in this case. And it is fully OOP - objects are created and instantiated. The possible problems are only theory, since you use it in a closed environment as a standalone app. In reality, I've never heard of or experienced any problem. – Oak_3260548 Nov 04 '19 at 17:09
  • Though this question is probably a duplicate, the linked question is not a correct duplicate question. It's C#, not VB.NET. – Oak_3260548 Nov 06 '19 at 11:08

1 Answers1

0

You can expose public property or variable and use it to define instance of the "linked" form.

Provided you have Parent-Child relation (simplified to relevant lines!):

Form2 code:

Public ParentFrm as Form1    ' in the header of the class Form2

Form1 code, where you create Form2 (run just once):

Dim Frm2 as Form2
Frm2.ParentFrm = Me
Frm2.Show()

Play with Panel from Form2 inside Form1:

ParentFrm.Panel1.Visible = False

You can access any GUI objects and public properties and variables this way.

If you have sibling relation, you can do the same, but via the common parent form.

There are more methods to communicate between forms, but this one is the simplest and works perfectly even with complicated forms. As oposed to this, you have options like MDI.

Oak_3260548
  • 1,882
  • 3
  • 23
  • 41
  • Make sure to make the controls as Public. By default they will not be. The property is `Modifiers`. See [this answer](https://stackoverflow.com/a/4133949/832052) – djv Nov 04 '19 at 16:00
  • This @jimi's comment above about public controls and that you (@djv) mention to, stroke me, because I did it thousands of times over last decade and never ever ran into such issue. When you define the forms as above, it works. I am interested in details, when it does not, though... – Oak_3260548 Nov 04 '19 at 16:07
  • I looked at the link and few posts and I think the scenario about accessing control of a form from another class is a different scenario. In this scenario it works without `Modifiers` set to `Public`, as the form is inherited via the local object. Not sure if my phraseology is correct, but it works absolutely fine this way. – Oak_3260548 Nov 04 '19 at 16:14
  • If the modifier is Friend, it works when both forms are in the same assembly. This will not work across projects - you would need it to be Public for that. – djv Nov 04 '19 at 16:30
  • 1
    This issue should never arise. You should have objects holding your logic and state. When the object is modified a certain way, it would raise an event to which Form1 subscribes, and Form1 can make its own panel visible or not. – djv Nov 04 '19 at 16:44