Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Old Chestnut

Status
Not open for further replies.

DarkConsultant

Programmer
Dec 4, 2007
156
GB
Hi Again,

I have been searching the Net for a solution to an old chestnut but having tried 4 solutions, all of which did not work, I have arrived at my favourite forum.

In my database app I need to have a form pop up with various bits of info on at varied times, no problem so far BUT I cannot have this form steal focus from the main form which it does at the moment. I have tried the ShowWindow API with absolutely no success and also getting the main form window handle before the info form opens and calling SetActiveWindow from the user32 dll again to no avail (the focus did not switch back to the main form).

I feel I am doing summat fundamentally wrong but cannot fathom it and need help from the kind members of this forum.

TIA

David

DarkConsultant

Live long and prosper \\//
 

Could you show the code you're using for SetActiveWindow, both declaration and calls?



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Hi Buddy,

I made a class ...

Imports System.Runtime.InteropServices
Public Class User32

<DllImport("user32", CharSet:=CharSet.Auto)> _
Public Shared Function GetActiveWindow() As Integer

End Function


<DllImport("user32", CharSet:=CharSet.Auto)> _
Public Shared Function SetActiveWindow(ByVal hwnd As Integer) As Integer

End Function

End Class

... and before the form was shown called ...

Dim MyhWnd as Integer = User32.GetActiveWindow()

... and after the form was shown ...

User32.SetActiveWindow(MyhWnd)

... and ...

nothing happened.

I got this code from the internet (several examples).

I have had some success by declaring the info form on the main form and calling Me.focus after the info form was updated (a listview with reminders on it) but when the update happens there is a noticeable loss of focus, sometimes for a couple of seconds which kinda screws up any data entry.

Any ideas ...

TIA

David

DarkConsultant

Live long and prosper \\//
 

Well, I've tested it and your code works fine.

Does the form being loaded do a lot of processing in the form_load event? If so, hat could be where the delay is occurring. Consider putting any processing in another event, such as Form_Activate, or using a timer like so:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Timer1.Start()

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Timer1.Stop()

'do processing here

End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Hi,

Yep that got it, thanks mate.

I am left with just an annoyance ... before the first line of code runs I get a System.InvalidOperationException. I can halt the app on a global module which seems to be the first run line but the error occurs before this. I have searched for this but I am struggling to find the words to elicit the correct response.

Would you have any pointers?

TIA

DarkConsultant

Live long and prosper \\//
 
MSDN says that this error is: "The exception that is thrown when a method call is invalid for the object's current state."

I'm afraid that without access to all your code I wouldn't even know where to begin.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top