|
-
Jun 8th, 2008, 12:23 PM
#1
Thread Starter
Member
[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!
-
Jun 8th, 2008, 12:26 PM
#2
Lively Member
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 =
-
Jun 8th, 2008, 07:05 PM
#3
Thread Starter
Member
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".
-
Jun 8th, 2008, 07:53 PM
#4
Thread Starter
Member
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.
-
Jun 9th, 2008, 12:58 AM
#5
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?
-
Jun 9th, 2008, 01:04 AM
#6
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.
-
Jun 9th, 2008, 06:29 AM
#7
Thread Starter
Member
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".
-
Jun 9th, 2008, 08:57 AM
#8
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:
If _form2Instance Is Nothing OrElse _form2Instance.IsDisposed Then
-
Jun 9th, 2008, 09:32 AM
#9
Thread Starter
Member
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.
-
Jun 9th, 2008, 08:43 PM
#10
Re: Passing a new string value to an open form
 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.
-
Jun 10th, 2008, 01:44 AM
#11
-
Jun 10th, 2008, 06:47 AM
#12
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|