Question:
Simple visual basic question.?
2007-05-24 05:28:27 UTC
Im tired of searching so I will ask this simple vb.net question here. I have shelled open a web browser and I want to bring my original vb form back to focus in windows after that is done.

me.focus I thought would work but it doesnt.

Thanks
Three answers:
math guy
2007-05-24 06:53:26 UTC
Activate an App by a Partial Window Title Visual basic



'PUT ALL THIS CODE (DECLARATIONS AND FUNCTIONS)

'INTO A .BAS MODULE.



Option Explicit

Option Compare Text



Private Declare Function EnumWindows Lib "user32" _

(ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long



Private Declare Function GetWindowText Lib "user32" Alias _

"GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _

ByVal cch As Long) As Long



Private psAppNameContains As String

Private pbFound as Boolean





'Code for form:





Public Function AppActivateByStringPart(StringPart As String) As Boolean



'PURPOSE: Activates the first window that contains any part of

'of StringPart

'PARAMETERS:

'AppNamePart = Any Part of the WindowTitle for the App



'RETURNS: True if successful (i.e., a running app was found)

'false otherwise (e.g., no running app was found with StringPart

'as part of a title, or an error occurred



'EXAMPLE:

' ActivateAppByStringPart "Microsoft Internet Explorer"

'Will Activate the first running instance of IE it finds,

'even though the window title in most cases does

'not begin with with "Microsoft Internet Explorer"



Dim lRet As Long



psAppNameContains = StringPart

lRet = EnumWindows(AddressOf CheckForInstance, 0)



AppActivateByStringPart = pbFound

'reset

pbFound = False



End Function



Private Function CheckForInstance(ByVal lhWnd As Long, ByVal _

lParam As Long) As Long



Dim sTitle As String

Dim lRet As Long

Dim iNew As Integer



If Trim(psAppNameContains = "") Then

CheckForInstance = False

Exit Function

End If



sTitle = Space(255)

lRet = GetWindowText(lhWnd, sTitle, 255)



sTitle = StripNull(sTitle)

If InStr(sTitle, psAppNameContains) > 0 Then

'we're done, stop looking

CheckForInstance = False

pbFound = True

AppActivate sTitle



Else



CheckForInstance = True

End If

End Function



Private Function StripNull(ByVal InString As String) As String



'Input: String containing null terminator (Chr(0))

'Returns: all character before the null terminator



Dim iNull As Integer

If Len(InString) > 0 Then

iNull = InStr(InString, vbNullChar)

Select Case iNull

Case 0

StripNull = InString

Case 1

StripNull = ""

Case Else

StripNull = Left$(InString, iNull - 1)

End Select

End If



End Function





Or you can try - me.show vbModal

this will force the window to be shown
Pens66
2007-05-24 06:14:53 UTC
'I took various snippets of code I had lying around and put into a simple app.

'This should work for you. Just paste it into a form and run it



Option Explicit



Private Const SWP_NOMOVE = 2

Private Const SWP_NOSIZE = 1

Private Const HWND_TOPMOST = -1

Private Const HWND_NOTOPMOST = -2

Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE







Private Const SW_SHOWNORMAL = 1



Private Declare Function SetWindowPos Lib "user32" _

(ByVal hwnd As Long, _

ByVal hWndInsertAfter As Long, _

ByVal x As Long, _

ByVal y As Long, _

ByVal cx As Long, _

ByVal cy As Long, _

ByVal wFlags As Long) As Long







Public Function SetTopMostWindow(hwnd As Long, Topmost As Boolean) As Long

' Function sets a window as always on top, or turns this off



' hwnd - handle the the window to affect

' Topmost - do you want it always on top or not



On Error GoTo ErrHandler



If Topmost = True Then 'Make the window topmost

SetTopMostWindow = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)

Else

SetTopMostWindow = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS)

'SetTopMostWindow = SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, FLAGS)

SetTopMostWindow = False

End If



Exit Function

ErrHandler:

Select Case Err.Number

Case Else

Err.Raise Err.Number, Err.Source & "+modAPIStuff/SetTopMostWindow", Err.Description

End Select

End Function



Private Sub Form_Load()

SetTopMostWindow Me.hwnd, True

SHELL "C:\Program Files\Internet Explorer\IEXPLORE.EXE"

End Sub
Scott D
2007-05-24 05:38:28 UTC
Try:



.show


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...