我一直在努力开发这个程序。我在VB.Net上只有一个学期的编程,并决定多练习,因为我真的想成为一名程序员。关于我所包含的计划,我有几个问题。 1.)对于我的nameTextBox,我有TabIndex = 0和TabStop True,但是当它启动时我无法让程序专注于该文本框。我最终使用了下面的子程序,但觉得我一定做错了,或者重点放在nameTextBox上。有什么想法吗?
Private Sub purchaseForm_Shown(sender As System.Object,
e As System.EventArgs) Handles Me.Shown
'When form opens, let focus be on nameTextBox.
nameTextBox.Focus()
End Sub
2。)我无法得到任何东西来为zipMaskedTextBox提供验证。我无法弄清楚如何确保它专注于该框并在传递并留空时引发错误消息。
3.)我的最后3个文本框(数量,重量和价格)需要数字条目或程序将崩溃。我可以为此工作的唯一验证是带有消息框消息的Try / Catch块。无论如何使用ErrorProvider1方法,就像我使用其他文本框一样?我想保持方法一致。
'Description: A purchase form that will enable the customer to input item, quantity and weight,
' returning the subtotal, sales tax and final total when the entire order is completed.
' Case structure will be used in parts of the program.
Public Class purchaseForm
Private quantityInteger As Integer
Private weightDecimal As Decimal
Private itemWeightDecimal As Decimal
Private totalWeightDecimal As Decimal
Private itemDescriptionString As String
Private priceDecimal As Decimal
Private itemPriceDecimal As Decimal
Private totalItemPriceecimal As Decimal
Private subtotalDecimal As Decimal
Private handlingInteger As Integer
Private totalHandlingInteger As Integer
Private Const shippingDecimal As Decimal = 0.25
Private totalShippingDecimal As Decimal
Private totalShippingAndHandlingDecimal As Decimal 'Shipping and handling combined for totalTextBox.
Private Sub purchaseForm_Shown(sender As System.Object, e As System.EventArgs) Handles Me.Shown
'When form opens, let focus be on nameTextBox.
nameTextBox.Focus()
End Sub
Private Sub addItemButton_Click(sender As System.Object, e As System.EventArgs) Handles addItemButton.Click
'Use numeric values in calculations.
Try
quantityInteger = Integer.Parse(quantityTextBox.Text)
Try
weightDecimal = Decimal.Parse(weightTextBox.Text)
Try
priceDecimal = Decimal.Parse(priceTextBox.Text)
'Perform the calculations
itemWeightDecimal = weightDecimal * quantityInteger
totalWeightDecimal += itemWeightDecimal
itemPriceDecimal = priceDecimal * quantityInteger
subtotalDecimal += itemPriceDecimal
totalShippingDecimal = shippingDecimal * totalWeightDecimal
'Convert numeric values back to string values to display in textboxes.
If quantityInteger <> 1 Then
weightLabel.Visible = False
quantityItemsWeightLabel.Visible = True
weightTextBox.Text = itemWeightDecimal.ToString()
priceLabel.Visible = False
quantityItemsPriceLabel.Visible = True
priceTextBox.Text = itemPriceDecimal.ToString("C")
Else
weightTextBox.Text = itemWeightDecimal.ToString()
priceTextBox.Text = itemPriceDecimal.ToString("C")
subtotalTextBox.Text = subtotalDecimal.ToString("C")
customerGroupBox.Enabled = False
End If
subtotalTextBox.Text = subtotalDecimal.ToString("C")
customerGroupBox.Enabled = False
Catch priceExemption As FormatException
MessageBox.Show("Value must be numerical", "Error", MessageBoxButtons.OK)
With priceTextBox
.Focus()
.SelectAll()
End With
End Try
Catch weightExemption As FormatException
MessageBox.Show("Value must be numerical", "Error", MessageBoxButtons.OK)
With weightTextBox
.Focus()
.SelectAll()
End With
End Try
Catch quantityExemption As FormatException
MessageBox.Show("Value must be numerical", "Error", MessageBoxButtons.OK)
With quantityTextBox
.Focus()
.SelectAll()
End With
End Try
End Sub
Private Sub clearButton_Click(sender As System.Object, e As System.EventArgs) Handles clearButton.Click
'Clears all the data and totals for the current customer.
descriptionTextBox.Text = String.Empty
quantityTextBox.Text = ""
weightLabel.Visible = True
quantityItemsWeightLabel.Visible = False
weightTextBox.Clear()
priceTextBox.Text = String.Empty
priceLabel.Visible = True
quantityItemsPriceLabel.Visible = False
shippingAndHandlingTextBox.Text = ""
salesTaxTextBox.Clear()
totalTextBox.Text = String.Empty
End Sub
Private Sub updateSummaryButton_Click(sender As System.Object, e As System.EventArgs) Handles updateSummaryButton.Click
'Declare local variables.
Dim salesTaxDecimal As Decimal = 0.08D
Dim totalSalesTaxDecimal As Decimal
Dim grandTotalDecimal As Decimal
Dim totalShippingAndHandlingDecimal As Decimal
Dim orderCompletedDialogResult As DialogResult
orderCompletedDialogResult = MessageBox.Show("Are you sure the order is completed?", "Order Completed", MessageBoxButtons.YesNo)
If orderCompletedDialogResult = Windows.Forms.DialogResult.Yes Then
'Determine sales tax.
If stateTextBox.Text.ToUpper() = "CA" Then
totalSalesTaxDecimal = salesTaxDecimal * subtotalDecimal
Else
totalSalesTaxDecimal = "0"
End If
'Create a Case structure to determine handling charges on order.
Select Case totalWeightDecimal
Case Is <= 10
totalHandlingInteger = 1
Case 11 To 100
totalHandlingInteger = 3
Case Is > 100
totalHandlingInteger = 5
End Select
'Calculate total shipping and handling.
totalShippingAndHandlingDecimal = totalShippingDecimal + totalHandlingInteger
'calculate grand total
grandTotalDecimal = subtotalDecimal + totalSalesTaxDecimal + totalShippingAndHandlingDecimal
'convert numeric values into string values to enter shipping & handling, sales tax and the grand total into respective text boxes
shippingAndHandlingTextBox.Text = (totalShippingDecimal + totalHandlingInteger).ToString("C")
salesTaxTextBox.Text = totalSalesTaxDecimal.ToString("C")
totalTextBox.Text = grandTotalDecimal.ToString("C")
orderCompletedDialogResult = MessageBox.Show("Have you printed the purchase form for the customer?", "Print", MessageBoxButtons.YesNo)
If orderCompletedDialogResult = Windows.Forms.DialogResult.Yes Then
'Code to clear the entire order form.
'Call the clearButton procedure and continue from there.
Call clearButton_Click(sender, e)
'Continue clearing the entire form.
nameTextBox.Text = ""
addressTextBox.Clear()
cityTextBox.Text = String.Empty
stateTextBox.Text = ""
zipMaskedTextBox.Clear()
subtotalTextBox.Text = String.Empty
End If
End If
customerGroupBox.Enabled = True
nameTextBox.Focus()
End Sub
Private Sub exitButton_Click(sender As System.Object, e As System.EventArgs) Handles exitButton.Click
'Close the form
Me.Close()
End Sub
Private Sub ValidatingForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'Do not allow validation to cancel the form closing. End the program and close the form.
e.Cancel = False
End Sub
Private Sub nameTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles nameTextBox.Validating
Me.Focus()
'Validate for a required entry
'Cancel any previous error.
ErrorProvider1.SetError(nameTextBox, "")
'Check for an empty string
If nameTextBox.Text = String.Empty Then
'Cancel the event.
e.Cancel = True
ErrorProvider1.SetError(nameTextBox, "Required Field.")
End If
End Sub
Private Sub addressTextBox_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles addressTextBox.Validating
'Validate for a required entry.
'Cancel any previous entry.
ErrorProvider1.SetError(addressTextBox, "")
'Check for an empty string
If addressTextBox.Text = String.Empty Then
'Cancel the event.
e.Cancel = True
ErrorProvider1.SetError(addressTextBox, "Required field")
End If
End Sub
Private Sub cityTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles cityTextBox.Validating
'Validate for a required entry.
'Cancel any previous entry.
ErrorProvider1.SetError(cityTextBox, "")
'Check for an empty string.
If cityTextBox.Text = String.Empty Then
'Cancel the event.
e.Cancel = True
ErrorProvider1.SetError(cityTextBox, "Required Field")
End If
End Sub
Private Sub stateTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles stateTextBox.Validating
'Validate for a required entry'
'Cancels any previous entry.
ErrorProvider1.SetError(stateTextBox, "")
'Check for an empty string.
If stateTextBox.Text.Length <> 2 Then
'Cancel the event
e.Cancel = True
ErrorProvider1.SetError(stateTextBox, "State Abbreviation Must Be 2 Letters")
End If
End Sub
Private Sub zipMaskedTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles zipMaskedTextBox.Validating
'Validate for a required entry.
'Cancels any previous entry.
ErrorProvider1.SetError(zipMaskedTextBox, "")
'Check for an empty string
If zipMaskedTextBox.Text = String.Empty Then
'cancel the event
e.Cancel = True
ErrorProvider1.SetError(zipMaskedTextBox, "Required Entry")
End If
End Sub
Private Sub descriptionTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles descriptionTextBox.Validating
'Validate for a required entry
'Cancel any previous entry.
ErrorProvider1.SetError(descriptionTextBox, "")
'Check for an empty string
If descriptionTextBox.Text = String.Empty Then
'cancel the event
e.Cancel = True
ErrorProvider1.SetError(descriptionTextBox, "Entry Required")
End If
End Sub
End Class