|
-
May 22nd, 2004, 07:31 AM
#1
Thread Starter
Fanatic Member
Using objects on different forms [Resolved]
hi,
this question should be really easy, and i dont understand why something so easy is causing me problems...
I have a form, called frmMain which starts up with the program. Now in a class called Connection.vb it does several things, and at a certain point in the class i want it to update a listbox on frmMain.
How the hell do i go about adding an item to a listbox on frmMain when im in this class?
I know about methods where u have to declare the form, like
Dim formMain as New frmMain
but the thing is, doing that will make a new instance of the form and not update the listbox on the form that is already open.
doing
Dim formMain as frmMain
won't work either, i get some error.
How do i do this? I just don't understand... without any declorations, and write frmMain. I get options like ActiveForm and don't understand...
in VB6 this was so easy, you just simply wrote frmMain.textbox1.text = "bla"
so why can't you just do something that easy in VB.NET? like
frmMain.listbox1.items.add("blah")
Thanks for the help
Last edited by LITHIA; Jul 29th, 2004 at 08:51 AM.
-
May 22nd, 2004, 08:42 AM
#2
Sleep mode
Why don't you call the function that updates the listbox from the MainFrm , passing the listbox as paramter to the function . It should be easy if I understand right .
-
May 22nd, 2004, 10:31 AM
#3
The real issue here is that in VB6 when you used the form name to access a form it automatically generated an instance of it and shared it anywhere via that name variable. This feature returns in the next version. This is basically the same as having the form declared public in a module. So you can do that if you want.
It would be more OOP to pass along a reference to a listbox and have it update that or to raise an Updated event and change the listbox in that. This topic has come up several times and a search would probably bring about some answers.
-
May 22nd, 2004, 01:51 PM
#4
Thread Starter
Fanatic Member
Originally posted by Pirate
Why don't you call the function that updates the listbox from the MainFrm , passing the listbox as paramter to the function . It should be easy if I understand right .
because im updating the listbox with private variables in the class that run from the stuff in the class (where i gets the variables results from)... i was thinking of this, but disregarded it because of this issue.
Edneeis, could you give me something to search for? I really did not have an idea, thats why i did this post. Could you explain what you mean as well please? Example code would be great and I would really appreciate it!
Thanks guys
-
May 22nd, 2004, 03:26 PM
#5
Here is an example of how to pass the reference or use an event hopefully it will help.
-
May 23rd, 2004, 04:33 AM
#6
Thread Starter
Fanatic Member
thanks, ill try and make sense of that... although has it gone away from the problem i have about just targetting objects from a class on a form?
This isn't going to help in later problems with the same issue is it?
Thanks for the help, I appreciate it
-
May 23rd, 2004, 12:17 PM
#7
The issue is just passing a reference. You can setup a global form like you had in VB6 but I'd recommend just passing the things the class needs into the method calls.
If you want to access a form or its objects from within a class just pass the class a reference to the form, thats it.
-
May 23rd, 2004, 12:22 PM
#8
Here is another example that follows the same principal with forms accessing other forms controls.
http://www.iedotnetug.org/downloads/MultiForm.zip
-
May 23rd, 2004, 05:24 PM
#9
PowerPoster
Hi Lithia,
IF Edneeis' posted solutions have not solved your long term problem, (I have not checked them out), why don't you simply use his other suggestion? i.e.
In the Module
Public frmMain as New fclsMain
Public Sub Main
Application.Run(frmMain)
End Sub
In fclsMain
Dim clConnection as New Connection
This may not be pure OOP but if it works then use it and become an OOP purist with the next version ov VB.NET.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 24th, 2004, 07:04 AM
#10
Thread Starter
Fanatic Member
i was just wondering if there was a way to target the main thingy that starts up with the app.
you know when u specify which form you want to show, or use Sub Main() in a mod.
because if there's a way to target the form that loads, i can do it like that. otherwise ill have to declare it in the module/class and open it from there instead. shouldn't be a prob... just kinda annoying... and may make some probs as well. the form gets declared as public yeah? so i can access it from a different class or module that i declared it in.
well i hopes anyway...
-
May 24th, 2004, 07:27 AM
#11
PowerPoster
Originally posted by LITHIA
i was just wondering if there was a way to target the main thingy that starts up with the app.
you know when u specify which form you want to show, or use Sub Main() in a mod.
because if there's a way to target the form that loads, i can do it like that. otherwise ill have to declare it in the module/class and open it from there instead. shouldn't be a prob... just kinda annoying... and may make some probs as well. the form gets declared as public yeah? so i can access it from a different class or module that i declared it in.
well i hopes anyway...
Sorry, you are not making any sense.
What do you mean by "Main Thingy"?? If you mean the form you want to start the project, then I have shown you how to do that, using Sub Main.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 24th, 2004, 07:30 AM
#12
Thread Starter
Fanatic Member
you know u define the form you want to start the project with, how can you target that?
I've set my project to start with frmMain and i want to be able to target frmMain through my class.
otherwise i will have to just declare frmmain and load it through the module i can use to start the project with instead of the form.
thanks for fast reply
-
May 24th, 2004, 06:37 PM
#13
PowerPoster
Originally posted by LITHIA
you know u define the form you want to start the project with, how can you target that?
I've set my project to start with frmMain and i want to be able to target frmMain through my class.
otherwise i will have to just declare frmmain and load it through the module i can use to start the project with instead of the form.
thanks for fast reply
I feel I must still be misunderstanding you.
Are you saying that you don't want to start your project through Sub Main in the module? If so, why don't you want to? It is much the easiest way to do what you want.
If you insist, then another way is to go to the region of code in formMain marked "Windows Form Designer Generated Code", find the "Friend With Events" line of the object you require to access from your class and replace "Friend" with "Public Shared". When you do this be very careful not to change anything else.
Hope this helps.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 25th, 2004, 11:29 AM
#14
Lively Member
if i get you right, your question is the first question a former vb.6-er will ask when using vb.net.
what you need is to pass a reference to your frmMain. you can do that in a module, then use this reference in other classes.
like this :
VB Code:
module module1
public MainForm as frmMain
end module
then in frmMain_load:
then in your classes just use mainform to reffer to your instance of frmMain. (i.e. instead of frmmain.label1.text you should use mainform.label1.text)
This, if i got you right, should be what you are looking for.
-
Jul 28th, 2004, 12:11 PM
#15
Addicted Member
After following the code and exmaples - I too am having issues with this.
FRMCUSTOMER
- This form has a SAVE button. When clicked, it checks for a similar name. If there are similar names, it calls FRMDUPE.
FRMDUPE
- Shows the possible matches for the customer name just entered. From here you can select one to bring up the existing record. If you decide to hit close, you are saying "My name is ok to add." I want this FRMDUPE to check the box on FRMCUSTOMER called chknameok.
Code:
frmcustomer.chknameok.checked=true
Even if declared - it tells me that the object is not referenced.
Any ideas... It seems like it should be easy to do.
-
Jul 28th, 2004, 12:47 PM
#16
Thread Starter
Fanatic Member
I questioned about this quite a while ago...
Since I have used the following solution.
Use a module with Sub Main in, and use this as your startup in your project.
In the submain, declare your first form you wish to show, eg.
Dim MyForm as New form1
Then what I do is use MyForm.ShowDialog()
This way it keeps the program open until you close this form. Then you can relate back to this form and its object with writing MyForm.ObjectName or subs. its pretty easy.
And you do this method for the other forms you want to show too! like
Dim MyForm2 as New form2
Then show it how you want, and you can link to that by using MyForm2.ObjectName and link from that or any other form back to the other form with MyForm1.ObjectName and so on for whatever you declare.
I find this works great for me, but I am sure there is probably a better method, but that's what I do. If anyone would like to share their way, or anything like that, feel free to. I don't know if this is the best method or if other people use it; I just came across using this myself
-
Jul 28th, 2004, 10:55 PM
#17
Addicted Member
this is exactly what I ended up doing and using a module to launch my main form and declare all others...
I hade to change some of my code as the CLOSE button on my forms was me.close() which prevented it from ebing called again. Now, I use me.hide()...
-
Jul 29th, 2004, 12:20 AM
#18
New Member
Hi,
I was just going through your problem, But it there has been long discussion which I was unable to follow
In short, If u want to Have a entry point in a Program
then define a module and say
Public Sub Main
End Sub
Within this proc... u can do anything like call a Form,Intsantiate a Class.....
Hope this will help u.
Tanmoy
-
Jul 29th, 2004, 03:04 AM
#19
Hyperactive Member
It's difficult for me to follow a so long discussion in english (sometime I have difficulties with short discussion,too ). Anyway it seems to me that Taxes and Lithia,at the end, are going in the same direction: Starting form sub main, in a module where FrmMain declared as public. I think it's a good idea. I have suggested exactly the same, but, because I'm always interested in new approach and different point of views, I'm very interested on to test the way of MindLoop. You can find similar approach on this forum, by Brown Monkey, too. Anyway, if the problem is only to refer a single form's control from a class, you can think also to pass it as a parameter (Byref, but it works also if Byval).
Live long and prosper (Mr. Spock)
-
Jul 29th, 2004, 09:51 PM
#20
Sleep mode
How's dug up this old thread ???
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|