Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem loading combobox insite a function

Status
Not open for further replies.

DeeRuff1

Programmer
May 17, 2005
17
US
I am having a problem. I have created a function. Inside the function I am trying to load information inside my combo box. The data is not making it out side the function to fill the combo box. I know that it is something simple but I am beating my head against the wall.
 
Post a piece or all the function.

the function is like:
1. function name()
2. function name() as type

?
 
Here is an Example of the Function :


Public Function BuildComb()

FORM1.Combo1.items.Add("Apple")
FORM1.Combo1.items.Add("Grapes")
FORM1.Combo1.items.Add("Rings")

End Function

This is not working. Is there something different in .NET compared to VB6?

 
Is Form1 the instantiation of the form that you are trying to work with?

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
I believe this creates and instance of Form1.Comb1. But I am not sure if the function is sending the data out to the load the form's combo box outside the function.
 
You have not created an instance of form1. Remove the "FORM1." from all:

Public Function BuildComb()

Combo1.items.Add("Apple")
Combo1.items.Add("Grapes")
Combo1.items.Add("Rings")

End Function
 
Lets say your neighboors have a boy named Bob. Any you have a boy named Bob. They are both humans, they both have the identifier "Bob", but they are most definately, not the same being.


In VB6 the way forms were accessed was always Singleton. No mater where you called Form1 from, it was always the 1 Form1 there was.

In VB.Net everything (Including forms!!!) is object oriented. If Form1 is the name of the class, you could have 50 different variables of type Form1, each of them completely independant from all of the others. You could show them, you could populate combo boxes, but you have to work with the correct one.

So, here is what I would suggest, if your method is in the Form1 Class, us the Me keyword. That will always reference the instance that the sub is a part of. Like so:
Code:
Public Sub BuildCombo()
     me.Combo1.items.Add("Apple")
     me.Combo1.items.Add("Grapes")
     me.Combo1.items.Add("Rings")
End Sub

If it's not, then I would suggest passing the combo box into the sub, like so:
Code:
Public Sub BuildCombo(byref cmbFood as ComboBox)
     cmbFood.items.Add("Apple")
     cmbFood.items.Add("Grapes")
     cmbFood.items.Add("Rings")
end sub

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
I just want to Thank everyone who helped. Thanks Rick ******************* It Worked *************************
 
bclt, if the method is not in the class that contains the Combo1 object, then you sub will not work. By passing a reference to the combo box into the sub, then you can be assured that you are working with the correct object.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
In this case U r right, but how should i know that this function is not in the form-code but somewhere else like a module ?
 
You don't, he never specified. But since he was refering to the Form1 object you can guess he is likely coding in a place other then the Form1 class.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top