📄 formmain.cs
字号:
}
private void btnNewTask_Click(object sender, System.EventArgs e)
{
// 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 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 the instructions.
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();
// Designate the handler for the TextChanged
// event of the newly created control.
txtTaskComments.TextChanged += new EventHandler(this.txtTaskComments_TextChanged);
// Hide self and show Add and Cancel.
btnAddTask.Visible = true;
btnCancel.Visible = true;
btnNewTask.Visible = false;
}
private void btnAddTask_Click(object sender, System.EventArgs e)
{
// Add the task to the Tasks array list.
alTasks.Add(new YaoDurant.Data.UtilData.Task(
txtTaskNo.Text, "17", txtTaskName.Text,
Convert.ToDateTime(txtTaskStart.Text),
Convert.ToDateTime(txtTaskEnd.Text),
Convert.ToInt32(txtTaskEstimated.Text),
Convert.ToInt32(txtTaskActual.Text),
txtTaskComments.Text));
// Rebind the Tasks ArrayList to the controls.
BindTasksToControls();
// Select the new task.
cboxTasks.SelectedIndex = alTasks.Count - 1;
// Reset the form.
AfterNewTaskCleanup(ixTaskPrev);
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
// Rebind the Tasks ArrayList from the controls.
BindTasksToControls();
// Select the previous task.
cboxTasks.SelectedIndex = ixTaskPrev;
// Reset the form.
AfterNewTaskCleanup(ixTaskPrev);
}
// The event handler for a control
// that is created "on the fly".
private void txtTaskComments_TextChanged(object sender,
EventArgs e )
{
// if ( the comment is more than thirty characters
// long, set the background color of the
// container panel to red.
txtTaskComments.BackColor =
(txtTaskComments.Text.Length > 30) ?
Color.Red : Color.White;
}
private void InitControlState()
{
// Hide the Add button directly under the new button.
btnAddTask.Visible = false;
btnAddTask.Bounds = btnNewTask.Bounds;
// Hide the Cancel button
btnCancel.Visible = false;
}
private void DisplayProject()
{
// Load Proj data into the labels.
lblProjName.Text = theProject.strName;
lblProjStart.Text = theProject.dateStart.ToString("ddMMM");
lblProjEnd.Text = theProject.dateEnd.ToString("ddMMM");
// Bind the dropdown ComboBox to the Tasks ArrayList.
BindTasksToControls();
// Start with the first task as
// the currently selected task.
cboxTasks.SelectedIndex = 0;
// set { focus to the tasks ComboBox.
cboxTasks.Focus();
}
private void BindTasksToControls()
{
// Bind the dropdown ComboBox to the Tasks ArrayList.
cboxTasks.DataSource = alTasks;
cboxTasks.DisplayMember = "strName";
cboxTasks.ValueMember = "strIdent";
// Bind the textboxes to a column of the dropdown
// ComboBox's DataSource (the Task ArrayList).
BindToTextbox(txtTaskNo, "strIdent");
BindToTextbox(txtTaskName, "strName");
BindToTextbox(txtTaskStart, "dateStart");
BindToTextbox(txtTaskEnd, "dateEnd");
BindToTextbox(txtTaskActual, "durActual");
BindToTextbox(txtTaskEstimated, "durEstimated");
}
private void BindToTextbox( TextBox txtTarget,
string strColumnName)
{
// Bind the textbox to a column of the dropdown
// ComboBox's DataSource (the Task ArrayList).
txtTarget.DataBindings.Add("Text",
cboxTasks.DataSource,
strColumnName);
// Specify an event handler for formating those
// fields that are datatype //DateTime//.
if( txtTarget == txtTaskStart ||
txtTarget == txtTaskEnd )
{
txtTarget.DataBindings[0].Format +=
new ConvertEventHandler(this.Date_Format);
}
}
private void Date_Format( object sender,
ConvertEventArgs e)
{
// Format each date as it is moved by data binding
// from the ArrayList into a Textbox.
//e.value = e.Value.ToString("d");
}
private void UnbindTasksFromControls()
{
// Iterate through all the controls in the TaskFields
// panel, unbind them and clear them.
foreach( Control theControl in panelTaskFields.Controls )
{
theControl.DataBindings.Clear();
theControl.Text = string.Empty;
}
// Unbind the ComboBox.
cboxTasks.DataSource = null;
}
private void AfterNewTaskCleanup( int ixTask)
{
// Destroy the comments textbox
txtTaskComments.Dispose();
txtTaskComments = null;
// Hide Add and Cancel and show new.
btnAddTask.Visible = false;
btnCancel.Visible = false;
btnNewTask.Visible = true;
// 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.
Panel panelBackColor = new Panel();
if ( colorBack.Equals(this.BackColor) )
{
if (! (lblTarget.Parent == this) )
{
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 == this )
{
panelBackColor = new Panel();
this.Controls.Add(panelBackColor);
panelBackColor.Visible = true;
panelBackColor.Bounds = lblTarget.Bounds;
lblTarget.Location = new Point(0, 0);
panelBackColor.Controls.Add(lblTarget);
}
panelBackColor.BackColor = colorBack;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -