Accessing form controls from mudules

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Hello:

I have a form with 26 panels. Each panel has a bunch of other controls in
it. The panels visible property are set to False.

Each panel has a corosponding button to set it's visible property to true
and all the other panels to false when clicked.

Well, thats a lot of code in the form. I would like to move the code for the
buttons to a module for code organization.

Here is the code in the form I have for each button.

I tried creating a sub in the module and putting all this in in the sub

Module modPanels
Dim frm1 as Form1


frm1.Label01.Visible = True 'Raises an error. Not set to an instance of an
object. As New Form1 causes nothing to happen
frm1.Panel01.Visible = True

<Same for other panels> etc
End Module

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Label01.Visible = True
Panel01.Visible = True

Panel02.Visible = False
Label02.Visible = False

Panel03.Visible = False
Label03.Visible = False

Panel04.Visible = False
Label04.Visible = False

Panel05.Visible = False
Label05.Visible = False

Panel06.Visible = False
Label06.Visible = False

Panel07.Visible = False
Label07.Visible = False

Panel08.Visible = False
Label08.Visible = False

Panel09.Visible = False
Label09.Visible = False

Panel10.Visible = False
Label010.Visible = False

Me.Panel11.Visible = False
Me.Label011.Visible = False

Me.Panel12.Visible = False
Me.Label012.Visible = False

Me.Panel13.Visible = False
Me.Label013.Visible = False

Me.Panel14.Visible = False
Me.Label014.Visible = False

Me.Panel15.Visible = False
Me.Label015.Visible = False

Me.Panel16.Visible = False
Me.Label016.Visible = False

Me.Panel17.Visible = False
Me.Label017.Visible = False

Me.Panel18.Visible = False
Me.Label018.Visible = False

Me.Panel19.Visible = False
Me.Label019.Visible = False

Me.Panel20.Visible = False
Me.Label020.Visible = False

Me.Panel21.Visible = False
Me.Label021.Visible = False

Me.Panel22.Visible = False
Me.Label022.Visible = False

Me.Panel23.Visible = False
Me.Label023.Visible = False

Me.Panel24.Visible = False
Me.Label024.Visible = False

Me.Panel25.Visible = False
Me.Label025.Visible = False

Me.Panel26.Visible = False
Me.Label026.Visible = False
End Sub

TIA

Bob
 
Bob said:
Hello:

I have a form with 26 panels. Each panel has a bunch of other controls in
it. The panels visible property are set to False.

Each panel has a corosponding button to set it's visible property to true
and all the other panels to false when clicked.

Well, thats a lot of code in the form. I would like to move the code for the
buttons to a module for code organization.

Here is the code in the form I have for each button.

I tried creating a sub in the module and putting all this in in the sub

Module modPanels
Dim frm1 as Form1


frm1.Label01.Visible = True 'Raises an error. Not set to an instance of an
object. As New Form1 causes nothing to happen
frm1.Panel01.Visible = True

<Same for other panels> etc
End Module

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Label01.Visible = True
Panel01.Visible = True

Panel02.Visible = False
Label02.Visible = False

Panel03.Visible = False
Label03.Visible = False

Panel04.Visible = False
Label04.Visible = False

Panel05.Visible = False
Label05.Visible = False

Panel06.Visible = False
Label06.Visible = False

Panel07.Visible = False
Label07.Visible = False

Panel08.Visible = False
Label08.Visible = False

Panel09.Visible = False
Label09.Visible = False

Panel10.Visible = False
Label010.Visible = False

Me.Panel11.Visible = False
Me.Label011.Visible = False

Me.Panel12.Visible = False
Me.Label012.Visible = False

Me.Panel13.Visible = False
Me.Label013.Visible = False

Me.Panel14.Visible = False
Me.Label014.Visible = False

Me.Panel15.Visible = False
Me.Label015.Visible = False

Me.Panel16.Visible = False
Me.Label016.Visible = False

Me.Panel17.Visible = False
Me.Label017.Visible = False

Me.Panel18.Visible = False
Me.Label018.Visible = False

Me.Panel19.Visible = False
Me.Label019.Visible = False

Me.Panel20.Visible = False
Me.Label020.Visible = False

