1

I'm calling a vb.net exe from a program in java, and i want it to stay behind all of the already opened programs.

I read that you can kinda bring to front with TopMost set to true , but i find no way to put it in the back.

There's some workarounds, like setvisible=false the previous form and setting to true after that, but that isn't quite good with my application. Thank you!

noidea
  • 184
  • 5
  • 22
  • In the load event of your program, have you tried [Control.SendToBack](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.sendtoback(v=vs.110).aspx)? – Trevor May 17 '16 at 14:41
  • Yep. And when i click the button on the java program it still comes in front of it – noidea May 17 '16 at 14:47
  • There's gotta be some native call you can make from the vb app. [pinvoke.net](http://pinvoke.net/) – RoyalPotato May 17 '16 at 14:52
  • http://stackoverflow.com/a/157843/5981756 Will this suffice? Or do you specifically want to keep the form in the back? EDIT: Sorry, re-read your question. You already said that you do want to specifically keep it in the back. – RoyalPotato May 17 '16 at 14:54
  • @RoyalPotato what about something built in the visual studio ? i'm using data and i can't test that because of the download stuff – noidea May 17 '16 at 14:59

1 Answers1

2

Calling the form's SendToBack (Inherited from Control) method from the Shown event handler is the key. Calling it from load for example, won't do anything since the window isn't "ready" yet anyway.

Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
    SendToBack()
End Sub

Alternatively, use the SetWindowPos native function as defined in User32.dll.

Imports System.Runtime.InteropServices

...

'Signature source: http://pinvoke.net/default.aspx/user32/SetWindowPos.html
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
End Function

...

Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
    SetWindowPos(Handle, New IntPtr(1), Location.X, Location.Y, Width, Height, 0)
End Sub

The only thing I changed from the signature provided by PInvoke.net was that I changed the "uFlags" parameter type from "SetWindowPosFlags" (Also defined on the PInvoke.net) to an Integer.

RoyalPotato
  • 515
  • 4
  • 15
  • 1
    Good explanation ! Thank you ! Idk why no one have ever made this question honestly. – noidea May 17 '16 at 15:16
  • 1
    Note that you can use the native function to determine if it failed or not since it returns a Boolean (True for succeed, False for fail). You can use GetLastError (https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360%28v=vs.85%29.aspx) (PInvoke.net: http://pinvoke.net/default.aspx/user32.GetLastError) to get more detailed info on why it failed. – RoyalPotato May 17 '16 at 15:32