Page 1 of 1

Custom Inputbox Create your own inputbox. Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2236
  • View blog
  • Posts: 9,407
  • Joined: 29-May 08

Posted 07 May 2009 - 12:45 AM

Your Own InputBox

In VB6 you could use the InputBox for inputting text, like so.
  Dim temp7 As String = InputBox("Enter the album name.")


But it has its limitations;-
Cancel returns "", which maybe a valid input, so making it nigh on impossible to detect the cancel.

Now we are using vb.net let start on step away from using the old vb6 functionality.
It is really simple to create your own.

All you need is
  • A Textbox for the input text. (Txt_TextEntry)
  • A Label for the prompt text. (Lbl_Prompt)
  • A Button for Cancel (But_Cancel)
  • A Button for OK (But_Ok)
Here mine
Attached Image

A little bit of code.
Imports System.Windows.Forms

Public Class InputBox

 Protected m_BlankValid As Boolean = True
 Protected m_ReturnText As String = ""

 Public Overloads Function ShowDialog( _
   ByVal TitleText As String, _
   ByVal PromptText As String, _
   ByVal DefaultText As String, _
   ByRef EnteredText As String, _
   ByVal BlankValid As Boolean) As System.Windows.Forms.DialogResult
  m_BlankValid = BlankValid
  Me.Lbl_Prompt.Text = PromptText
  Me.Text = TitleText
  Me.Txt_TextEntry.Text = DefaultText
  Me.ShowDialog()
  EnteredText = m_ReturnText
  Return Me.DialogResult
 End Function

 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txt_TextEntry.TextChanged
  If Me.Txt_TextEntry.Text = "" Then
   Me.But_Ok.Enabled = m_BlankValid
  Else
   Me.But_Ok.Enabled = True
  End If
 End Sub

 Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Ok.Click
  Me.DialogResult = System.Windows.Forms.DialogResult.OK
  m_ReturnText = Me.Txt_TextEntry.Text
  Me.Close()
 End Sub

 Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Cancel.Click
  Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
  m_ReturnText = ""
  Me.Close()
 End Sub
End Class



Usage is not a simple as vb6 but hey we got a lot more capabilities.

Dim TextReturned As String="" 
Dim a As New InputBox
If a.ShowDialog("The Title", "The Prompt", "Default", TextReturned , False) = Windows.Forms.DialogResult.Cancel Then 
' Cancel Pressed
Beep()
Else
'
Endif



You can create different types of inputbox, for example;- password, numbers only, masked etc.

Simples

So now you're a custom InputBox building coding pirate. :pirate:

Is This A Good Question/Topic? 1
  • +

Replies To: Custom Inputbox

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2236
  • View blog
  • Posts: 9,407
  • Joined: 29-May 08

Posted 19 June 2010 - 12:59 PM

An update to the tutorial.
By also adding in this bit of code.

 Public Overloads Shared Function Show(ByVal TitleText As String, ByVal promptText As String, ByRef TextInputted As String, Optional ByVal IsEmptyValid As Boolean = True) As System.Windows.Forms.DialogResult
  Dim tmp As New InputBox
  Return tmp.ShowDialog(TitleText, promptText, TextInputted, IsEmptyValid)
 End Function



You can now do

Dim result = InputBox.Show("The Title", "The Prompt", "Default", TextReturned , False)

Was This Post Helpful? 0
  • +
  • -

#3 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Posted 04 February 2012 - 03:22 AM

Hello! first of all, Thanks for this tutorial. It suits exactly what I wanna do right now. But I've hit a snag and kinda stuck with it. I hope you could clear out what I'm missing here.

Okay here's the scenario. I've a Main form. When it loads, a custom inputbox with only a DateTimePicker pops up to get the date.


Posted Image

Here's the code for the DateTimePicker inputbox

Public Class frmdeliverDate

    Protected returnVal As String

    Public Overloads Function ShowDialog(ByRef deliver As String) As System.Windows.Forms.DialogResult

        deliver = returnVal
        Me.ShowDialog()
        Return Me.DialogResult

    End Function

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        Me.DialogResult = Windows.Forms.DialogResult.OK
        returnVal = dtpDeliver.Value.ToString("dd/mm/yyyy")  
        'the variable returnVal gets the date value as a string as expected
        Me.Close()

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        returnVal = ""
        Me.Close()

    End Sub

