What's wrong with inputBox usage

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
Private Sub RadioBtnRemove_CheckedChanged(sender As Object, e As EventArgs) Handles RadioBtnRemove.CheckedChanged
        If RadioBtnRemove.Checked = True Then
            strIlk = InputBox("Please enter a sort length string!")
        ElseIf strIlk = Nothing Then
            Exit Sub
        End If
        If strIlk.Trim().Length > 0 Then
            Label1.Text = "Orjinal string: " & strIlk
            strIkinci = strIlk.Remove(0, 3)
            Label2.Text = "After the process: " & strIkinci
        Else
            MessageBox.Show("Please provide a sort length string or try again!")
        End If
    End Sub


My ait is to get a string with InputBox but if change mind and usage the cancel button on InputBox then why function jump to line of last MessageBox, I used Exit Sub actually, something wrong the lines of codes.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,335
Location
Sydney, Australia
Programming Experience
10+
First of all, InputBox returns an empty String if the user doesn't enter anything into the dialogue as well as if they click the Cancel button. That's one of the main reasons that many people, myself included, recommend against using it at all. You are testing the input against Nothing in one place and testing its Length elsewhere, like they are two different things.

Secondly, that ElseIf statement is only going to be evaluated if the If statement evaluates to False, i.e. if the RadioButton is not checked.

Your code structure is very wrong in fact. Presumably you don't want to do anything when the RadioButton is unchecked, so everything else should be inside a single If statement that checks that. You would then display the InputBox and then check whether the user entered anything once and once only. If they did then you use it. This is exactly why elementary programming classes teach people to write out their plan first, before writing any code. If you had done that then it would be easy to see that the code you have doesn't actually make sense in the context of what you want to achieve.
 

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
But it does not appear to use only one if statement, there might be another one at least. InputBox's cancel option spoilts my aim. I have to check this anyway
 

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
Your spot on the issue, thanks I'll try it with else in second if statement for reply whether ok or cancel of InputBox.
 

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
jmcilhinney, thanks for helping hand.. It works

Private Sub RadioBtnRemove_CheckedChanged(sender As Object, e As EventArgs) Handles RadioBtnRemove.CheckedChanged
        If RadioBtnRemove.Checked = True Then
            strIlk = InputBox("Please enter a sort length string!")
            If strIlk <> "" Then
                Label1.Text = "Orjinal string: " & strIlk
                strIkinci = strIlk.Remove(0, 3)
                Label2.Text = "After the process: " & strIkinci
            Else
                MessageBox.Show("Please provide a sort length string or try again!")
            End If
        End If
    End Sub
 
Top Bottom