VB.NET sendkey function - cant Minimize the application - vb.net

i know that sendkeys function is not the right way to change text of text box
i just want to try the function although that this function is not Effective
lets say that i got form with textbox and timer
Timer1.Interval = 1000
Timer1.Tick:
textbox1.Focus()
sendkeys.Send("123")
now evrey second the timer send keys into my textbox.
if i Minimize My Application the application send keys to EvreyWhere
lets say that i open NotePad And Then Run My Application the application send the keys into notepad and not my application
how can i send keys only to my application?

The problem is once your application is minimized it is no longer the active application. SendKeys sends to the Active Application. Take a look at this Forum Link for possible options
From above link:
Use SendKeys to send keystrokes and keystroke combinations to the
active application. This class cannot be instantiated. To send a
keystroke to a class and immediately continue with the flow of your
program, use Send. To wait for any processes started by the keystroke,
use SendWait.

Related

How to check if VBA application is in focus when GetAsyncKeyState is pressed

I have a long running application written in VBA. Because it runs for a long time I call a sub from the main loop every iteration to check if the user has pressed the escape key. The sub is listed below. The code works great except that it is always listening, even when the VBA application does not have the focus. What's the best way to approach this? Is there an alternative to GetAsyncKeyState which only listens when the correct application is in focus, or are there system calls I can use to check that the correct window is in focus.
Private Sub checkForUserEscKeyAbort()
'Listen for escape key and exit gracefully if user aborts.
Dim abortResult As VbMsgBoxResult
If GetAsyncKeyState(vbKeyEscape) Then
abortResult = MsgBox("Escape key pressed, do you want to abort?", vbYesNo)
If abortResult = vbYes Then Call teardownSLICER
End If
End Sub
The problem is the way GetAsyncKeyState operates. It does more than just check it the key is currently down, it also checks if the key was pressed since the last time GetAsyncKeyState was called. Thus, your problem of "always listening".
You could use GetKeyState, but frankly, I'm not a fan of that option either. Your code must be well crafted to avoid a polling window that is so small that you literally have to hold the key down to ensure it isn't missed.
A viable alternative is key masking where you use a combination of keys such as Shift-Escape. There is a decent introduction to this (and many other subjects) on Chip Pearson's site. However, this is still not my preferred method.
My preference has already mentioned in the comments. Your application may best be improved with a user form. It also gives you the ability to get information in front of the user. Perhaps they wouldn't try quitting the application if a progress bar on the user form indicated 95% completion. Maybe you can add a pause button that free's some resources for a prescient need and then resumes when the user is ready. That little extra functionality on its own is enough to win me over but there is an even better reason for using a User Form - it does exactly what you asked!
User forms, and many user form controls, have Keydown, Keyup, and Keypress events that only trigger when the form (or control) have focus. This is precisely what you wanted.

Keep VB.NET application running in the background after base form closed

I am writing an application in VB.Net in which a user can schedule emails to be sent in the future. Is there a way to keep my listener thread running in the background after the base form on the application is closed? I would also need to start up the listener when the system boots, but wouldn't want any forms to open at that time.
Example (desired) functionality: I open the application in the morning and schedule three emails, one of which should be sent in three hours, and the other two of which should be sent tomorrow morning. I close the application. In three hours, the first one sends. At the end of the day, I shut down my machine. When I turn it back on tomorrow morning, the other two are sent without me ever opening the application.
I am thinking I need to separate the listener into a service, but want to make sure there isn't an easier way before going down that road.
If I do end up having a separate service and application, can I create an installer that will install both at once?
In the options of your project, set the “Startup Object” to “Sub Main” instead of of a form name. You might have to disable the setting “Application Framework” first.
Now you have to launch the form manually because it’s no longer done automatically, but on the other hand you can now control when to launch it, and your application will exit when it reaches the end of the Main method (or when it’s quit explicitly) rather than when the form closes.
If all you want is to keep the program running, you could supply it with the definition for a TaskBar icon, and represent state in your application using the icon's graphic. It would appear over on the right next to the system clock.
Then it's just a matter of reconfiguring your development project so that the application close event is triggered off that icon's menu instead of off the fact that the form is closed.
Use a NotifyIcon control and when the user close the mainform capture the OnClosing Event to Ask the Question if he want to minimize to the tray or exit the application.
Here is the documentation of NotifyIcon control from Microsoft:
NotifyIcon Control
I have a possible solution for you here. In the form closing event you can place the following code:
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.Visible = False
e.Cancel = True
End Sub
This will hide the form and allow it to run in the background until you shutdown the computer. To handle the restart and then send emails I'd suggest adding the emails to be sent to a text file that can be accessed after restart and then adding the program to startup. An even better solution might be to have 2 programs, one that is on startup and is always hidden that checks the text file every 15 minutes (for example) and a second program that is launched when a new email is to be added to the list and it appends the text file with new emails to be sent.
(Sorry if i am wrong, just proposing a probable solution here)
I think attaching a simple db to hold the email sending schedule information/date-time and loading the application at startup with Notification Icon (or without Notification Icon but with a shortcut to show user interface/form to feed the emails and schedule information/date-time, if required + BackgroundWorker, if you want the application to go to sleep mode and wake it later to send first scheduled email; after X hours). After sending first email update the db then exit the application. Check the db on startup to send the first email or go to sleep mode if pc boots before sending first email. But, if first email is already sent then send other two scheduled emails (check db date-time). This should do the job...
:)