End Class



As I've commented, the returnVal gets the date string but when I call the .DialogResult from my Main Form, it returns "1". Below is the code from my Main Form


Imports System.Windows.Forms

Public Class frmMain

	Dim frmdeliverDate As New frmdeliverDate
	
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	
    frmdeliverDate.Visible = False
    frmdeliverDate.StartPosition = FormStartPosition.CenterScreen
    If frmdeliverDate.ShowDialog = Windows.Forms.DialogResult.OK Then
        deliverDate = frmdeliverDate.DialogResult
    Else
        deliverDate = ""
    End If

End Sub

End Class


I can't figure out what I'm doing wrong here. I'd be grateful if you could help me out with this. :)

This post has been edited by nK0de: 04 February 2012 - 03:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2236
  • View blog
  • Posts: 9,407
  • Joined: 29-May 08

Posted 04 February 2012 - 09:12 AM

Ansewer these questions
How do you get the inputted text from my verison of the inputbox?
What does the ShowDialog method actually return?
What steps are you actually doing?
Was This Post Helpful? 0
  • +
  • -

#5 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Posted 04 February 2012 - 10:15 AM

View PostAdamSpeight2008, on 04 February 2012 - 09:42 PM, said:

Ansewer these questions
How do you get the inputted text from my verison of the inputbox?
What does the ShowDialog method actually return?


Sorry I'm a little confused, are you referring to your code in the tutorial or mine??
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2236
  • View blog
  • Posts: 9,407
  • Joined: 29-May 08

Posted 04 February 2012 - 12:56 PM

Both. Examine mine, then examine yours.
What parameters does your ShowDialog take? What type does is return?
Another tutorial for you to do.
Was This Post Helpful? 0
  • +
  • -

#7 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Posted 04 February 2012 - 01:48 PM

View PostAdamSpeight2008, on 05 February 2012 - 01:26 AM, said:

Both. Examine mine, then examine yours.
What parameters does your ShowDialog take? What type does is return?
Another tutorial for you to do.


hmm...okay, in your code, you pass EnteredText to the function. and it is assigned the the value from m_ReturnText. I fixed my code like this.

I have a variable defined in my Main Form like this

Dim deliverDate As String


So I pass that into the function. Which would be assinged with the value from the DateTimePicker,

Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

    Me.DialogResult = Windows.Forms.DialogResult.OK
    returnVal = dtpDeliver.Value.Date.ToString("MM/dd/yyyy")
    Me.Close()

End Sub


Public Overloads Function ShowDialog(ByVal deliverDate As String) As System.Windows.Forms.DialogResult

    Me.ShowDialog()
    deliverDate = returnVal
    Return Me.DialogResult

End Function



Aaand that's where I'm lost. Its supposed to return the deliverDate as a string but it doesn't. :dontgetit:

This post has been edited by nK0de: 04 February 2012 - 01:49 PM

Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2236
  • View blog
  • Posts: 9,407
  • Joined: 29-May 08

Posted 04 February 2012 - 01:54 PM

What other difference is there? in how the string is passed back to caller.
What do ShowDialog function return?

How can it return something else? like the value of the inputbox?
Was This Post Helpful? 0
  • +
  • -

#9 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Posted 04 February 2012 - 02:35 PM

Right, the Me.DialogResult is an integer value, so that's why I was getting 1 before.

I fixed it like this (in my Main Form).

If frmdeliverDate.ShowDialog = Windows.Forms.DialogResult.OK Then
    deliverDate = frmdeliverDate.dtpDeliver.Value.ToString("MM/dd/yyyy")
Else
    deliverDate = ""
End If


It works now. I get the date value in the deliverDate.

But I don't see how the ShowDialog method affects it. Because once I run it with the assigning part commented out like this,

Public Overloads Function ShowDialog(ByVal deliverDate As String) As System.Windows.Forms.DialogResult

   Me.ShowDialog()
   'deliverDate = returnVal
   Return Me.DialogResult

End Function


It still works fine. Please explain. :)

This post has been edited by nK0de: 04 February 2012 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2236
  • View blog
  • Posts: 9,407
  • Joined: 29-May 08

Posted 05 February 2012 - 08:34 PM

I'll defer the explaination to crepitus's tutorial:- Reference Types, Value Types, ByVal, ByRef
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1