Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 8th, 2004, 06:19 PM
Authorized User
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

        ...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
                return this.maxLength;
                // 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

                // 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 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);

        ...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);

Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Visual C# Exercises - Chapter 15 seblake C# 1 September 17th, 2004 08:41 PM
Beginning Visual C# Exercises - Chapter 10 seblake C# 1 July 29th, 2004 05:10 PM
Beginning Visual C# Exercises - Chapter 05 seblake C# 1 July 26th, 2004 07:40 AM
Beginning Visual C# Exercises - Chapter 04 seblake C# 0 July 21st, 2004 09:21 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.