Results 1 to 10 of 10

Thread: Problem with positioning InputBox form in Visual Basic

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    6

    Problem with positioning InputBox form in Visual Basic

    Hi, I am quite beginner with VB, so please do sorry for such obvious question

    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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    6

    Re: Problem with positioning InputBox form in Visual Basic

    @techgnome
    Thanks for language tipp 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.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Problem with positioning InputBox form in Visual Basic

    Quote Originally Posted by techgnome View Post
    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?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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
    Last edited by Edgemeal; Feb 25th, 2010 at 10:30 PM.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Problem with positioning InputBox form in Visual Basic

    EdgeMeal; sneaky and apparently effective.

    However, I don't believe it will work until app is compiled; not with VB timers anyway. Modal windows block timers while in IDE.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Problem with positioning InputBox form in Visual Basic

    Quote Originally Posted by LaVolpe View Post
    EdgeMeal; sneaky and apparently effective.

    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?

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Problem with positioning InputBox form in Visual Basic

    Quote Originally Posted by Edgemeal View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width