Sub Form Getting Values from the Parent Form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eBob.com

    #1

    Sub Form Getting Values from the Parent Form

    I have a "parent" form (if that's the right terminology), Form1. I
    declare two public values in the parent form :
    Public Class Form1
    Inherits System.Windows. Forms.Form
    Public CurDir As String = Directory.GetCu rrentDirectory( ) 'used
    by FormGetJobOutpu t
    Public PubUser As String 'tbxUser.text value for subForms

    In the "child" form, I call it FormGetJobOutpu t, I reference these two
    public values ...
    Sub CreateGetJobOut putScript(ByVal filename As String)
    Dim swScriptFile As New System.IO.Strea mWriter(CurDir( ) + "\"
    + filename, True)
    swScriptFile.Wr iteLine("open xxx.xxxx.com")
    swScriptFile.Wr iteLine(PubUser )

    The reference to CurDir works fine. (Although there is something a
    bit strange about it. I type "CurDir" but VS adds the parens. I have
    no idea why. But VB.Net seems to be happy with that syntax and it
    executes fine. But the reference to PubUser results in error message
    "Name 'PubUser' is not declared. If I use Form1.PubUser I get error
    "Reference to a non-shared member requires an object reference."
    I also tried a different declare for PubUser (Public PubUser As String
    = "") but that also did not help.

    What do I have to do to use PubUser in the child form? Why can I
    reference CurDir but not PubUser?

    Thanks, Bob
  • Chris

    #2
    Re: Sub Form Getting Values from the Parent Form

    eBob.com wrote:[color=blue]
    > I have a "parent" form (if that's the right terminology), Form1. I
    > declare two public values in the parent form :
    > Public Class Form1
    > Inherits System.Windows. Forms.Form
    > Public CurDir As String = Directory.GetCu rrentDirectory( ) 'used
    > by FormGetJobOutpu t
    > Public PubUser As String 'tbxUser.text value for subForms
    >
    > In the "child" form, I call it FormGetJobOutpu t, I reference these two
    > public values ...
    > Sub CreateGetJobOut putScript(ByVal filename As String)
    > Dim swScriptFile As New System.IO.Strea mWriter(CurDir( ) + "\"
    > + filename, True)
    > swScriptFile.Wr iteLine("open xxx.xxxx.com")
    > swScriptFile.Wr iteLine(PubUser )
    >
    > The reference to CurDir works fine. (Although there is something a
    > bit strange about it. I type "CurDir" but VS adds the parens. I have
    > no idea why. But VB.Net seems to be happy with that syntax and it
    > executes fine. But the reference to PubUser results in error message
    > "Name 'PubUser' is not declared. If I use Form1.PubUser I get error
    > "Reference to a non-shared member requires an object reference."
    > I also tried a different declare for PubUser (Public PubUser As String
    > = "") but that also did not help.
    >
    > What do I have to do to use PubUser in the child form? Why can I
    > reference CurDir but not PubUser?
    >
    > Thanks, Bob[/color]

    Your child form has to have a reference to the parent. Form1.PubUser
    doesn't work cause Form1 is a class definition not an instatance of the
    class. How are you opening child form? You could do something like:

    Class Child
    Parent as Form1
    ...... All your stuff
    public shadows sub ShowDialog(Valu e as Form1)
    Parent = Value
    End Sub

    Sub CreateGetJobOut putScript(ByVal filename As String)
    Dim swScriptFile As New System.IO.Strea mWriter(Parnet. CurDir()
    + "\" + filename, True)
    swScriptFile.Wr iteLine("open xxx.xxxx.com")
    swScriptFile.Wr iteLine(Parnet. PubUser)
    ..........
    End sub
    End Class

    This way whenever you open Child you have to pass in his parent.

    Lots of other ways to do it, can you make a property to hold it. There
    may be an owner property that will hold it already but I can't remember
    off the top of my head...

    Chris

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Sub Form Getting Values from the Parent Form

      eBob,

      Two points

      What is your currentdirector y. If it is something that you want to save for
      the user than a good place for that is the registry, when the user than
      restart it has a nice start point without intitializing that again.

      Your current user can you save in by instance a shared sub.
      \\\
      Public Class Variables
      Private Shared mUserName As String
      Friend Shared Property Username() As String
      Get
      Return mUserName
      End Get
      Set(ByVal Value As String)
      mUserName = Value
      End Set
      End Property
      End Class
      ////

      Use this only for that what is really needed on all forms. It is a fixed
      part of your program and uses therefore all the time the memory.

      I hope this helps,

      Cor


      Comment

      • eBob.com

        #4
        Re: Sub Form Getting Values from the Parent Form (Thanks)

        On Thu, 25 Aug 2005 23:35:11 -0400, eBob.com
        <eBob.com@total lyfakeisp.com> wrote:

        Thank you Chris and Cor. I appreciate your help. But you were both a
        bit over my head. I am a real novice. I finally remembered a
        technique I've used in the past. I declared some Public variables in
        the child class and set them in the parent form after instantiating
        the child but before the child.Show()

        Thanks, Bob

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: Sub Form Getting Values from the Parent Form (Thanks)

          erBob,

          Probably by *putting* it in that form

          \\\
          dim frm as new form2
          frm2.curdir = curdir
          frm2.user = user
          frm2.showdialog
          ///

          I hope this helps,

          Cor


          Comment

          Working...