Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Reading Existing Instances - Help?

Reading Existing Instances - Help?

Scheduled Pinned Locked Moved Visual Basic
helpquestioncsharptutorial
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    halhamilton
    wrote on last edited by
    #1

    I'm fairly new to VB.NET, and am almost embarassed to ask this, but... If I already have a form instance open and want to refer back to it, how do I do that? Example --------- Public F1 as New Form1 '(say it has two textboxes 1 and 2) F1.show() 'Fill out TextBox1 Dim F2 as New Form2 F2.show() Now I'm in the F2 instance of Form2. I get some input that I now want to put back into F1. When I try the following in F2 F1.TextBox2.Text="yadayadayada" I get a syntax problem (squiggly underline) unless I re-instantiate F1 like so... Dim F1 as New Form1 This, of course, results in a new F1 instance, not the one I had before. Thanks for any help! If this is in any of my books, I can't seem to find it. Hal in AZ

    C R 2 Replies Last reply
    0
    • H halhamilton

      I'm fairly new to VB.NET, and am almost embarassed to ask this, but... If I already have a form instance open and want to refer back to it, how do I do that? Example --------- Public F1 as New Form1 '(say it has two textboxes 1 and 2) F1.show() 'Fill out TextBox1 Dim F2 as New Form2 F2.show() Now I'm in the F2 instance of Form2. I get some input that I now want to put back into F1. When I try the following in F2 F1.TextBox2.Text="yadayadayada" I get a syntax problem (squiggly underline) unless I re-instantiate F1 like so... Dim F1 as New Form1 This, of course, results in a new F1 instance, not the one I had before. Thanks for any help! If this is in any of my books, I can't seem to find it. Hal in AZ

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      If the form is no longer visible, then even though the class instance exists, the window does not. You need to get F1 to store this string variable outside the textbox ( best place would be in the click event that closes the window ), so you can still get to it. It's the height of ugliness for a control variable to be public, in any case. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

      H 1 Reply Last reply
      0
      • C Christian Graus

        If the form is no longer visible, then even though the class instance exists, the window does not. You need to get F1 to store this string variable outside the textbox ( best place would be in the click event that closes the window ), so you can still get to it. It's the height of ugliness for a control variable to be public, in any case. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

        H Offline
        H Offline
        halhamilton
        wrote on last edited by
        #3

        Sorry for not being clearer. The instance F1 is still visible, and its control TextBox1 contains data I want to use. I simply go to F2 to collect additional data, and want to put that data back into the same instance (F1) of Form1. Hal in AZ

        C 1 Reply Last reply
        0
        • H halhamilton

          Sorry for not being clearer. The instance F1 is still visible, and its control TextBox1 contains data I want to use. I simply go to F2 to collect additional data, and want to put that data back into the same instance (F1) of Form1. Hal in AZ

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          OK - then I would still make the textbox private, and add a property to allow you to get it's text. That will also give you something you can step into in the debugger, to see what's going on. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

          H 1 Reply Last reply
          0
          • C Christian Graus

            OK - then I would still make the textbox private, and add a property to allow you to get it's text. That will also give you something you can step into in the debugger, to see what's going on. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

            H Offline
            H Offline
            halhamilton
            wrote on last edited by
            #5

            Still doesn't address the problem. Let's say that in F1, TextBox1 and TextBox2 are private. Now, I write methods doText1 and doText2 with both Get and Set capability. Now we've taken care of the private part. BUT...it still doesn't do what I want. Inside of instance F2 (of Form2), I want to refer to these methods inside of instance F1 (of Form1). When I try to do something like F1.doText1.Set("yadayadayada") I still get a syntax error saying I haven't declared F1 from inside F2. In other words, inside of F2 any reference to F1 does not point to an instance. On the other hand, if (inside of F2) I DO declare the instance F1, it creates a NEW F1 rather than referring to the one I already have opened. I'd like, from instance F2 (of Form2), to be able to write something back to instance F1 (of Form1) without having the declaration construct a new instance named F1. Seems like this would be very easy, but I can't seem to get it to happen.:confused: Hal in AZ

            C 1 Reply Last reply
            0
            • H halhamilton

              Still doesn't address the problem. Let's say that in F1, TextBox1 and TextBox2 are private. Now, I write methods doText1 and doText2 with both Get and Set capability. Now we've taken care of the private part. BUT...it still doesn't do what I want. Inside of instance F2 (of Form2), I want to refer to these methods inside of instance F1 (of Form1). When I try to do something like F1.doText1.Set("yadayadayada") I still get a syntax error saying I haven't declared F1 from inside F2. In other words, inside of F2 any reference to F1 does not point to an instance. On the other hand, if (inside of F2) I DO declare the instance F1, it creates a NEW F1 rather than referring to the one I already have opened. I'd like, from instance F2 (of Form2), to be able to write something back to instance F1 (of Form1) without having the declaration construct a new instance named F1. Seems like this would be very easy, but I can't seem to get it to happen.:confused: Hal in AZ

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              halhamilton wrote: Still doesn't address the problem. It addresses two things. One is a design problem, the other is that you now have a point of entry you can mark to see how far your code is getting, a step towards working out what the problem is. halhamilton wrote: Now, I write methods doText1 and doText2 with both Get and Set capability. Properties you mean ? Why the set ? halhamilton wrote: F1.doText1.Set("yadayadayada") What ? What is doText1 ? halhamilton wrote: In other words, inside of F2 any reference to F1 does not point to an instance. Oh - I thought you were trying to call F1 from the code that holds F1 and F2. Unless you declare an instance inside F2 of F1, that is plainly the case. It sounds like you need to use an event and subscribe to it in the code outside of F2 and F1. halhamilton wrote: On the other hand, if (inside of F2) I DO declare the instance F1, it creates a NEW F1 rather than referring to the one I already have opened. Well, now, that's obvious. That's what you'd have asked it to do. You could SET an instance of F1 to equal the instance you already have, but an event is a far better way to handle this. halhamilton wrote: Seems like this would be very easy, but I can't seem to get it to happen It is very easy, but it's not the way to go. Look up events and delegates, that will put you on the right track. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

              H 1 Reply Last reply
              0
              • C Christian Graus

                halhamilton wrote: Still doesn't address the problem. It addresses two things. One is a design problem, the other is that you now have a point of entry you can mark to see how far your code is getting, a step towards working out what the problem is. halhamilton wrote: Now, I write methods doText1 and doText2 with both Get and Set capability. Properties you mean ? Why the set ? halhamilton wrote: F1.doText1.Set("yadayadayada") What ? What is doText1 ? halhamilton wrote: In other words, inside of F2 any reference to F1 does not point to an instance. Oh - I thought you were trying to call F1 from the code that holds F1 and F2. Unless you declare an instance inside F2 of F1, that is plainly the case. It sounds like you need to use an event and subscribe to it in the code outside of F2 and F1. halhamilton wrote: On the other hand, if (inside of F2) I DO declare the instance F1, it creates a NEW F1 rather than referring to the one I already have opened. Well, now, that's obvious. That's what you'd have asked it to do. You could SET an instance of F1 to equal the instance you already have, but an event is a far better way to handle this. halhamilton wrote: Seems like this would be very easy, but I can't seem to get it to happen It is very easy, but it's not the way to go. Look up events and delegates, that will put you on the right track. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                H Offline
                H Offline
                halhamilton
                wrote on last edited by
                #7

                Thanks Christian. I'll give that a shot! Hal in AZ

                1 Reply Last reply
                0
                • H halhamilton

                  I'm fairly new to VB.NET, and am almost embarassed to ask this, but... If I already have a form instance open and want to refer back to it, how do I do that? Example --------- Public F1 as New Form1 '(say it has two textboxes 1 and 2) F1.show() 'Fill out TextBox1 Dim F2 as New Form2 F2.show() Now I'm in the F2 instance of Form2. I get some input that I now want to put back into F1. When I try the following in F2 F1.TextBox2.Text="yadayadayada" I get a syntax problem (squiggly underline) unless I re-instantiate F1 like so... Dim F1 as New Form1 This, of course, results in a new F1 instance, not the one I had before. Thanks for any help! If this is in any of my books, I can't seem to find it. Hal in AZ

                  R Offline
                  R Offline
                  rwestgraham
                  wrote on last edited by
                  #8

                  This is a simple solution that wil work (I tested it). 1) Create a public property "MyForm1" in Form2. The property type is Form1. Example: ---------------------------- Private m_Form1 As Form1 Public Property MyForm1() Get MyForm1 = m_Form1 End Get Set(ByVal Value) m_Form1 = Value End Set End Property ---------------------------- 2) Modify the code to set the reference to Form1 in Form2: Example: ---------------------------- Dim F2 As New Form2 'Set reference to Form1 F2.MyForm1 = F1 F2.Show() ---------------------------- 3) In F2 use the reference to access the text box Example: ---------------------------- m_Form1.TextBox2.Text = "yadayadaya" m_Form1.TextBox2.Refresh() ---------------------------- No problem. Robert

                  H 1 Reply Last reply
                  0
                  • R rwestgraham

                    This is a simple solution that wil work (I tested it). 1) Create a public property "MyForm1" in Form2. The property type is Form1. Example: ---------------------------- Private m_Form1 As Form1 Public Property MyForm1() Get MyForm1 = m_Form1 End Get Set(ByVal Value) m_Form1 = Value End Set End Property ---------------------------- 2) Modify the code to set the reference to Form1 in Form2: Example: ---------------------------- Dim F2 As New Form2 'Set reference to Form1 F2.MyForm1 = F1 F2.Show() ---------------------------- 3) In F2 use the reference to access the text box Example: ---------------------------- m_Form1.TextBox2.Text = "yadayadaya" m_Form1.TextBox2.Refresh() ---------------------------- No problem. Robert

                    H Offline
                    H Offline
                    halhamilton
                    wrote on last edited by
                    #9

                    Thanks to Robert and Christian for the help on this. I learned a lot in looking up event subscription, etc. Also, Robert's solution is what I was thinking of, but I couldn't quite see how to implement it. After seeing his solution, it is very clear. I am trying to find a way to check for an existing instance. For example, before declaring Dim F1 as New Form1 I'd like to know if I already have an instance named F1 running. I can't seem to find much to indicate how to do this. Maybe I just don't know how to search the help files that well yet. Is there a managed way to do this? Hal in AZ

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups