VB2005 App loses focus

App hides then I cant use it any more

Page 1 of 1

3 Replies - 1804 Views - Last Post: 23 August 2007 - 09:34 PM Rate Topic: -----

#1 jontcaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-August 07

VB2005 App loses focus

Posted 22 August 2007 - 02:04 PM

Good Day,
I have an application that will send text out of a serial port if i hold down the "1" key for 2 seconds. I am currently just turning a chime on and off but want to expand this to the ability to turn my amplifier on/off etc. I want this to be running in the background but to respond at any time by just hitting a certain key. The program works great one time when i execute, but goes into hide and i never see it again. Any suggestions for bringing the app back into focus are appreciated.

Public Class frmKeyCatcher
Dim WithEvents serialPort As New IO.Ports.SerialPort

Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles serialPort.DataReceived

txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), _
New Object() {})
End Sub

Public Delegate Sub myDelegate()
Public Sub updateTextBox()
With txtDataReceived
.SelectionColor = Color.Red
.AppendText(serialPort.ReadExisting)
.ScrollToCaret()
End With
End Sub

Private Sub frmKeyCatcher_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
serialPort.Close()
End Sub


Private Sub frmKeyCatcher_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = 49 Then
Timer1.Start()

End If
End Sub

Private Sub frmKeyCatcher_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = 49 Then
Timer1.Stop()
End If
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Show()
Me.BackColor = Color.LightGray
If serialPort.IsOpen Then
serialPort.Close()
End If
Try
With serialPort
.PortName = "COM3"
.BaudRate = 96000
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One

End With

serialPort.Open()
Timer4.Start()
Catch ex As Exception
MsgBox(ex.ToString)
End Try

End Sub

Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Me.Hide()
Timer2.Stop()
End Sub

Private Sub Timer4_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer4.Tick
Try
serialPort.Write("C")
With txtDataReceived
.SelectionColor = Color.Black
.AppendText("C" & vbCrLf)
.ScrollToCaret()

End With
Me.BackColor = Color.white
Catch ex As Exception

End Try
Timer4.Stop()
Timer2.Start()
End Sub

End Class



Is This A Good Question/Topic? 0
  • +

Replies To: VB2005 App loses focus

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: VB2005 App loses focus

Posted 23 August 2007 - 04:45 AM

You could simply use the NotifyIcon control to minimize to the system tray. Then all you need to do is click the icon in the system tray to bring it back into focus.

Then you could use the click or doubleclick event of the control to execute code that will bring your form into focus.

An example from a project I did. This is just the code to bring the the form back into focus, it doesn't take much.
	Private Sub ntfySystemTray_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ntfySystemTray.DoubleClick
		'unhide form and set window state to Normal
		If Me.WindowState = FormWindowState.Minimized Then
			Me.Show()
			Me.WindowState = FormWindowState.Normal
			Me.Activate()
			Me.ntfySystemTray.Visible = False
		End If
	End Sub


Here is how I hid the form using the Move event of the form.
	Private Sub WootTracker_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move

		'if form is minimized then hide applicaton so it doesn't show up in tray
		If Me.WindowState = FormWindowState.Minimized Then
			'show the notify icon
			Me.ntfySystemTray.Visible = True
			Me.Hide()
			DisplayBalloonTip()
		Else
			'show the notify icon
			Me.ntfySystemTray.Visible = False
			Me.Show()
		End If
	End Sub

Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: VB2005 App loses focus

Posted 23 August 2007 - 04:47 AM

The easiest way to do this is to research the following items:

Those 3 things will tell you how to activate the instance of your application. Research them, write some code in an attempt to solve this issue, then if you run into problems you can come here and post your code and someone will gladly help you, but we're not going to do your work for you :)
Was This Post Helpful? 0
  • +
  • -

#4 jontcaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-August 07

Re: VB2005 App loses focus

Posted 23 August 2007 - 09:34 PM

Thanks Jayman,
that works awesome, very enlightening. What i ultimately want to do is use this with a remote so i cant double click. But i love the icon in the system tray and will use that. i am still researching this, thanks again.


Thanks Psycho,
I will research those areas you pointed out, i was also reading on multi threading, but still new at this as you can tell. I will let you know what i come up with.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1