Beginning Visual C# Exercises - Chapter 14
1. While both requested events are newly created, one is triggered within the set portion of the
property for the maxLength field; the other, is triggered within an existing event - the
TextChanged event of the TextBox.
[u]UserControl1.cs</u>
...Above the constructor in the UserControl1.cs code, add the following
// Define the two new events
public event System.EventHandler MaxLengthChanged;
public event System.EventHandler MaxLengthReached;
// Define an integer field to hold TextBox string limit; default is 20 characters
private int maxLength = 20;
// Define a property to process TextBox string limit
public int MaxLength
{
get
{
return this.maxLength;
}
set
{
// Remember what the start length value was
int startValue = this.maxLength;
// Assign the new length value
this.maxLength = value;
// Truncate text in TextBox when the length is reduced
this.adjustText();
// When there is a subscriber and there is a change, raise an event
if ((MaxLengthChanged != null) && (startValue != this.maxLength))
{
System.Delegate[] subscribers = MaxLengthChanged.GetInvocationList();
foreach (System.EventHandler target in subscribers)
{
target(this, new System.EventArgs());
}
}
}
}
public ctlLabelTextbox()
{
... At the end of existing constructor code add this line
// Subscribe to TextBox text changed event to watch text length changes
this.txtLabelText.TextChanged += new EventHandler(txtLabelText_TextChanged);
}
... At bottom of UserControl1.cs, add the following
// This method evaluates text length to raise the "MaxLengthReached" event
private void txtLabelText_TextChanged(object sender, EventArgs e)
{
// Raise an event when subscribers exist and text is too long
if ((this.MaxLengthReached != null) && (this.txtLabelText.Text.Length >= this.maxLength))
{
this.MaxLengthReached(this, new System.EventArgs());
}
// Truncate text in TextBox if length was exceeded
this.adjustText();
}
// This method truncates the text in the TextBox control to maxlength
private void adjustText()
{
if (this.maxLength < this.txtLabelText.Text.Length)
this.txtLabelText.Text = this.txtLabelText.Text.Substring(0, this.maxLength);
}
[u]Form1.cs</u>
...Above the constructor in the Form1.cs code, add the following
// Add a control to set the maxLength value and this line will result
private System.Windows.Forms.NumericUpDown numericUpDown1;
public Form1()
{
... At the end of existing constructor code add these lines
// Subscriptions
this.ctlLabelTextbox1.MaxLengthChanged +=
new EventHandler(ctlLabelTextbox1_MaxLengthChanged);
this.ctlLabelTextbox1.MaxLengthReached +=
new EventHandler(ctlLabelTextbox1_MaxLengthReached);
// Set the value of this control to the default value within the "LabelTextBox" control
this.numericUpDown1.Value = this.ctlLabelTextbox1.MaxLength;
}
... At bottom of Form1.cs, add the following
// Here's the method called in the delegate
private void ctlLabelTextbox1_MaxLengthChanged(object sender, EventArgs e)
{
MessageBox.Show("The maximum span for the label name has changed");
}
// Here's the method called in the delegate
private void ctlLabelTextbox1_MaxLengthReached(object sender, EventArgs e)
{
MessageBox.Show("You have met or exceeded the allowable string span for the label.");
}
// Here's where the maxLength is sent to the LabelTextBox control
private void numericUpDown1_Validated(object sender, System.EventArgs e)
{
this.ctlLabelTextbox1.MaxLength = System.Convert.ToInt32(this.numericUpDown1.Value);
}
|