📄 formmain.cs
字号:
// during task creation.
ixTaskPrev = cboxTasks.SelectedIndex;
// Deselect the current task from the dropdown
// ComboBox. This will also cause the
// SelectedIndexChanged event to fire
// which will cause the task fields to clear.
cboxTasks.SelectedIndex = -1;
cboxTasks.SelectedIndex = -1;
// Create and display a multiline textbox
// for the user to enter comments.
txtTaskComments = new TextBox();
txtTaskComments.Multiline = true;
// Locate it relative to other
// controls on the form.
txtTaskComments.Left = txtTaskNo.Left;
txtTaskComments.Top = lblProjName.Top;
txtTaskComments.Width =
this.Width - (2 * txtTaskComments.Left);
txtTaskComments.Height =
(txtTaskNo.Top - txtTaskComments.Top) -
(txtTaskNo.Height / 3);
// Enter and select some text.
txtTaskComments.Text = "Add task comments here.";
txtTaskComments.SelectAll();
// Add the control to the form.
this.Controls.Add(txtTaskComments);
// Bring it to the z-axis top and
// set the focus into it.
txtTaskComments.BringToFront();
txtTaskComments.Focus();
// Associate the TextChanged event handler
// that we have written for this
// control with this control.
this.txtTaskComments.TextChanged +=
new System.EventHandler(
this.txtTaskComments_TextChanged);
// Hide self and show Add and Cancel.
cmdAddTask.Visible = true;
cmdCancel.Visible = true;
cmdNewTask.Visible = false;
}
private void cmdAddTask_Click(object sender, System.EventArgs e)
{
// // Unbind the Tasks array list from
// // the dropdown ComboBox.
// cboxTasks.DataSource = null;
//
// Add the task to the Tasks array list.
alTasks.Add(new Task(
txtTaskNo.Text,
txtTaskName.Text,
Convert.ToDateTime(txtTaskStart.Text),
Convert.ToDateTime(txtTaskEnd.Text),
Convert.ToInt32(txtTaskEstimated.Text),
Convert.ToInt32(txtTaskActual.Text),
txtTaskComments.Text));
// // Rebind the Tasks array list to
// // the dropdown ComboBox.
// cboxTasks.DataSource = alTasks;
// cboxTasks.DisplayMember = "strTaskName";
// cboxTasks.ValueMember = "strTaskIdent";
// Cleanup the form and select the new task.
AfterNewTaskCleanup(alTasks.Count - 1);
}
private void cmdCancel_Click(object sender,
System.EventArgs e)
{
// Cleanup the form and re-select the old task.
AfterNewTaskCleanup(ixTaskPrev);
}
private void cboxTasks_SelectedIndexChanged(
object sender,
System.EventArgs e)
{
LoadTaskFields(cboxTasks.SelectedIndex);
}
private void lblProjEnd_TextChanged(object sender,
System.EventArgs e)
{
// if this project is due or
// overdue, use red ink.
SetBkColor(lblProjEnd,Color.Red);
}
private void txtTaskDates_Validating(
object sender,
System.ComponentModel.CancelEventArgs e)
{
// if the date entered is not within one year
// of today, make the background light red.
TextBox txtSender = (TextBox)sender;
if ( Convert.ToDateTime(txtSender.Text) <=
DateTime.Today.AddYears(-1)
|| Convert.ToDateTime(txtSender.Text) >=
DateTime.Today.AddYears(1) )
{
txtSender.BackColor = Color.LightPink;
}
}
private void txtTaskComments_TextChanged(object sender,
EventArgs e )
{
txtTaskComments.BackColor =
(txtTaskComments.Text.Length > 30) ?
Color.Red : Color.White;
}
private void InitControlState()
{
// Hide the Add button directly under the new button.
cmdAddTask.Visible = false;
cmdAddTask.Bounds = cmdNewTask.Bounds;
// Hide the cancel button
cmdCancel.Visible = false;
}
private void DisplayProject()
{
// Load Proj data into the labels.
lblProjName.Text = theProject.strProjName;
lblProjStart.Text =
theProject.dateProjStart.ToString("ddMMM");
lblProjEnd.Text =
theProject.dateProjEnd.ToString("ddMMM");
// Load the tasks into the dropdown ComboBox.
foreach( Task theTask in alTasks )
{
cboxTasks.Items.Add(theTask.strTaskName);
}
// Start with the first task as
// the currently selected task.
cboxTasks.SelectedIndex = 0;
// set { focus to the tasks ComboBox.
cboxTasks.Focus();
}
private void LoadTaskFields( int intTaskNo)
{
// Load the fields for the specified
// task into the text boxes. if (
// intTaskNo is out of range, clear
// the textboxes.
if ( intTaskNo >= 0 && intTaskNo < alTasks.Count )
{
// Create a variable of a specific type
// so that we can do early binding.
Task refTask = (Task)alTasks[intTaskNo];
txtTaskNo.Text = refTask.strTaskIdent;
txtTaskName.Text = refTask.strTaskName;
txtTaskStart.Text =
refTask.dateTaskStart.ToString("MM/dd/yy");
txtTaskEnd.Text =
refTask.dateTaskEnd.ToString("MM/dd/yy");
txtTaskEstimated.Text =
refTask.durTaskEstimated.ToString();
txtTaskActual.Text =
refTask.durTaskActual.ToString();
}
else
{
txtTaskNo.Text = string.Empty;
txtTaskName.Text = string.Empty;
txtTaskStart.Text = string.Empty;
txtTaskEnd.Text = string.Empty;
txtTaskEstimated.Text = string.Empty;
txtTaskActual.Text = string.Empty;
}
}
private void AfterNewTaskCleanup(int ixTask)
{
// Destroy the comments textbox
txtTaskComments.Dispose();
txtTaskComments = null;
// Hide Add and Cancel and show new.
cmdAddTask.Visible = false;
cmdCancel.Visible = false;
cmdNewTask.Visible = true;
// Select the specified task.
cboxTasks.SelectedIndex = ixTask;
// set { focus to the tasks ComboBox.
cboxTasks.Focus();
}
private void SetBkColor(Label lblTarget, Color colorBack)
{
// if the desired background color is the background
// color of this form, remove the label from the
// panel and dispose of the panel.
if ( colorBack.Equals(this.BackColor) )
{
if (lblTarget.Parent.Equals(this) )
{
}
else
{
Panel panelBackColor = (Panel)(lblTarget.Parent);
lblTarget.Bounds = panelBackColor.Bounds;
lblTarget.Parent = this;
panelBackColor.Dispose();
}
}
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.Equals(this) )
{
Panel panelBackColor = new Panel();
panelBackColor.BackColor = colorBack;
this.Controls.Add(panelBackColor);
panelBackColor.Visible = true;
panelBackColor.Bounds = lblTarget.Bounds;
lblTarget.Location = new Point(0, 0);
panelBackColor.Controls.Add(lblTarget);
}
else
{
((Panel)(lblTarget.Parent)).BackColor = colorBack;
}
}
}
#region Database Read Simulation
private bool LoadProject( int ProjectID )
{
// Simulate having retrieved project data
// from a database. Here, we'll use our
// Project and Task data structures to
// hold the data.
theProject.strProjNo = "1";
theProject.strProjName = "Net CF Book";
theProject.dateProjStart = DateTime.Today.AddDays(-100);
theProject.dateProjEnd = DateTime.Today.AddDays(100);
alTasks.Add(new Task("0",
"Create Blueprints",
DateTime.Today.AddDays(-17), DateTime.Today.AddDays(22),
120, 60, ""));
alTasks.Add(new Task("1",
"Build Franistans",
DateTime.Today.AddDays(-11), DateTime.Today.AddDays(0),
35, 30, ""));
alTasks.Add(new Task("2",
"Build Widgets",
DateTime.Today.AddDays(-4), DateTime.Today.AddDays(44),
400, 45, ""));
alTasks.Add(new Task("3",
"Assemble Woobletogles",
DateTime.Today.AddDays(-19), DateTime.Today.AddDays(2),
20, 20, ""));
alTasks.Add(new Task("4",
"Weld Mainwareing",
DateTime.Today.AddDays(-100), DateTime.Today.AddDays(50),
20, 6, ""));
return true;
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -