Click here to Skip to main content
15,897,371 members
Home / Discussions / C#
   

C#

 
AnswerRe: Creating Dataset from Xml Pin
Ennis Ray Lynch, Jr.20-Jan-10 10:17
Ennis Ray Lynch, Jr.20-Jan-10 10:17 
AnswerRe: Creating Dataset from Xml Pin
Fayu21-Jan-10 3:14
Fayu21-Jan-10 3:14 
QuestionDeleted controls still appear in InitializeComponents() Pin
yeah100020-Jan-10 4:49
yeah100020-Jan-10 4:49 
AnswerRe: Deleted controls still appear in InitializeComponents() Pin
Rick Shaub20-Jan-10 5:57
Rick Shaub20-Jan-10 5:57 
GeneralRe: Deleted controls still appear in InitializeComponents() Pin
yeah100020-Jan-10 21:49
yeah100020-Jan-10 21:49 
GeneralRe: Deleted controls still appear in InitializeComponents() Pin
yeah10004-Feb-10 2:15
yeah10004-Feb-10 2:15 
GeneralRe: Deleted controls still appear in InitializeComponents() Pin
yeah10008-Feb-10 2:28
yeah10008-Feb-10 2:28 
QuestionComboBox.Items.Clear() not working -- Totally Revised Question Pin
Xpnctoc20-Jan-10 4:15
Xpnctoc20-Jan-10 4:15 
My last post about this issue resulted in a lot of confusion. So I want to try this a different way:

I'm making a custom time picker that inherits directly from ComboBox. I am not using a designer to make a System.Windows.Forms.UserControl-based control. I will post the full code in a moment to clarify.

The control has a property called Increment, which is the increment in minutes between each ComboBox item. Therefore the "set" action of Increment invokes the private method RepopulateChoices(). The method is also invoked in the "set" action of another property called ShowMilitary. The first line of RepopulateChoices() is:
<br />
Items.Clear();<br />

Remember I'm inheriting directly from ComboBox, so this syntax is CORRECT. It does not need to be prefixed by "myComboBox1." or anything like that. It is not a designer-based user control.

The problem is that I end up with duplicate items. Example: If I set Increment = 30, I'll see the items 11:00 PM, 11:30 PM, and then 12:00 AM, 12:30 AM and it keeps going. The number of duplicates depends on the nesting of containers, adding 2 days' worth of entries for every layer:

Dropped directly on a form: 96 entries (2 days @ 30 minute increments)
Dropped in a panel on a form: 192 entries (4 days)
Dropped in a panel in a TableLayout cell on a form: 288 entries (6 days)

It's like Items.Clear() is failing to do anything. I'm trying to figure out why. Here is my complete class code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace PCA.SecureWinCtrls
{
    [Description("Control for displaying a time selection combox box with custom time increments. Includes provision for PCA securiyt model."),
    ToolboxBitmap(typeof(ComboBox))]
    public sealed class pcaTimePicker : ComboBox
    {
        #region MEMBERS

        /// <summary>
        /// Override of the base ComboBox property. Style cannot be altered due to the specific
        /// use of this control.
        /// </summary>
        [Browsable(false)]
        public new ComboBoxStyle DropDownStyle
        {
            get
            {
                return ComboBoxStyle.DropDown;
            }
        }

        /// <summary>
        /// Gets the current hours value. If no value is set, and exception will be thrown.
        /// </summary>
        [Browsable(false)]
        public int Hours
        {
            get 
            {
                if (ShowMilitary)
                    return MilHours;
                int i = MilHours;
                if (i > 12)
                    return i - 12;
                else if (i == 0)
                    return 12;
                else
                    return i;
            }
        }

        /// <summary>
        /// Gets the current hours as a military time value, regardless of display mode. If no
        /// value is set, an exception will be thrown.
        /// </summary>
        [Browsable(false)]
        public int MilHours
        {
            get
            {
                ValidateValue(true);
                return DateTime.Parse(Text).Hour;
            }
        }

        /// <summary>
        /// Gets the current minutes value. If no value is set, and exception will be thrown.
        /// </summary>
        [Browsable(false)]
        public int Minutes
        {
            get
            {
                ValidateValue(true);
                return DateTime.Parse(Text).Minute;
            }
        }

        private bool mbMilitary = false;
        /// <summary>
        /// Gets/sets whether the control shows time as a 24-hour clock.
        /// </summary>
        [Description("Gets/sets whether the control shows time as a 24-hour clock."),
        DefaultValue(false)]
        public bool ShowMilitary
        {
            get { return mbMilitary; }
            set
            {
                mbMilitary = value;
                RepopulateChoices();
            }
        }

        private int miIncrement = 30;
        /// <summary>
        /// Gets/sets the increment of the drop-down.
        /// </summary>
        [Description("Gets/sets the increment value in minutes. Valid values are 10 - 720."),
        DefaultValue(30)]
        public int Increment
        {
            get { return miIncrement; }
            set
            {
                if (value < 10 || value > 720)
                {
                    throw new Exception("Increment value must be a value in minutes ranging from 10 to 720 (12 hours).");
                }
                miIncrement = value;
                RepopulateChoices();
            }
        }

        #endregion

        #region EVENT HANDLERS

        /// <summary>
        /// Event fired when the control performs self-validation and fails.
        /// </summary>
        [Description("Event fired when the control loses focus and the text value cannot be parsed as time.")]
        public event EventHandler ValidationFailed;
        /// <summary>
        /// Event fired when the control performs self-validation and succeeds.
        /// </summary>
        public event EventHandler ValidationSucceeded;

        #endregion

        #region PUBLIC METHODS

        /// <summary>
        /// Constructor.
        /// </summary>
        public pcaTimePicker() : base()
        {
            base.DropDownStyle = ComboBoxStyle.DropDown;
            base.Leave += new EventHandler(this_Leave);
            RepopulateChoices();
        }

        /// <summary>
        /// Triggers immediate validation of the control's current value.
        /// </summary>
        /// <param name="bCauseException">Indicates whether an exception should be thrown
        /// if the validation fails.</param>
        public void ForceValidation(bool bCauseException)
        {
            ValidateValue(bCauseException);
        }

        #endregion

        #region PRIVATE METHODS

        /// <summary>
        /// Repopulates the combo box based on the current military and increment settings.
        /// </summary>
        private void RepopulateChoices()
        {
            Items.Clear();
            int i = 0;
            while (i < 1440)
            {
                DateTime dat = new DateTime(1, 1, 1, i / 60, i % 60, 0);
                if (ShowMilitary)
                    Items.Add(dat.ToString("HH:mm"));
                else
                    Items.Add(dat.ToString("h:mm tt"));
                i += Increment;
            }
        }

        /// <summary>
        /// Checkes that the current value of the control is a parsable time.
        /// </summary>
        private void ValidateValue(bool bOnPropertyAccess)
        {
            bool bOK = true;
            try
            {
                DateTime.Parse(base.Text);
            }
            catch (Exception)
            {
                bOK = false;
                if (ValidationFailed != null)
                {
                    ValidationFailed(this, null);
                    if (bOnPropertyAccess)
                        throw;
                }
                else
                    throw;
            }
            if (bOK && ValidationSucceeded != null)
                ValidationSucceeded(this, null);
        }

        #endregion

        #region EVENT HANDLERS

        private void this_Leave(object sender, EventArgs e)
        {
            ValidateValue(false);
        }

        #endregion
    }
}

AnswerRe: ComboBox.Items.Clear() not working -- Totally Revised Question Pin
Xpnctoc20-Jan-10 4:20
Xpnctoc20-Jan-10 4:20 
AnswerRe: ComboBox.Items.Clear() not working -- Totally Revised Question Pin
Dan Mos20-Jan-10 4:53
Dan Mos20-Jan-10 4:53 
AnswerRe: ComboBox.Items.Clear() not working -- Totally Revised Question Pin
Covean20-Jan-10 5:02
Covean20-Jan-10 5:02 
GeneralRe: ComboBox.Items.Clear() not working -- Totally Revised Question Pin
Xpnctoc20-Jan-10 5:19
Xpnctoc20-Jan-10 5:19 
GeneralRe: ComboBox.Items.Clear() not working -- Totally Revised Question Pin
Covean20-Jan-10 5:27
Covean20-Jan-10 5:27 
GeneralRe: ComboBox.Items.Clear() not working -- Totally Revised Question Pin
DaveyM6920-Jan-10 7:55
professionalDaveyM6920-Jan-10 7:55 
GeneralRe: ComboBox.Items.Clear() not working -- Totally Revised Question Pin
Covean20-Jan-10 21:56
Covean20-Jan-10 21:56 
Questionhow to import a video in C # .net? and what is its api?? Pin
rabia javeed20-Jan-10 3:07
rabia javeed20-Jan-10 3:07 
AnswerRe: how to import a video in C # .net? and what is its api?? Pin
Pete O'Hanlon20-Jan-10 3:25
mvePete O'Hanlon20-Jan-10 3:25 
GeneralRe: how to import a video in C # .net? and what is its api?? Pin
rabia javeed23-Jan-10 7:06
rabia javeed23-Jan-10 7:06 
Questionprevent form resizing Pin
Jassim Rahma20-Jan-10 2:51
Jassim Rahma20-Jan-10 2:51 
AnswerRe: prevent form resizing Pin
Keith Barrow20-Jan-10 2:57
professionalKeith Barrow20-Jan-10 2:57 
GeneralRe: prevent form resizing Pin
Jassim Rahma20-Jan-10 3:00
Jassim Rahma20-Jan-10 3:00 
AnswerRe: prevent form resizing Pin
PIEBALDconsult20-Jan-10 3:19
mvePIEBALDconsult20-Jan-10 3:19 
AnswerRe: prevent form resizing Pin
Nuri Ismail20-Jan-10 3:24
Nuri Ismail20-Jan-10 3:24 
AnswerRe: prevent form resizing Pin
Rob Philpott20-Jan-10 5:12
Rob Philpott20-Jan-10 5:12 
Questiontwo titles on title bar Pin
Jassim Rahma20-Jan-10 2:46
Jassim Rahma20-Jan-10 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.