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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input box as variable?

Status
Not open for further replies.

Huitzilopochtli

Programmer
Feb 18, 2002
81
DE
I would appreciate some help on this one:

Is it possible to position an InputBox (Left and Top values) by declaring an InputBox as a variable? For example:

Dim InputBoxLeft as Single (or whatever)
Dim InputBoxTop as Single (or whatever)

If it is possible, which values would I use to position the InputBox in the centre of the screen and in the lower half of the screen?

Many thanks!
 
Option Explicit

Private Sub Command1_Click()
Const BOXHEIGHT = 145& ' in pixels (may differ per machine)
Const BOXWIDTH = 363& ' in pixels (may differ per machine)

Dim s As String
Dim x As Long
Dim y As Long

Dim screenheight As Long
Dim screenwidth As Long
Dim tppX As Long
Dim tppY As Long

screenheight = Screen.Height
screenwidth = Screen.Width
tppX = Screen.twipsperpixelX
tppY = Screen.twipsperpixelY

x = (screenwidth - (BOXWIDTH * tppX)) / 2
y = (screenheight - (BOXHEIGHT * tppY)) / 2

' center horizontally and vertically
s = InputBox$("prompt", "title", "default", x, y)

' center horizontally; center vertically at lower half of screen
y = (((screenheight / 2) - (BOXHEIGHT * tppY)) / 2) + (screenheight / 2)
s = InputBox$("prompt", "title", "default", x, y)

End Sub
 
Hello JustinEzequiel

Very many thanks for your reply.

I have pasted in the code - and it works, thank you - except that the InputBox is only positioned centrally once the InputBox itself has been clicked. The box doesn't centre when it first appears (as a result of, in the case you kindly offered, clicking on the cmd button on the main form). In other words, the box centralises after two clicks.

I am sure the answer lies in your code, however, so I will do a little bit of work on it.

Very many thanks again, Justin
 
was not sure what you wanted so I displayed the input box twice.
the first time at the center of the screen.
the second time at the center of the lower half of the screen.
if as you say what you wanted is the second then do as I did below and comment out the first
Code:
y = ...
and the first
Code:
s = InputBox$...

Code:
Option Explicit

Private Sub Command1_Click()
  Const BOXHEIGHT = 145& ' in pixels (may differ per machine)
  Const BOXWIDTH = 363& ' in pixels (may differ per machine)
  
  Dim s As String
  Dim x As Long
  Dim y As Long
  
  Dim screenheight As Long
  Dim screenwidth As Long
  Dim tppX As Long
  Dim tppY As Long
  
  screenheight = Screen.Height
  screenwidth = Screen.Width
  tppX = Screen.twipsperpixelX
  tppY = Screen.twipsperpixelY
  
  x = (screenwidth - (BOXWIDTH * tppX)) / 2
'  y = (screenheight - (BOXHEIGHT * tppY)) / 2
  
  ' center horizontally and vertically
'  s = InputBox$("prompt", "title", "default", x, y)
  
  ' center horizontally; center vertically at lower half of screen
  y = (((screenheight / 2) - (BOXHEIGHT * tppY)) / 2) + (screenheight / 2)
  s = InputBox$("prompt", "title", "default", x, y)
  
End Sub
 
Hello Justin

Very many thanks again for your message.
I have tried it and it works just a treat. I know it is to come up soon in an exam I have so sit, so I owe you a double thanks. If you were nearer I'd buy you a drink!

Many thanks again, mate, and best wishes

 
How can I specify in an input box prompt a variable? For example...
For i = 1 to 5
intHours = InputBox("How many hours in day" i)
Next i

How do you put a variable in for i? Can you do it with an extra line of code?
Would really appreciate the help! Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top