Results 1 to 20 of 20

Thread: Using objects on different forms [Resolved]

  1. #1

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575

    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.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  4. #4

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    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

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is an example of how to pass the reference or use an event hopefully it will help.

  6. #6

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    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

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is another example that follows the same principal with forms accessing other forms controls.

    http://www.iedotnetug.org/downloads/MultiForm.zip

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  10. #10

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    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...

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  12. #12

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    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

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  14. #14
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    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:
    1. module module1
    2. public MainForm as frmMain
    3. end module
    then in frmMain_load:
    VB Code:
    1. mainform = me
    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.
    ehmm...

  15. #15
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188
    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.

  16. #16

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    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

  17. #17
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188
    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()...

  18. #18
    New Member
    Join Date
    Jul 2004
    Posts
    9
    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

  19. #19
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    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)

  20. #20
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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
  •  



Click Here to Expand Forum to Full Width