好的代码给大家共享
#region Private Methods
/// <summary>
/// Sets the ActorDisplay to display no Actor.
/// Disables all of the GUI controls of the ActorDisplay.
/// </summary>
private void InitEmpty()
{
initializing=true;
saveX = "0";
saveY = "0";
saveW = "0";
saveH = "0";
saveFilled =false;
saveColor =Color.White;
saveBordered=true;
saveBrdColor=Color.Black;
saveText ="";
saveTxtColor=Color.Black;
UpdateGUI();
noActor=true;
this.tBox_LocX.Enabled=true;
this.tBox_LocY.Enabled=true;
this.tBox_Width.Enabled=true;
this.tBox_Height.Enabled=true;
this.check_Filled.Enabled=true;
this.btn_ColorPick.Enabled=true;
this.check_Bordered.Enabled=true;
this.btn_BrdColorPick.Enabled=true;
this.tBox_Text.Enabled=true;
this.btn_TxtColorPick.Enabled=true;
initializing=false;
}
/// <summary>
/// Sets the ActorDisplay to display the given Actor.
/// Enables all of the GUI Controls of the ActorDisplay.
/// </summary>
/// <param name="inA">The Actor to be displayed.</param>
private void SaveActor(Actor inA)
{
initializing=true;
saveX = inA[UseCaseAnchors.TopLeft].CenterX.ToString();
saveY = inA[UseCaseAnchors.TopLeft].CenterY.ToString();
saveW = inA.Width.ToString();
saveH = inA.Height.ToString();
saveFilled = inA.Filled;
saveColor = inA.Color;
saveBordered= inA.Bordered;
saveBrdColor= inA.BrdColor;
saveText = inA.Text;
saveTxtColor= inA.TxtColor;
UpdateGUI();
noActor=false;
this.tBox_LocX.Enabled=true;
this.tBox_LocY.Enabled=true;
this.tBox_Width.Enabled=true;
this.tBox_Height.Enabled=true;
this.check_Filled.Enabled=true;
this.btn_ColorPick.Enabled=true;
this.check_Bordered.Enabled=true;
this.btn_BrdColorPick.Enabled=true;
this.tBox_Text.Enabled=true;
this.btn_TxtColorPick.Enabled=true;
initializing=false;
}
/// <summary>
/// Updates the GUI Control of the ActorDisplay to match
/// the values in the private fields of the ActorDisplay.
/// </summary>
private void UpdateGUI()
{
this.tBox_LocX.Text=saveX;
this.tBox_LocY.Text=saveY;
this.tBox_Width.Text=saveW;
this.tBox_Height.Text=saveH;
this.check_Filled.Checked=saveFilled;
this.pnl_Color.BackColor=saveColor;
this.check_Bordered.Checked=saveBordered;
this.pnl_BrdColor.BackColor=saveBrdColor;
this.tBox_Text.Text=saveText;
this.pnl_TxtColor.BackColor=saveTxtColor;
}
/// <summary>
/// Announces (via MessageBox) that the specified coordinate
/// is invalid.
/// </summary>
/// <param name="s">The string representation of the invalid coordinate.</param>
private void AnnounceBadCoordinate(string s)
{
StringBuilder sb = new StringBuilder("The value you specified for the actor's location\n");
sb.Append("coordinate ("+s+") is not a valid coordinate.\n");
sb.Append("The coordinate will be reset to its previous value.\n");
MessageBox.Show(this, sb.ToString(), "Invalid Coordinate:");
}
/// <summary>
/// Announces (via MessageBox) that the specified dimension
/// is invalid.
/// </summary>
/// <param name="s">The string representation of the invalid dimension.</param>
private void AnnounceBadDimension(string s)
{
StringBuilder sb = new StringBuilder("The value you specified for the actor's dimension\n");
sb.Append("("+s+") is not a valid dimension.\n");
sb.Append("The dimension will be reset to its previous value.\n");
MessageBox.Show(this, sb.ToString(), "Invalid Dimension:");
}
#region Handle events from the GUI Controls
/// <summary>
/// Handles the Leave (i.e. LooseFocus) event for the text boxes used to
/// display the coordinates of the Actor's Location.
/// The new coordinate is validated (see ValidLocationCoordinate method). If
/// the new coordinate is valid (and different from the previous value
/// of the same coordinate), the OnLocationChanged method is invoked
/// to fire a new ActorLocationChanged event. If the new coordinate is invalid,
/// the AnnounceBadCoordinate method is invoked.
/// </summary>
/// <param name="sender">The object sending the event.</param>
/// <param name="e">The System.EventArgs describing the event.</param>
private void locCoord_LooseFocus(object sender, System.EventArgs e)
{
string ntxt ="";
string otxt ="";
Coordinate whichCoordinate = Coordinate.X;
TextBox tbox = sender as TextBox;
if(tbox==null) return;
switch(tbox.Name)
{
case "tBox_LocX":
otxt = saveX;
ntxt = this.tBox_LocX.Text;
whichCoordinate = Coordinate.X;
break;
case "tBox_LocY":
otxt = saveY;
ntxt = this.tBox_LocY.Text;
whichCoordinate = Coordinate.Y;
break;
default:
return;
}
float newVal = 0.0f;
if(ValidLocationCoordinate(ntxt, ref newVal))
{
switch(whichCoordinate)
{
case Coordinate.X:
saveX = ntxt;
break;
case Coordinate.Y:
saveY = ntxt;
break;
default:
break;
}
if(!initializing)
OnLocationChanged(new ActorLocationChangedEventArgs(whichCoordinate, newVal));
}
else
{
AnnounceBadCoordinate(ntxt);
tbox.Text=otxt;
}
}
/// <summary>
/// Handles the Leave (i.e. LooseFocus) event for the text boxes used to
/// display the dimensions of the Actor.
/// The new dimension is validated (see ValidDimension method). If
/// the new dimension is valid (and different from the previous value
/// of the same coordinate), the OnActorResized method is invoked
/// to fire a new ActorResized event. If the new coordinate is invalid,
/// the AnnounceBadDimension method is invoked.
/// </summary>
/// <param name="sender">The object sending the event.</param>
/// <param name="e">The System.EventArgs describing the event.</param>
private void dimension_LooseFocus(object sender, System.EventArgs e)
{
string ntxt ="";
string otxt ="";
TextBox tbox = sender as TextBox;
if(tbox==null) return;
switch(tbox.Name)
{
case "tBox_Width":
ntxt = this.tBox_Width.Text;
otxt = saveW;
break;
case "tBox_Height":
ntxt = this.tBox_Height.Text;
otxt = saveH;
break;
default:
return;
}
float f = 0.0f;
if(ValidDimension(ntxt, ref f))
{
switch(tbox.Name)
{
case "tBox_Width":
saveW=ntxt;
break;
case "tBox_Height":
saveH=ntxt;
break;
default:
return;
}
float fw = float.Parse(this.tBox_Width.Text);
float fh = float.Parse(this.tBox_Height.Text);
if(!initializing)
OnActorResized(new ActorResizeEventArgs(fw, fh));
}
else
{
AnnounceBadDimension(ntxt);
tbox.Text=otxt;
}
}
/// <summary>
/// Handles the CheckedChanged event of the check_Filled checkbox.
/// Invokes the OnActorFilledChanged method to fire a new
/// ActorFilledChanged event.
/// </summary>
/// <param name="sender">The object sending the event.</param>
/// <param name="e">The System.EventArgs describing the event.</param>
private void check_Filled_CheckedChanged(object sender, System.EventArgs e)
{
saveFilled = this.check_Filled.Checked;
if(!initializing)
OnActorFilledChanged(new ActorFilledChangedEventArgs(saveFilled));
}
/// <summary>
private void check_Bordered_CheckedChanged(object sender, System.EventArgs e)
{
saveBordered = this.check_Bordered.Checked;
if(!initializing)
OnActorBorderedChanged(new ActorBorderedChangedEventArgs(saveFilled));
}