Me.Panel21.Visible = False
Me.Label021.Visible = False

Me.Panel22.Visible = False
Me.Label022.Visible = False

Me.Panel23.Visible = False
Me.Label023.Visible = False

Me.Panel24.Visible = False
Me.Label024.Visible = False

Me.Panel25.Visible = False
Me.Label025.Visible = False

Me.Panel26.Visible = False
Me.Label026.Visible = False
End Sub

TIA

Bob

You never created an instance of Form1

dim frm1 as new Form1

Otherwise frm1 is just a pointer to nothing.

Chris
 
I did

Here is what I tried and nothing happens. Not even an error


Module modPanels
Sub Panel2()
Dim frm1 As New Form1

frm1.Label01.Visible = False
frm1.Panel01.Visible = False

frm1.Panel02.Visible = True
frm1.Label02.Visible = True

frm1.Panel03.Visible = False
frm1.Label03.Visible = False

frm1.Panel04.Visible = False
frm1.Label04.Visible = False

frm1.Panel05.Visible = False
frm1.Label05.Visible = False

frm1.Panel06.Visible = False
frm1.Label06.Visible = False

frm1.Panel07.Visible = False
frm1.Label07.Visible = False

frm1.Panel08.Visible = False
frm1.Label08.Visible = False

frm1.Panel09.Visible = False
frm1.Label09.Visible = False

frm1.Panel10.Visible = False
frm1.Label010.Visible = False

frm1.Panel11.Visible = False
frm1.Label011.Visible = False

frm1.Panel12.Visible = False
frm1.Label012.Visible = False

frm1.Panel13.Visible = False
frm1.Label013.Visible = False

frm1.Panel14.Visible = False
frm1.Label014.Visible = False

frm1.Panel15.Visible = False
frm1.Label015.Visible = False

frm1.Panel16.Visible = False
frm1.Label016.Visible = False

frm1.Panel17.Visible = False
frm1.Label017.Visible = False

frm1.Panel18.Visible = False
frm1.Label018.Visible = False

frm1.Panel19.Visible = False
frm1.Label019.Visible = False

frm1.Panel20.Visible = False
frm1.Label020.Visible = False

frm1.Panel21.Visible = False
frm1.Label021.Visible = False

frm1.Panel22.Visible = False
frm1.Label022.Visible = False

frm1.Panel23.Visible = False
frm1.Label023.Visible = False

frm1.Panel24.Visible = False
frm1.Label024.Visible = False

frm1.Panel25.Visible = False
frm1.Label025.Visible = False

frm1.Panel26.Visible = False
frm1.Label026.Visible = False
End Sub
End Module


Then in my buttons click event in the Form1

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
modPanels.Panel2()
End Sub
 
Chris:

Form1 is already open. Thats the form that has the button on it. Putting
frm1.showdialog() in the Module opens another Form1

TIA

Bob
 
Bob said:
Hello:

I have a form with 26 panels. Each panel has a bunch of other
controls in it. The panels visible property are set to False.

Each panel has a corosponding button to set it's visible property to
true and all the other panels to false when clicked.

Well, thats a lot of code in the form. I would like to move the code
for the buttons to a module for code organization.

"Organization"? Don't do it as it's often considered bad design. Why don't
you keep it in the Form? If you need the code multiple times, put all
controls onto a usercontrol and place it on as many forms as you need.


Armin
 
Chris:

Thanks. That did it.
But my question then becomes why even put this in a module, since you are
acting on frm1, then put the code in frm1, that's why it is a class....

Yeah someone else said that. I just wanted to break some of that code out of
the form for organization. Is it a performance hit? I'm just using a
standerd module. Not a class mod.

here is the code I wanted to get out of the main form.
http://www.are-hosting.com/panelcode.asp
My skills will improve in time. Maybe when you go to that link, you'll
still think I'm wasting time. Plus it's good experience however trivial it
might be. Gotta start somewhere.

I will be accessing it from other controles later too. There probably is a
better way but if I knew that, I wouldn't be asking for answers to basic
stuff like what you helped me with. I need more experience. I read up on
passing arguments and actually build some functions in VB 6 but that one
eluded me.

Thanks for the help.

Bob
 

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