Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42441

Noob: Error Message Box Suggestion

$
0
0
The chapter I working on is covering adding forms and modules; my question isn't directed at those topics. In one the assignments, I was looking to a single MessageBox that shows all the inputs a users does when he/she clicks a calculate button. In the code below, I have a working version; see the strError variable string accumulator (I don't know a better term for it). I was thinking I could probably create procedure or function to do some thing. I just couldn't figure out really how to create it to list any specific errors if an error did occur. I considering using some kind of boolean if there was an error, it would true, and that error would be called and added to the list. Maybe it beyond my skill set, but it seems there is a better or more efficient way. Just looking to learn some alternatives. Thank you in advance.

Code:

Public Class frmMainForm

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        'declare constants
        Const decBaseShadeFee As Decimal = 50
        Const decREGULAR_SHADES As Decimal = 0
        Const decFODLING_SHADES As Decimal = 10
        Const decROMAN_SHADES As Decimal = 15
        Const dec25INCH_SHADES As Decimal = 0
        Const dec27INCH_SHADES As Decimal = 2
        Const dec32INCH_SHADES As Decimal = 4
        Const dec40INCH_SHADES As Decimal = 6
        Const decNATURAL_COLOR As Decimal = 5
        Const decBLUE_COLOR As Decimal = 0
        Const decTEAL_COLOR As Decimal = 0
        Const decRED_COLOR As Decimal = 0
        Const decGREEN_COLOR As Decimal = 0

        'declare variables
        Dim frmTotalForm As New frmTotalForm
        Dim decStyleCharge As Decimal = 0
        Dim decSizeCharge As Decimal = 0
        Dim decColorCharge As Decimal = 0
        Dim intNumberShades As Integer

        Dim strError As String = String.Empty

        If Integer.TryParse(txtNumberShades.Text, intNumberShades) Then
            If intNumberShades > 0 Then
            Else
                strError += ControlChars.CrLf & "Need to order at least 1 shade"
            End If
        Else
            strError += ControlChars.CrLf & "Need to Enter Numeric Value for Number of Shades"
        End If

        Select Case cboStyles.SelectedIndex
            Case 0
                decStyleCharge += decREGULAR_SHADES
            Case 1
                decStyleCharge += decFODLING_SHADES
            Case 2
                decStyleCharge += decROMAN_SHADES
            Case Else
                strError += ControlChars.CrLf & "No style selected"
        End Select

        Select Case cboSizes.SelectedIndex
            Case 0
                decSizeCharge += dec25INCH_SHADES
            Case 1
                decSizeCharge += dec27INCH_SHADES
            Case 2
                decSizeCharge += dec32INCH_SHADES
            Case 3
                decSizeCharge += dec40INCH_SHADES
            Case Else
                strError += ControlChars.CrLf & "No shade size selected"
        End Select

        Select Case cboColors.SelectedIndex
            Case 0
                decColorCharge += decNATURAL_COLOR
            Case 1
                decColorCharge += decBLUE_COLOR
            Case 2
                decColorCharge += decTEAL_COLOR
            Case 3
                decColorCharge += decRED_COLOR
            Case 4
                decColorCharge += decGREEN_COLOR
            Case Else
                strError += ControlChars.CrLf & "No shade color selected"
        End Select

        decTotal = intNumberShades * (decBaseShadeFee + decStyleCharge + decSizeCharge + decColorCharge)

        If strError <> String.Empty Then
            MessageBox.Show(strError, "Input Error")
        Else
            frmTotalForm.ShowDialog() 'this calls the TotalForm to display total.
        End If
    End Sub

    Function ErrorOutput() As Boolean
        Dim strNonNumeric As String = "Need to enter numerical value"
        Dim strOneShade As String = "Need to order at least 1 shade"
        Dim strStyle As String = "Need to select shade style"
        Dim strSize As String = "Need to select a shade size"
        Dim strColor As String = "Need to select a shade color"
        Dim strErrorString As String = " "

        Return True
    End Function
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub frmMainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MessageBox.Show("Do you want to quit application?", "Exit Application", MessageBoxButtons.OKCancel) = DialogResult.Cancel Then
            e.Cancel = True
        Else
            e.Cancel = False
        End If
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        MessageBox.Show("Chapter 7, Programming Challenge 5", "About")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txtNumberShades.Clear()
        cboColors.SelectedIndex = -1
        cboSizes.SelectedIndex = -1
        cboStyles.SelectedIndex = -1
        decTotal = 0
        txtNumberShades.Focus()
    End Sub
End Class

On a side note, what is the "e" after the "ByVal" I see that is created when I create an event handler? My book didn't explain it, but showed how to use to create the close verification MessageBox.

Viewing all articles
Browse latest Browse all 42441

Trending Articles