Problem with positioning InputBox form in Visual Basic
Hi, I am quite beginner with VB, so please do sorry for such obvious question :D
I have certain problem with positioning InputBox forum in Visual Basic.
I would like to position this form in right-bottom corner of my screen, but I cannot achive this. I would like to do this dinamicly, so it fit by itself whatever screen resolution or size client has.
The code I am using is quite simple:
Code:
Public Sub inputNumber()
Dim Number As Double
On Error GoTo ErrorHandler
Number = InputBox("Please insert Number", "NA", "", 0, 0)
Select Case Number
Case Is > 0
MsgBox "Number is bigger then 0"
Case Is < 0
MsgBox "Number is smaller then 0"
Case Else
MsgBox "Number = 0"
End Select
ErrorHandler: MsgBox "You have not provided number"
End Sub
Now, the Input Box form is positioned at the left-top corner.
Can anyone help me with this problem?
Thanks in advance
Re: Problem with positioning InputBox form in Visual Basic
you need to figure out the screen resolution of the user's screen... then figure out how big your input box is... then adjust accordingly.
It's putting it in the top left because that's where you told it to go:
Number = InputBox("Please insert Number", "NA", "", 0, 0)
That's what the 0,0 on the end it is for. Replace them with the appropriate values, and it will go where you tell it to.
Also, it's "bigger than 0" and "smaller than 0" .... "than", not "then"... "then" is time-based. "It happened then that the number is bigger than 0."
-tg
Re: Problem with positioning InputBox form in Visual Basic
@techgnome
Thanks for language tipp :D and for helping out
Anyway, my problem is all about figuring out user/client resolution automaticly.
Is there any automated way like in CSS where You can say somthing like:
Number = InputBox("Please insert Number", "NA", "", Right, Bottom)
So, I would like that regarding of user screen resolution my form always comes to right-bottom angle of the screen.
Re: Problem with positioning InputBox form in Visual Basic
You can position an inputbox to any coordinates on the screen using the X,Y parameters. However, the catch, in your case, is that you need to know the size of the inputbox so that you can calculate the coords from 3 of the 4 corners of the screen. Unfortunately, I don't know of any way to get the inputbox size easily. Most people that need to do this, do one of 2 things: 1) Create their own inputbox using a VB form where they have full control, or 2) create a message hook, trap the creation of the inputbox, change its coordinates at that point, then release the hook. Hooks in VB are somewhat advanced.
Re: Problem with positioning InputBox form in Visual Basic
Even getting the screen size enough any more you also have to deal with the taskbar too... otherwise you're inputbox is going to get caught behind it.
Is there a reason to put it in such an odd place, rather than up in the center, or near the rest of the user interface?
-tg
Re: Problem with positioning InputBox form in Visual Basic
Quote:
Originally Posted by
techgnome
Even getting the screen size enough any more you also have to deal with the taskbar too... otherwise you're inputbox is going to get caught behind it.
Is there a reason to put it in such an odd place, rather than up in the center, or near the rest of the user interface?
-tg
Or maybe aligned with the top/left of your form?
Re: Problem with positioning InputBox form in Visual Basic
Heres a dirty way to move the InputBox to the lower right corner of the desktop, ...
' FORM CODE
Code:
' // Only works when compiled! (Or use an API Timer inplace of VB Timer.) //
Option Explicit
Dim sTitle As String
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 30
End Sub
Private Sub Command1_Click()
'// get user name from inputbox //
Dim sUserName As String
' inputbox title
sTitle = "Your Name?"
' start watching for our input box
Timer1.Enabled = True
'Open inputbox off screen ;)
sUserName = InputBox("Please Enter Your Name.", sTitle, "", -Screen.Width, -Screen.Height)
' show result
MsgBox "Welcome " & sUserName
End Sub
Private Sub Timer1_Timer()
Dim WinHwnd As Long
' find our input box
WinHwnd = FindWindow("#32770", sTitle)
If WinHwnd <> 0 Then
Timer1.Enabled = False ' stop timer!
SetWinPos WinHwnd ' move inputbox window
End If
End Sub
' MODULE CODE
Code:
Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Private Const SPI_GETWORKAREA = 48
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Sub SetWinPos(WinHwnd As Long)
' // Move window to lower right corner //
Dim L As Long, T As Long, W As Long, H As Long
Dim DeskTop As RECT, TheWindow As RECT
SystemParametersInfo SPI_GETWORKAREA, 0&, DeskTop, 0&
GetWindowRect WinHwnd, TheWindow
W = TheWindow.Right - TheWindow.Left
H = TheWindow.Bottom - TheWindow.Top
T = DeskTop.Bottom - H
L = DeskTop.Right - W
Call MoveWindow(WinHwnd, L, T, W, H, 1)
End Sub
Re: Problem with positioning InputBox form in Visual Basic
EdgeMeal; sneaky and apparently effective. :thumb:
However, I don't believe it will work until app is compiled; not with VB timers anyway. Modal windows block timers while in IDE.
Re: Problem with positioning InputBox form in Visual Basic
Quote:
Originally Posted by
LaVolpe
EdgeMeal; sneaky and apparently effective. :thumb:
However, I don't believe it will work until app is compiled; not with VB timers anyway. Modal windows block timers while in IDE.
I mentioned that in the code, API timer should solve that though right?
Re: Problem with positioning InputBox form in Visual Basic
Quote:
Originally Posted by
Edgemeal
I mentioned that in the code, API timer should solve that though right?
Ah so you did, I must just be tired. API timer sent to a timer procedure should work just fine. Another option could simply be to test if in IDE and not try to position it while in design vs compiled.