vb.net sendKeys to my application when application is minimized

i want to send keys into my from when my form is minimized and Not in focus
SendKeys.Send("{TAB}")
works but only when my form is on focus.
and this is very not convenient, if the user want to minimize the application then the application send the keys to everywhere, i want that the application send the keys only to my application.
So I need a function that looks like this:
Dim wb As New WebBrowser
wb.Navigate(urll)
WaitToWb()
'Send Keys To WebBrowser
SendKeysToBrowser(Me.Handle, wb, "{TAB}")
i need that the function send the keys only to my webbrowser, if my application is minimized the keys send only to the webbrowser control.
there is a function that can send keys only to my application when my application is minimized?

Msgbox Wait until, VB.NET

Is it possible to have a Msgbox without a button on it in a console application. I would like to have a msgbox pop up and then disappear when the task has been completed. Or could I send the msgboxresult to some form of window that would just disappear when the file has been written?
MsgBox("The users on the domain are being gathered. A prompt will appear when all information has been gathered.")
Dim userFile2 As String = savefileDialog1.FileName & ".txt"
Dim fileExists2 As Boolean = File.Exists(userFile2)
Using sw As New StreamWriter(File.Open(userFile2, FileMode.OpenOrCreate))
For Each d As DirectoryEntry In de.Children()
sw.WriteLine(d.Name)
Next
End Using
If you want to have a console app with a GUI, I'd suggest that it might be easier to just make a WinForms app. Just create a new tiny WinForms app, make the default Form small as a dialog box and make it have only one invisible Close button.
Then you can just show the Close button when it's finished.
Just remember to disable the Control box on the Form (the X up in the top right hand corner) and handle any keyboard combination that could close it.
Edit: Or if for any reason you have to have it as a Console app, then you could still write a tiny separate app that just does the GUI part that you need and have the Console app start up the GUI app, sending over the text to display.
First, a quick point. A MsgBox is a modal dialog so will halt execution until the user responds, you can't use this.
In general "console" applications should be non-graphical.
You don't want to use a console application to disiplay a window. Since .Net 4.0 is available to you may want a WPF application that can write to the console.
There is a post about outputting to the console with WPF here on SO.
You should do your work on a different thread perhaps using a System.ComponentModel.BackgroundWorker. This will allow the Window to respond to user interaction and render while your task progresses.

Can not get Process to die in VB.net

I've inherited some VB.net code. My task is to find out why it isn't working. I have 2 applications. The first one
is run as a service, infinitely checking a table to see if there are any tasks to be handled. If it finds one, its supposed to fire off the second application to handle the task then returns to the loop and checks for another. Both these applications are forms but they do not show any windows. The problem I'm having is after the second application is finished, the first application never gets a signal it is done so it is waiting forever, thus it can't move onto the next task. If I go into TaskManager and kill the second application, the first one gets that notification and proceeds as it should. Below is how I am creating the process and waiting for it. I've tried several different ways of creating and waiting for the process (using a Shell/OpenProcess, WaitForSingleObject,etc) and I can't get it to work. I've searched all over the internet, StackOverflow and the MSDN site but nothing I've tried works. I've been messing with this for 2 days!!
Form 1 Load:
Dim ProcessProperties As New ProcessStartInfo
ProcessProperties.FileName = strExeFullPath
ProcessProperties.Arguments = " /project " & l_project
ProcessProperties.CreateNoWindow = True
Dim myProcess As Process = Process.Start(ProcessProperties)
myProcess.WaitForExit()
When Form2 is finished, it does a Me.Close() and Exit Sub in the load subroutine but the process is still showing in the TaskManager and never returns to Form1 so Form1 is in WaitForExit forever. I've tried closing every open file and connection and setting them to Nothing in Form2, Me.Dispose,etc. I've tried Application.Exit as the last line of Form2. That stupid thing will not die!! Why won't it DIE!!??!!
What am I missing?
If I go into TaskManager and kill the second application, the first one gets that notification
Keep your eyes on the ball, the real problem is that this second application is not exiting by itself. And thus myProcess.WaitForExit() isn't going to return. So this is not a problem in your code snippet.
Why the 2nd app doesn't want to quit is completely unclear from your question. Given that it is a Windows Forms app, do keep in mind that there is nobody to click the Close button of the form. Application.Exit() should make it stop, Environment.Exit() is a rude abort that cannot be veto-ed by a FormClosing event handler.
Anyway use this:
ProcessProperties.Arguments = String.Format("/project {0}", 1_project)
No leading space is required and code becomes more readable.
Cheers!
I suspect Form2 is trying to show some modal dialog (maybe a message box, maybe an unhandled exception box) before quitting. Since App2 is launched by App1, which is a service, Form2 cannot interact with the desktop and just sits there waiting for a button click that will never happen.
Try to allow the App1 service to interact with the desktop (you can find that option on the Log On tab of the service properties dialog box) and check if Form2 actually pops up a dialog before quitting.