I'm trying to emulate sending a "CTRL-N" to a form in a Windows app after checking for a previous instance of the application. Once I determine if the users has the existing application up, I activate the current window, send a keystroke and then close the app that had the previous instance when launched. When the AppActivate runs, I can see that its setting focus to the app, but the control key combo doesn't work. If I do it manually, it works fine. Here's my code:
If blnPrevInstance Then 'Activate Main Form window of existing app
.
.
.
Try
AppActivate("FiberNet Reporting") 'This is the name of the title bar in the form that is already active
SendKeys.Send("^N") ' I also tried SendKeys.Send("^(N)")
Catch exException As ArgumentException
.
.
.
Finally
Me.Visible = False
CloseForm()
Cursor.Current = System.Windows.Forms.Cursors.Default
Application.Exit()
End Try
End If
Why is it not working

Sending keystrokes...
Deepak_SQL
SendKeys.Send(Chr(14))
Butt3r5
From the Visual Studio .NET documentation (SendKeys class):
"The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses () have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use {+}. "
I haven't done this in .net yet myself, so it's just a quickie answer to see if this will solve your problem.
julie
enid1229
Here is my code:
If blnPrevInstance Then 'Activate Main Form window of existing app
Dim strErrMsg As String
Dim strAppName As String = "FiberNet Reporting"
Dim blnWeb As Boolean = False
Dim i As Integer
Try
Me.Visible = False
CloseForm()
Cursor.Current = System.Windows.Forms.Cursors.Default
AppActivate("FiberNet Reporting")
Select Case strAutomatedMenuItem
Case "A"
SendKeys.Send(Chr(1))
Case "N"
SendKeys.Send(Chr(14))
Case "F"
SendKeys.Send(Chr(6))
Case "Y"
SendKeys.Send(Chr(25))
Case "L"
SendKeys.Send(Chr(12))
Case "S"
SendKeys.Send(Chr(19))
End Select
Catch exException As ArgumentException
Dim strBuilder As New StringBuilder()
Dim clsLogEntry As New LogPLLEntry()
clsLogEntry.LogMessage(exException.Message(), exException.Source, exException.StackTrace, exException.TargetSite, exException.GetType.ToString, strAppName, EventLogEntryType.Error, strBuilder, blnWeb)
strErrMsg = strBuilder.ToString
clsLogEntry = Nothing
strBuilder = Nothing
MessageBox.Show(strErrMsg, "frmSignon_Load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
Finally
Application.Exit()
End Try
Else
Me.Show()
StatusBar1.Text = "Validating user..."
Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
blnCheckUserName = CheckUserName()
Cursor.Current = System.Windows.Forms.Cursors.Default
End If
According to the AppActivate function, it doesn't take care of maximizing or minimizing a form. Is there any way for me to somehow maximize the form via code I tried the "me.WindowState = FormWindowState.Maximized", but the problem is, is that I don't know how I can reference the form after I issue the Appactivate statement and before I send the keycode value in order to actually issue a command against it.
nunuk
"^(N)", so there is no question about that. I just don't know what is capturing the CTRL-N. Have you tried to sendkeys CTRL-N to make the application close itself
Well, here is what did work.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AppActivate("AppDomainTest AppTwo")
SendKeys.Send("%({F4})")
End Sub
julie
Steve Wertz
Alex Chertov
Well, now you have intrigued me with this problem!
If these are both .net applications, i.e. both managed code, you should be able to get both apps to talk to each other.
Probably when you manually hit the control-N keys, windows is the first to catch that and sends it to the active application. But when you do the SendKeys from one application domain, it doesn't know that you want to send the keystrokes OUTSIDE of the domain, and therefore it doesn't have a clue about what is the "active application".
I have not done any work with application domains yet, but here is a resource that might be useful
http://www.gotdotnet.com/team/clr/appdomainfaq.aspx
Of course,I can't help wondering if the active app that you are trying to send the keys to is the VB6 app that you were talking about in a different thread. In that case, I believe there are still means within the managed code to talk to the unmanaged (VB6) code.
Hopefully, someone else will come along to this thread and be able to tell you a few simple lines of code to accomplish this task. But until then, you might want to dig around in the app domain stuff.
Man, there is just TOO much to learn in .NET!!!
julie
nougat
UtterMan
I knew there was going to be a "keep it simple/stupid" solution to what you were trying to do.
julie
DotFrammie
1) The .Net application is already active.
2) The user issues a command to start the application again. If the application is already active, I then try to activate the other application, issue the Sendkeys command and then close the application that they tried to start.
I can't get it to work..........
MJC_Eagle
<quote>
Man, there is just TOO much to learn in .NET!!!
</quote>
Amen! That's what makes it so much fun! :)