1 Attachment(s)
Extended InputBox Function
I made the following function because Kleinma was asking about why the inputbox function wouldn't allow dialog results and wouldn't allow the prompt message to get bigger like it did in VB6...one of those things that M$ seemed to forget. so i wrote my own version on the InputBox function and made it much like the MessageBox.Show function...in fact i named it InputMessageBox.Show... if you download the source do whatever you'd like...i'm going to try and add more functionality to it in the future...like adding icons...being about to validation...etc...but for now here's the basic with a TestBed project to see how it works...one thing i would suggest is not to change anything in the ComputerLabelHeight routine...feel free to...just a suggestion. well here it is hope you like it :afrog:
1 Attachment(s)
Re: Extended InputBox Function
Just wanted to let you know that I love your add-on!
Also, I have updated it a little bit, and yes, I did do a major revision to your ComputeLabelHeight function.
In my program, I format my message with vbNewLine's quite a bit, and your InputBox worked great, except it wouldn't show anything past 5 lines.
After a bit of playing around with the numbers, I came up with the following:
Code:
Private Sub ComputeLabelHeight(ByVal sz As String)
Dim fnt As New Font(Me.lblPrompt.Font.Name, Me.lblPrompt.Font.Size)
' Compute the string dimensions in the given font
Dim bmp As Bitmap = New Bitmap(1, 1, Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim gph As Graphics = Graphics.FromImage(bmp)
Dim lineHeight As Integer = CInt(Me.lblPrompt.Font.Height * 1.25)
Dim stringSize As SizeF = gph.MeasureString(sz, fnt)
Dim lines As Integer = CInt((Math.Floor(stringSize.Width) + ((Split(sz, vbNewLine).Length) * Me.lblPrompt.Width)) / Me.lblPrompt.Width)
Me.lblPrompt.Height = (lines * lineHeight)
Me.Height = Me.lblPrompt.Top + (lines * lineHeight) + 72
gph.Dispose()
bmp.Dispose()
End Sub
This will allow any number of lines to be shown in your InputBox. I've also set the form's minimum height to 30 (just below the Cancel button).
In addition, I've also appended to the end of your .vb file two "flavors" of the traditional InputBox. They are both detailed below:
Code:
Module InputMessageBoxFunctions
Private Const CANCEL As String = Chr(24)
'Function: myInputBox
'Purpose: Provides the user with a VB.NET InputBox with one exception:
' it can tell the user whether or not the Cancel button has been
' pressed.
' The difference between this function and the VB.NET InputBox
' is that the VB.NET InputBox only returns "" (aka String.Empty or
' Nothing) when the Cancel button is pushed whether or not anything
' has been entered into the InputBox. It wasn't made clear enough
' to me when the Cancel button was pressed, hence this function.
'Parameters: Takes same parameters as a VB.NET InputBox
'Returns: If OK was pressed, will return the text typed in.
' If Cancel was pressed, it will return the "Cancel" ASCII character
Public Function myInputBox(ByVal Prompt As String, Optional ByVal Title As String = "", Optional ByVal DefaultResponse As String = "") As String
Dim result As InputDialogResults.InputMsgResults = InputMessageBox.Show(Prompt, Title, DefaultResponse)
If result.DialogResult = InputDialogResults.InputMsgResults.InputDialogResult.OK Then
myInputBox = result.Response
Else
myInputBox = CANCEL
End If
End Function
'Function: InputBox
'Purpose: Extremely similar to the VB.NET InputBox, except that upon the
' user clicking the Cancel button, rather than returning "" (aka
' String.Empty or Nothing), this will return the Default Response.
'Parameters: Takes same parameters as a VB.NET InputBox
'Returns: If OK was pressed, will return the text typed in.
' If Cancel was pressed, it will return the Default Response.
Public Function InputBox(ByVal Prompt As String, Optional ByVal Title As String = "", Optional ByVal DefaultResponse As String = "") As String
InputBox = myInputBox(Prompt, Title, DefaultResponse)
If InputBox = CANCEL Then
InputBox = DefaultResponse
End If
End Function
End Module
I appreciate your add-on very much! I'm glad someone finally put the time in to make one. (Thank you!)
I have attached the updated file for those of you who'd rather not copy/paste.
Enjoy!
Re: Extended InputBox Function
Glad someone likes it. I will have to update my version with the updates you added. Again thanks for the complements :thumb: