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
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.
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.
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?
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.
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 :)
Re: Detect if cancel is clicked on inputbox
Quote:
Originally Posted by
m.davide
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.
Re: Detect if cancel is clicked on inputbox