Referring to control on other form

  • Thread starter Thread starter Elmo Watson
  • Start date Start date
E

Elmo Watson

Control1 is on Form1

on Form two - I'd like to access the instanciation of that control
(Control1), using different properties & methods

Is there a way, in VB.Net, to refer to a control on another form?
 
This depends on where Form 1 and Form 2 are created. Is form 1 created
from form 2?

if it is, then you definitely already have access via your standard
object that you instantiated to show form 1. form1.control1.forecolor
for example.

If this isn't the case, you can do the following:

Change the visibility of that control so that it can be seen by another
object. In otherwords, make the control shared. This is the easiest
way to do it, but it can possibly be problematic. You will be changing
the auto generated code that Visual Studio provides. But I ran a few
basic tests and it doesn't seem to be a problem.

You'll do the following to accomplish this.
'Generated Code shows
Friend WithEvents TextBox1 as System.Windows.Forms.TextBox
'You'll need to change it to
Friend Shared WithEvents TextBox1 as System.Windows.Forms.TextBox

Then you can just do Form1.TextBox1... to access whatever you want with
that control.

There are some other things you can do too. It really depends on what
control you want to capture and what about that control you want to
capture.
The structure of your app can also determine the best way to handle
this. If you need to capture events, you should then have a global,
maybe in a different class or your component application class that
runs the forms. If and when those forms are fired, the component app
can communicate between the two forms whatever data needs to be passed
between the two.
You can also create a class with some shared properties that will hold
the properties that you want access to on the other form. Just
remember to set them whenever changes occur on Form1 that you want to
capture.

Derek Woo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top