Results 1 to 8 of 8

Thread: Detect if cancel is clicked on inputbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    65

    Detect if cancel is clicked on inputbox

    Hi,
    How can i get the program to detect if cancel was clicked? Right now when i click cancel the inputbox does not close and simply comes back up saying please enter a number. Do i have to do something boolean related or something?

    Code:
    'Developer- John Nelson
    'Date- Feb 10, 2012 
    'Application Name- Hawaainn Average Tempeture
    'The windows application is written for the hawaniin tourism bosard with the task of calculationting 
    'The average yearly tempurture in the hawaiin Islands for one year
    
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            out.Text = ""
        End Sub
    
        Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
            'This will close the program when clicked.
            Close()
        End Sub
    
    
        Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
            'This code will ask the user to input 12 temperutres, and it will then calculate them.
            Dim dectemptotalloop As Decimal
            Dim decave As Decimal
            Dim stroutputmessage As String = "The average tempo for HI this was "
    
            'Loop Strucure function
            dectemptotalloop = calclateloop()
            'ave temp calculation
            decave = Calculateavetemp(dectemptotalloop)
            'output label
            out.Text = stroutputmessage & decave.ToString("G")
    
    
    
        End Sub
        Private Function calclateloop()
            'This section will be the loop function. It will calculate the **** and all that.
            Dim strcancelclicked As String = ""
            Dim dectotaloftemps As Decimal
            Dim decmaxentry As Decimal = 12
            Dim deccurrententries As Decimal
            Dim strentryhold As String
            Dim strinputmessage As String = "Please enter the average temperature of the month"
            Dim strerrormessage As String = "Please enter a number or you did not enter up to 12 months"
    
    
            Do Until deccurrententries = decmaxentry
                strentryhold = InputBox(strinputmessage)
                If IsNumeric(strentryhold) Then
                    temp.Items.Add(strentryhold)
                    dectotaloftemps += Convert.ToDecimal(strentryhold)
                    deccurrententries += 1
                Else
                    MsgBox(strerrormessage)
                End If
    
            Loop
            Return dectotaloftemps
    
    
        End Function
        Private Function Calculateavetemp(ByVal decave As Decimal)
            Dim decavetemp As Decimal
            decavetemp = decave / 12
            Return decavetemp
    
        End Function
    End Class

  2. #2
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Post Re: Detect if cancel is clicked on inputbox

    Clicking the X or the Cancel button on an InputBox returns a string with a length of 0.
    ~Peter


  3. #3
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Post Re: Detect if cancel is clicked on inputbox

    Actually, the InputBox returns a string with a length of zero if you don't type in a value, but click the OK button.

    It looks like you'll need to build your own InputBox.
    ~Peter


  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Detect if cancel is clicked on inputbox

    Surely not entering any thing should be the same as canceled unless checking for an empty string?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Detect if cancel is clicked on inputbox

    There is no difference between Cancel and entering nothing followed by OK. This is one of the reasons why the InputBox is never the best tool for any job. Make your own form. That way you get all the control over what it does, how it looks, how it behaves, and so forth. The InputBox is a legacy item that has no useful purpose.
    My usual boring signature: Nothing

  6. #6
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: Detect if cancel is clicked on inputbox

    Hello I have the same problem as the OP

    What I found while Googling is this:

    Code:
            Dim message, title, myValue As String
            message = "Please Enter file name:"
            title = "Save Map"
    
            myValue = InputBox(message, title, "", 150, 150)
    
            If StrPtr(myValue) = 0 Then
                MsgBox("You pressed cancel!")
            ElseIf myValue.Length = 0 Then
                MsgBox("OK pressed but nothing entered.")
            Else
                MsgBox("OK pressed: value= " & myValue)
            End If
    I don't know what to use as an alternative for the StrPtr. Maybe you gurus can help us both

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Detect if cancel is clicked on inputbox

    Quote Originally Posted by m.davide View Post
    I don't know what to use as an alternative for the StrPtr. Maybe you gurus can help us both
    The alternative is to not use InputBox because it's a piece of garbage. There is no easy way to distinguish between cancelling and entering nothing because there was never supposed to be an easy way. Don't use InputBox at all, ever in VB.NET. It's a small task to create your own dialogue that matches your UI and display it as you want. If you want such a dialogue regularly then you can put it in a DLL and re-use it in multiple projects.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: Detect if cancel is clicked on inputbox

    Ok thanks!

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