📄 frmtimetracker.vb
字号:
End Sub
Private Sub txtTaskDates_Validating( _
ByVal sender As System.Object, _
ByVal e As CancelEventArgs _
) _
Handles txtTaskStart.Validating, _
txtTaskEnd.Validating
' If the date entered is not within one year
' of today, make the background light red.
Dim txtSender As TextBox = sender
With txtSender
If CDate(.Text) <= DateTime.Today.AddYears(-1) _
Or CDate(.Text) >= DateTime.Today.AddYears(1) Then
.BackColor = Color.LightPink
End If
End With
End Sub
'Private Sub cboxTasks_SelectedIndexChanged( _
' ByVal sender As System.Object, _
' ByVal e As System.EventArgs _
' ) _
' Handles cboxTasks.SelectedIndexChanged
' 'LoadTaskFields(cboxTasks.SelectedIndex)
'End Sub
Private Sub btnNewTask_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) _
Handles btnNewTask.Click
' Save the index number of the previous task. We will
' need to know it in case the users cancels out
' during task creation.
ixTaskPrev = cboxTasks.SelectedIndex
' Unbind the Tasks ArrayList from the controls.
UnbindTasksFromControls()
' Create and display a multiline textbox
' for the user to enter comments.
txtTaskComments = New Windows.Forms.TextBox
With txtTaskComments
.Multiline = True
' Locate it relative to other
' controls on the form.
.Left = txtTaskNo.Left
.Top = lblProjName.Top
.Width = Me.Width - (2 * .Left)
.Height = (txtTaskNo.Top - .Top) - (txtTaskNo.Height / 3)
' Enter and select the instructions.
.Text = "Add task comments here."
.SelectAll()
' Add the control to the form.
Me.Controls.Add(txtTaskComments)
' Bring it to the z-axis top and
' set the focus into it.
.BringToFront()
.Focus()
End With
' Designate the handler for the TextChanged
' event of the newly created control.
AddHandler txtTaskComments.TextChanged, _
AddressOf txtTaskComments_TextChanged
' Hide self and show Add and Cancel.
btnAddTask.Visible = True
btnCancel.Visible = True
btnNewTask.Visible = False
End Sub
Private Sub btnAddTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTask.Click
' Add the task to the Tasks array list.
theTasks.Add(New Task(txtTaskNo.Text, txtTaskName.Text, _
txtTaskStart.Text, txtTaskEnd.Text, _
txtTaskEstimated.Text, _
txtTaskActual.Text, _
txtTaskComments.Text))
' Rebind the Tasks ArrayList to the controls.
BindTasksToControls()
' Select the new task.
cboxTasks.SelectedIndex = theTasks.Count - 1
' Reset the form.
AfterNewTaskCleanup(ixTaskPrev)
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
' Rebind the Tasks ArrayList from the controls.
BindTasksToControls()
' Select the previous task.
cboxTasks.SelectedIndex = ixTaskPrev
' Reset the form.
AfterNewTaskCleanup(ixTaskPrev)
End Sub
' The event handler for a control
' that is created "on the fly".
Private Sub txtTaskComments_TextChanged( _
ByVal sender As System.Object, _
ByVal e As EventArgs _
)
' If the comment is more than thirty characters
' long, set the background color of the
' container panel to red.
With txtTaskComments
.BackColor = IIf(.Text.Length > 30, _
Color.Red, _
Color.White)
End With
End Sub
Private Sub InitControlState()
' Hide the Add button directly under the New button.
btnAddTask.Visible = False
btnAddTask.Bounds = btnNewTask.Bounds
' Hide the Cancel button
btnCancel.Visible = False
End Sub
Private Sub DisplayProject()
' Load Proj data into the labels.
With theProject
lblProjName.Text = .strProjName
lblProjStart.Text = .dateProjStart.ToString("ddMMM")
lblProjEnd.Text = .dateProjEnd.ToString("ddMMM")
End With
' Bind the dropdown ComboBox to the Tasks ArrayList.
BindTasksToControls()
With cboxTasks
' Start with the first task as
' the currently selected task.
.SelectedIndex = 0
' Set focus to the tasks ComboBox.
.Focus()
End With
End Sub
Private Sub BindTasksToControls()
With cboxTasks
' Bind the dropdown ComboBox to the Tasks ArrayList.
.DataSource = theTasks
.DisplayMember = "strTaskName"
.ValueMember = "strTaskIdent"
' Bind the textboxes to a column of the dropdown
' ComboBox's DataSource (the Task ArrayList).
BindToTextbox(txtTaskNo, "strTaskIdent")
BindToTextbox(txtTaskName, "strTaskName")
BindToTextbox(txtTaskStart, "dateTaskStart")
BindToTextbox(txtTaskEnd, "dateTaskEnd")
BindToTextbox(txtTaskActual, "durTaskActual")
BindToTextbox(txtTaskEstimated, "durTaskEstimated")
End With
End Sub
Private Sub BindToTextbox(ByVal txtTarget As TextBox, _
ByVal strColumnName As String)
With txtTarget
' Bind the textbox to a column of the dropdown
' ComboBox's DataSource (the Task ArrayList).
.DataBindings.Add( _
"Text", cboxTasks.DataSource, strColumnName)
' Specify an event handler for formating those
' fields that are datatype 'DateTime'.
If txtTarget Is txtTaskStart _
Or txtTarget Is txtTaskEnd Then
AddHandler .DataBindings(0).Format, _
AddressOf Date_Format
End If
End With
End Sub
Private Sub Date_Format(ByVal sender As System.Object, _
ByVal e As ConvertEventArgs)
' Format each date as it is moved by data binding
' from the ArrayList into a Textbox.
e.Value = CType(e.Value, DateTime).ToShortDateString
End Sub
Private Sub UnbindTasksFromControls()
' Iterate through all the controls in the TaskFields
' panel, unbind them and clear them.
Dim theControl As Control
For Each theControl In panelTaskFields.Controls
With theControl
.DataBindings.Clear()
.Text = String.Empty
End With
Next
' Unbind the ComboBox.
cboxTasks.DataSource = Nothing
End Sub
Private Sub AfterNewTaskCleanup(ByVal ixTask As Integer)
' Destroy the comments textbox
txtTaskComments.Dispose()
txtTaskComments = Nothing
' Hide Add and Cancel and show New.
btnAddTask.Visible = False
btnCancel.Visible = False
btnNewTask.Visible = True
' Set focus to the tasks ComboBox.
cboxTasks.Focus()
End Sub
Private Sub SetBkColor( _
ByVal lblTarget As Label, _
ByVal colorBack As Color _
)
' If the desired background color is the background
' color of this form, remove the label from the
' panel and dispose of the panel.
Dim panelBackColor As New Panel
If colorBack.Equals(Me.BackColor) Then
If Not lblTarget.Parent Is Me Then
panelBackColor = lblTarget.Parent
lblTarget.Bounds = panelBackColor.Bounds
lblTarget.Parent = Me
panelBackColor.Dispose()
End If
Else
' If the desired background color is not the
' background color of this form, then if the
' label is already inside a panel, set the
' background color of that panel. If not,
' create a panel, position it, put the label
' in it, and set the background color.
If lblTarget.Parent Is Me Then
panelBackColor = New Panel
Me.Controls.Add(panelBackColor)
With panelBackColor
.Visible = True
.Bounds = lblTarget.Bounds
lblTarget.Location = New Point(0, 0)
.Controls.Add(lblTarget)
End With
End If
panelBackColor.BackColor = colorBack
End If
End Sub
#Region " Database Read Simulation "
Private Function LoadProject( _
ByVal ProjectID As Integer _
) _
As Boolean
' Simulate having retrieved project data
' from a database. Here, we'll use our
' Project and Task data structures to
' hold the data.
With theProject
.strProjNo = 1
.strProjName = "Net CF Book"
.dateProjStart = DateTime.Today.AddDays(-100)
.dateProjEnd = DateTime.Today.AddDays(100)
End With
theTasks.Add(New Task(0, "Create Blueprints", _
DateTime.Today.AddDays(-17), _
DateTime.Today.AddDays(22), _
120, 60, ""))
theTasks.Add(New Task(1, "Build Franistans", _
DateTime.Today.AddDays(-11), _
DateTime.Today.AddDays(0), _
35, 30, ""))
theTasks.Add(New Task(2, "Build Widgets", _
DateTime.Today.AddDays(-4), _
DateTime.Today.AddDays(44), _
400, 45, ""))
theTasks.Add(New Task(3, "Assemble Woobletogles", _
DateTime.Today.AddDays(-19), _
DateTime.Today.AddDays(2), _
20, 20, ""))
theTasks.Add(New Task(4, "Weld Mainwareing", _
DateTime.Today.AddDays(-100), _
DateTime.Today.AddDays(50), _
20, 6, ""))
Return True
End Function
#End Region
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -