Results 1 to 18 of 18

Thread: Simple VB control question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Location
    Illinois
    Posts
    20

    Simple VB control question

    Hi everyone,

    I know that there is probably a very easy way to do this, but I have yet to figure it out. Here is the basic scenario:

    Form1 contains Label1
    Form1 creates an instance of Form2

    Form2 contains Button1
    Form2.Button1.onClick needs to set Form1.Label1.Text = "something" (without creating a new instance of Form1 because I need to keep all other values as-is).

    Your help is appreciated. Thanks!

    -Chris

  2. #2

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Location
    Illinois
    Posts
    20
    That was one of the first things that I tried....the error is:

    Reference to a non-shared member requires an object reference.

  4. #4

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Location
    Illinois
    Posts
    20
    I checked out your project and I think I discovered our differences. My project is a VB.NET project. I'm not sure if that is the cause of the differences or not (sorry for not making that clear earlier).

    -Chris

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Location
    Illinois
    Posts
    20
    Here is my code:

    VB Code:
    1. Public Class Form1
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Dim form2 As New Form2()
    4.         form2.Show()
    5.     End Sub
    6. End Class
    7.  
    8.  
    9. Public Class Form2
    10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         Form1.Label1.caption = "something" ' this does not work
    12.     End Sub
    13. End Class

  7. #7

  8. #8

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Location
    Illinois
    Posts
    20
    Hmm....maybe this is not as easy to do as I thought. Your help is appreciated though Martin. Does anyone else have any suggestions?

  10. #10
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    You could have the following in Form2:
    VB Code:
    1. Private mfrmForm1 As Form1
    2.  
    3. Public Property Set Form1(Byval pfrmValue as Form1)
    4.    Set mfrmForm1 = pfrmValue
    5. End Property
    When you Load form2 from form1 just set Form1 = Me
    Then when you click the button just access the load varible...

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Location
    Illinois
    Posts
    20
    Wokawidget, is your code in VB6? .NET gave me an error about it no longer being supported. I tried to create something equivalent and it gave me null object errors when trying to set values in the form. I think it has something to do with how .NET uses "instances" of objects (i.e. I wasn't working with the actual form1, only an instance of it.)


    This is really frustrating, something that seems so easy and trivial is turning out to be quite a pain. Anyone else have any ideas?

  12. #12
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    you should be posting .NET questions in the .NET section. you'll get your answer there quicker than here. Maybe martinliss could move the thread?
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Location
    Illinois
    Posts
    20
    Would that be ok to move the thread, MartinLiss? Or should I just repost the question? Thanks.

    -Chris

  14. #14

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Location
    Illinois
    Posts
    20
    I found the solution to my problem. In case anyone is interested:

    VB Code:
    1. Public Class Form1
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Dim f2 As New Form2()
    4.         f2.Create(Me)
    5.     End Sub
    6. End Class
    7.  
    8. Public Class Form2
    9.     Private m_fParent As Form1
    10.  
    11.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.         m_fParent.Label1.Text = "something"
    13.     End Sub
    14.  
    15.     Sub Create(ByVal fParent As Form1)
    16.         m_fParent = fParent
    17.         Me.Show()
    18.     End Sub
    19. End Class

  16. #16
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Search the VB.NET Forum for passing values across forms or something like that . There are couple of ways , one of them is to override the form's constructor passing a form as para . The other way , is to define global form variable . I'm not sure if you can do it with delegates . Sorry I can't think of that right now .

  17. #17

  18. #18
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872
    You could just have Searched in the " Windows Form Designer generated code " of your form1 for the declarationn of your label and, you could add the word "Shared" in the beginning of the declaration. This is what caused this error "Reference to a non-shared member requires an object reference."
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

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