Results 1 to 12 of 12

Thread: [RESOLVED] Passing a new string value to an open form

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Cleveland, OH
    Posts
    41

    Resolved [RESOLVED] Passing a new string value to an open form

    (v2008) I need to pass a new string value (and other data) from a second instance to a form that is already open. I'm setting a new string value as the command line in StartupNextInstance. Here is my code in ApplicationEvents:

    Code:
                
            Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) _
            Handles Me.StartupNextInstance
                strCommandLine = e.CommandLine.Item(0)
                frmStartup.MainText = strCommandLine
                e.BringToForeground = True
            End Sub
    Here is my code in the startup form (frmStartup):

    Code:
        
    Public Property MainText()
            Get
                Return TextBox1.Text
            End Get
            Set(ByVal value)
                TextBox1.Text = value
                TextBox1.Update()
            End Set
    End Property
    During debug, it appears as though TextBox1 does in fact get set to the command line string as expected, but it doesn't display on the form.

    Help!

  2. #2
    Lively Member
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    86

    Re: Passing a new string value to an open form

    Is the application a console app or a form app?

    If it's a form app, just use this:

    Code:
    form2.textbox2.text =

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Cleveland, OH
    Posts
    41

    Re: Passing a new string value to an open form

    It is a form app, but your suggestion does not work. As I have the code written above, the TextBox1.text does get set to the Command Line string as expected when monitoring the value during debug, but it doesn't get displayed on the form when the form is "brought forward".

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Cleveland, OH
    Posts
    41

    Re: Passing a new string value to an open form

    One other piece of info that might help someone help me...if I add a line of code in StartupNextInstance to "show" the startup form instead of "BringToForeground":

    Code:
     
    Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) _
            Handles Me.StartupNextInstance
                strCommandLine = e.CommandLine.Item(0)
                frmStartup.MainText = strCommandLine
                frmStartup.Show()
            End Sub
    ..then I get a second instance of the Startup form, and the second instance does have the text box set to the command line text.

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: Passing a new string value to an open form

    The idea of having a property expose the textbox or it's value upon form2 is the correct way of going about this. Where is your frmStartup variable declared here?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: Passing a new string value to an open form

    Here you go. If you want to recreate this (tested) sample, create a new windows application, and add a 2nd form in there. Add a button control to form1, a textbox control to form2, then run the app.
    Code:
    Public Class Form1
    
        ' Declare instance reference of form2 privately in Form1 code.
        Private _form2Instance As Form2
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            ' Do we have an instance of form2 open already? 
            If (_form2Instance Is Nothing OrElse _form2Instance.IsDisposed) Then
                ' If not, then create one!
                _form2Instance = New Form2
                _form2Instance.Show()
            Else
                ' If so, then set it's text property value.
                _form2Instance.textValue = "Hello World!"
            End If
    
        End Sub
    End Class
    Code:
    Public Class Form2
    
        Public WriteOnly Property TextValue() As String
            Set(ByVal value As String)
                Me.TextBox1.Text = value
            End Set
        End Property
    End Class
    Last edited by alex_read; Jun 10th, 2008 at 01:42 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Cleveland, OH
    Posts
    41

    Re: Passing a new string value to an open form

    Thanks for the reply, alex_read. Your code works great for two forms, but I'm still having a problem because I'm using ApplicationEvents instead a form. So in your example, instead of Form1 I use the same code but in ApplicationEvents:

    Code:
            Private _form2Instance As Form2
    
            Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) _
            Handles Me.StartupNextInstance
    
                ' Do we have an instance of form2 open already? 
                If (_form2Instance Is Nothing) Then
                    ' If not, then create one!
                    _form2Instance = New Form2
                    _form2Instance.Show()
                Else
                    ' If so, then set it's text property value.
                    _form2Instance.TextValue = "Hello World!"
                End If
    
            End Sub
    So I launch the app and start with Form2. Then I launch the app again when it is already running, but I don't want a second instance to open. I want the original instance to get "Hello World".

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing a new string value to an open form

    When you open the original instance of Form2 are you assigning it to that _form2Instance variable? I'll wager not, given that it's private. That code doesn't tell you whether a Form2 instance is open or not. It simply tells you whether _form2Instance refers to a Form2 instance. They are not the same thing.

    Also, that code doesn't allow for the fact that a Form2 instance may have been opened, assigned to _form2Instance and then closed. For that you'd have to do this:
    vb.net Code:
    1. If _form2Instance Is Nothing OrElse _form2Instance.IsDisposed Then
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Cleveland, OH
    Posts
    41

    Re: Passing a new string value to an open form

    But in this case, I know that Form2 is open, because I'm launching the app from a shortcut with a command line knowing that the app is already running with Form2 open. I just want to pass the command line to the TextBox on Form2. That's why I'm using 'StartupNextInstance'. I just want to stuff the command line into the Form2 that is already running when I launch the app a second time.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing a new string value to an open form

    Quote Originally Posted by britt13
    But in this case, I know that Form2 is open, because I'm launching the app from a shortcut with a command line knowing that the app is already running with Form2 open. I just want to pass the command line to the TextBox on Form2. That's why I'm using 'StartupNextInstance'. I just want to stuff the command line into the Form2 that is already running when I launch the app a second time.
    Yes, I realise that. The problem is that you aren't testing whether a Form2 instance is already open. You're testing whether your _form2Instance variable refers to an object. They are not the same thing. If you have opened a Form2 instance but not assigned it to that variable then your code is going to create a new one. You have to make sure that the one you created in the first place gets assigned to that variable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: Passing a new string value to an open form

    Very good point there, have ammended the above to include the Disposed check now for anyone else who might read this. Thanks for pointing that out. Didn't think of that

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  12. #12

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Cleveland, OH
    Posts
    41

    Re: Passing a new string value to an open form

    OK...got it. I stuck one line of code in Form2:

    Code:
     _form2Instance = Me
    Things seem to have fallen into place.

    Thanks for all your help!

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