Controlling Controls on another Form

G

Guest

This is rediculous that I can't figure this out. I'm really getting frustrated with VB .NET. Everything seemed so much easier in VB5 and VB6. I'm really getting to feel conciseness about the number of questions I need to ask. If it weren't for a couple of you I would have accomplished almost nothing. Oh well, I guess I help your MVP status

Anyways, sorry for that. Here is my problem. I have form1 and form2. Form1 has 25 labels and form2 has 25 checkboxes. the labels on form1 all have their visible property set to false on application startup. When I am on form2 I want to be able to check a checkbox and make the corresponding label on form1 visible. I will exit form2 and return to form1 when I click on the Apply button so some of the code can probably be in either form2.close or form1.show or maybe a got.focus or lost.focus

Thank you
John
 
P

Patrick Steele [MVP]

I have form1 and form2. Form1 has 25 labels and form2 has 25 checkboxes.
the labels on form1 all have their visible property set to false on
application startup. When I am on form2 I want to be able to check a
checkbox and make the corresponding label on form1 visible.

Form2 will need a reference to Form1 to be able to access the Labels.
Create a property on Form2 that will accept a Form1 instance:

Public Class Form2
Inherits System.Windows.Form

...
Private m_Form1 As Form1
...
Public Property MainForm
Set
m_Form1 = Value
End Set
End Property

...
End Class

Now, before showing Form2, pass Form1 to it:

(somewhere in Form1)

Dim f2 As Form2 = New Form2
f2.MainForm = Me
f2.ShowDialog()

Now inside the checkbox click events in Form2, you can access Form1
labels through the m_Form1 variable.
 
C

Cor Ligthert

Hi John,

When you use this in your program, (on a new item class page or even pasted
under your form1) then you can use it everywhere on every form in your
project. If you have beside this problems with showing the form, message
that again? Making them every time new will in my opinion not solve your
problem.

I hope this helps?

Cor
\\\
Public Class Checked
Private Shared mCheck(25) As Boolean
Public Shared Sub SetCheck(ByVal index As Integer, _
ByVal setting As Boolean)
mCheck(index) = setting
End Sub
Public Shared Function _
GetCheck(ByVal index As Integer) As Boolean
Return mCheck(index)
End Function
End Class
///
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?amNyb3VzZQ==?= said:
Anyways, sorry for that. Here is my problem. I have form1 and
form2. Form1 has 25 labels and form2 has 25 checkboxes. the labels on
form1 all have their visible property set to false on application
startup. When I am on form2 I want to be able to check a checkbox and
make the corresponding label on form1 visible. I will exit form2 and
return to form1 when I click on the Apply button so some of the code can
probably be in either form2.close or form1.show or maybe a got.focus or
lost.focus.

Are you creating 'Form2' within 'Form1'? Then you can easily pass a
reference to your instance of 'Form2' by adding a property of type
'Form1' to your 'Form2' and setting this property to 'Me' in 'Form1':

\\\
Public Class Form2
Inherits...

Private m_MyForm1 As Form1

Public Property MyForm1() As Form1
Get
Return m_MyForm1
End Get
Set(ByVal Value As Form1)
m_MyForm1 = Value
End Set
End Property
...
End Class
///

Inside 'Form1':

\\\
Dim f As New Form2()
f.MyForm1 = Me
f.Show()
///
 
C

Cor Ligthert

Hi John,

I forgot to tell how to use it.

mycheckbox.checked = checked.getcheck(23) 'checkbox 24

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
When you use this in your program, (on a new item class page or even pasted
under your form1) then you can use it everywhere on every form in your
project. If you have beside this problems with showing the form, message
that again? Making them every time new will in my opinion not solve your
problem.

I hope this helps?

Cor
\\\
Public Class Checked
Private Shared mCheck(25) As Boolean
Public Shared Sub SetCheck(ByVal index As Integer, _
ByVal setting As Boolean)
mCheck(index) = setting
End Sub
Public Shared Function _
GetCheck(ByVal index As Integer) As Boolean
Return mCheck(index)
End Function
End Class
///

1. Why not use a property?

2. I don't understand how this will allow access to the other forms'
controls.
 
C

Cor Ligthert

Hi Herfried,

I will take the time, however when it is to difficult you may ask again.

Let say you use the visible event from the form, than you can do in that
when you have used a simple textbox array.

for I as integer = 0 to 24
mytextbox(i).enabled = checked.getcheck(i)
next

Simple is it not?

What would be the benefit if a property was used by the way (with wich I
started however this is much simpler)

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
I will take the time, however when it is to difficult you may ask again.

Let say you use the visible event from the form, than you can do in that
when you have used a simple textbox array.

for I as integer = 0 to 24
mytextbox(i).enabled = checked.getcheck(i)
next

Simple is it not?

Right, but you cannot force the form to update without having a
reference to it.
What would be the benefit if a property was used by the way (with wich I
started however this is much simpler)

I hate those 'Set*' and 'Get*' methods :).
 
C

Cor Ligthert

What would be the benefit if a property was used by the way (with wich I
I hate those 'Set*' and 'Get*' methods :).

Exactly the same with me however in this case the alternative was in my
opinion more difficult to describe.

:)

Cor
 

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

Top