EASY question in regards to object references

Chronis

Member
Joined
Jul 11, 2006
Messages
9
Programming Experience
Beginner
This is driving me insane, because it should be SO EASY!! I just keep thinking of VB 6.0 and that way how to do it.

SIMPLE question here, HOW do i gain access to all of a certain forms controls and properties in a module?

E.G -

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button6_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE]
[SIZE=2]System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button6.Click[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2]     Call ChangeButton6()[/SIZE]
 
[COLOR=#0000ff]     End[/COLOR][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[COLOR=#0000ff][/COLOR] 
[SIZE=2][SIZE=2][COLOR=#0000ff]Module[/COLOR][/SIZE][SIZE=2] ChangeObject[/SIZE]
[SIZE=2] 
[/SIZE][SIZE=2][COLOR=#0000ff]    Sub[/COLOR][/SIZE][SIZE=2] ChangeButton6()
    button6.text = "My text is changed!"
 
[COLOR=#0000ff]End[/COLOR][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[COLOR=#0000ff][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] 
End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Module[/COLOR][/SIZE][/SIZE]

Yes I know this is not going to work on it's own because I have to reference the form's objects and all, but I've tried heaps of stuff and I can't get it to work. Thats basically the layout there, please give me a hand in getting access to all the form objects so if the button was clicked, it's text would eb modified in run time.

Seems so easy but it's seriously driving me insane.
 
I have to say that i am going to answer this not knowing why you would ever want to. You need to pass the form into the sub in an argument

VB.NET:
Sub ChangeForm(ByRef Frm as form)
...
end sub
 
Or pass the Button in
 
Sub ChangeButton(ByRef Btn as button)
...
end sub
But why would you want to?
 
Well i need access to all the forms objects so I can add various information to the combo boxes, list boxes and such on the form from the module.

Depending on the option chosen, it's going to call the correct module where all the code for that option will lie there for.
 
Can you not do all that from within that forms class? Or even create a form updater class and pass the form or the forms control collection into that class for further processing. Aside from that the code i posted above should work.
 
I could do it all within the forms class, but it's getting all too long and messy and I'm getting lost, and the modules would be perfect to break up the code into logical sequences.

But a) I don't know how to reference an entire forms controls so that i can access it's objects and they can be changed in run time.

Your code that you gave will only affect the form, but i need everything within the form, including all the combo boxes, list boxes, buttons, everything

How can I do this.. ?
 
You can pass the forms ControlCollection....

VB.NET:
Sub ChangeForm(ByRef ContCollection as System.windows.Forms.ControlCollection)
 
End sub
 
To use
 
Module.ChangeControls(Me.Controls)
The only problem with doing that is you would have to iterate through the controlcollection with a for - next/each loop and then determine the type of the control and then alter it accordingly.

VB.NET:
Sub ChangeControls(ByRef ConCollection as System.Windows.Forms.ControlCollection)
 
Dim Lb as listbox
 
For each Ctrl as control in ContCollection
 
if typeof ctrl is ListBox then
Lb = directcast(ctrl,listbox)
 
'Then do whatever with it here
 
next
 
end sub
It would work but it is in danger of getting a bit messy
 
Chronis said:
...
please give me a hand in getting access to all the form objects so if the button was clicked, it's text would be modified
...
but i need everything within the form, including all the combo boxes, list boxes, buttons, everything
...
Does all your methods really need access to everything? That sounds like a beginner design flaw. Think in objects - and Everything is a really bad object noone can handle. If one of your methods exceeds 50 lines of code, then break it down into smaller methods, if the resulting method is reusable in another context you may have done a good job. Example; you don't create a method that fill the combo from one source and fill the listbox from another and also dis-/enable the buttons and sets the font and colors of all textboxes. Instead you create different methods for this, and each need only know about one control, a combobox or a listbox or a button or a textbox. In the case of setting the font of all textboxes you need two methods, one that iterates all the controls on form to find textboxes, and one that sets the font and colors of a single textbox. You see? A method should only do one task.
 
My code

Whew, let me just say before I post this reply, I hit submit reply and the quoth the server "404", just as I quickly copied it as I noticed it was crashing.

Thanks heaps for the help guys for helping this newbie out, but im still so confuzzled on how im going to build...perhaps sharing out my code and exact intenion might help. forgive the little bit of code here but its easy to read and understand im sure.

This is what I want to do - I have 5 combo boxes. It is a trouble shooting program on when the user can't access the internet and what steps to take to get access again.

All the other combo boxes are going to depend on what the previous combo box answer is...and I was thinking of adding something like this - the module level variable is to determine which modem was picked in the first place.

Oh, and mResolution is the variable needed for a button click event to determine which form to load.

VB.NET:
[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=black]Dim mWhichModem As String 'Determines which modem has been picked[/COLOR]
[COLOR=black]Dim mResolution As String 'Determines which resolution form to load[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=blue]Private[/COLOR][COLOR=black] [/COLOR][COLOR=blue]Sub[/COLOR][COLOR=black] cboModem_SelectedIndexChanged[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=black]ComboBox2.Items.Text = "Please enter what the lights are doing"[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=black]Select Case cboModem.SelectedIndex[/COLOR]
[COLOR=black]  Case is = 0
     ComboBox2.Items.Add("No DSL light")[/COLOR]
[COLOR=black]     ComboBox2.Items.Add("No Power Light")[/COLOR]
[COLOR=black]     mWhichModem = "SpeedTouch530"[/COLOR]
[COLOR=black] Case is = 1[/COLOR]
[COLOR=black]    ComboBox2.Items.Add("No ADSL light")[/COLOR]
[COLOR=black]    ComboBox2.Items.Add("No Power Light")[/COLOR]
[COLOR=black]    mWhichModem = "DLink"[/COLOR]
[COLOR=black]End Select[/COLOR]
[COLOR=black]

So then for the next Combo Box, it will look something like this

VB.NET:
[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=blue]Private[/COLOR][COLOR=black] [/COLOR][COLOR=blue]Sub[/COLOR][COLOR=black] cboLights_SelectedIndexChanged[/COLOR]
[COLOR=black]Select Case mWhichModem[/COLOR]
[COLOR=black]   Case is = "SpeedTouch530" [/COLOR]
[COLOR=black]        Call SpeedTouch_Step1() 'Calls module to populate next cbobox[/COLOR]
[COLOR=black]   Case is = "DLink"[/COLOR]
[COLOR=black]       Call Dlink_Step1() 'Calls Dlink module to populate next box[/COLOR]
[COLOR=black]End Select[/COLOR]
[COLOR=black]

See, in the modules though , all the options won't be to populate the next combo box - depending on which item is chosen in the combo box may lead to another form showing up and only some options chosen will mean the next combo box to be populated with new items, depending on the steps taken.

My problem is, that there are way too many modems and types of problems to type it all in the Form Main Class, so I wanted to break them up into about 7 different modules (7 types of modems to choose) - and as each option is clicked, in the module it will call the appropriate steps, like step_1, step_2 procedures to carry out the tasks.

So, the module will look something like this (we'll take the SpeedTouch530 module for example)

VB.NET:
[/COLOR]
[COLOR=black]Module SpeedTouch530[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=blue]Public Sub[/COLOR][COLOR=black] Step1()[/COLOR]
[COLOR=black]    Select Case cboLights.SelectedIndex[/COLOR]
[COLOR=black]    Case is = 0[/COLOR]
[COLOR=black]    btnResolution.BackColor = System.Drawing.Color.Red[/COLOR]
[COLOR=black]    frmResolution = "NoPower"[/COLOR]
[COLOR=black]    Case is = 1[/COLOR]
[COLOR=black]    ComboBox3.Items.Text = "Does the user have a session?"[/COLOR]
[COLOR=black]    ComboBox3.Items.Add = ("Yes")[/COLOR]
[COLOR=black]    ComboBox3.Items.Add = ("No")    [/COLOR]
[COLOR=black] [/COLOR]
[COLOR=blue]End[/COLOR][COLOR=black] [/COLOR][COLOR=blue]Sub[/COLOR][COLOR=black][/COLOR]
[COLOR=black] [/COLOR]
[COLOR=black]Public Sub Step2()[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=black]   Select Case ComboBox3.SelectedIndex[/COLOR]
[COLOR=black]   Case is = 0[/COLOR]
[COLOR=black]   btnResolution.BackColor = System.Drawing.Color.Red[/COLOR]
[COLOR=black]   frmResolution = "LockedSession"[/COLOR]
[COLOR=black]   Case is = 1[/COLOR]
[COLOR=black]   ComboBox3.Items.Add = "Can the user see the modem interface?"[/COLOR]
[COLOR=black]   ComboBox3.Items.Add = ("Yes")[/COLOR]
[COLOR=black]   ComboBox3.Items.Add  =("No)[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=blue]End Sub[/COLOR][COLOR=black][/COLOR]
[COLOR=black] [/COLOR]
[COLOR=blue]Public Sub Step3()[/COLOR][COLOR=black][/COLOR]
[COLOR=blue] [/COLOR][COLOR=black][/COLOR]
[COLOR=blue]'etc. etc, same process as Step_2()[/COLOR][COLOR=black][/COLOR]
[COLOR=blue] [/COLOR]
[COLOR=black]

It all depends on which item was checked in the next combo box to add the next set of items in the combo box (and as you can see, with some cases the next combo box doesn't need to be populated at all)

So, what I'm trying to do, is make a module for each modem, and have all the steps needed to get the final resolution in them.

Guys, once again thanks so much for the help, and yes I know its very beginner stuff but I'm really grateful :)
 
In this case it at first glance seems more appropriate to use #Regions within main form or Partial classes of main form to help separate sections of code. But there may be other ways of doing things.

One little refactoring I can see from these little code snippets is method below. Actually it seems you sometimes use the Text property for prompt and other times the first item. Using Text property for this is not a good idea, because user first tries one option and forget what the question was but the question has disappeared. So instead always using the first item (selectedindex 0) for this could work. I have based the code below upon this assumption, also for reuse of comboxes at different steps by clearing items before new is added. Another thing is there could be lots of options, so this method could be overloaded to accept different input, sometimes you would just use the simple two-option method, other times you would create a String array of options and pass in.

VB.NET:
Sub PopulateCombo(ByVal combo As ComboBox, ByVal prompt As String, ByVal option1 As String, ByVal option2 As String)
  PopulateCombo(combo, prompt, New String() {option1, option2})
End Sub
 
Sub PopulateCombo(ByVal combo As ComboBox, ByVal prompt As String, ByVal options() As String)
  combo.Items.Clear()
  combo.Items.Add(prompt)
  combo.Items.AddRange(options)
  combo.SelectedIndex = 0
End Sub
Which you several places could use like this for instance:
VB.NET:
PopulateCombo(ComboBox2, "Question?", "option1", "option2")
Reducing three codelines to one should be good for much repeated code.
 
vis781 and JohnH, I just want to thank both so much for all your help on this. FINALLY, I have figured it out with your help on how I'm going to do this. This project I'm creating has been a big milestone for me, and well it's still beginner level, but we all gotta start somewhere right?

I can see myself hanging around these forums quite a lot and I'll help whoever I can because I'll remember you both helping me like this. It's just that this problem has seriously been driving me insane/sleepless/caffeine pepped up/chain smoking for about 2 days lol.

Thanks a ton again for both of your help, it suddently all seems to clear *looks to the clear skies* :D
 
Back
Top