Exemple #1
0
        /// <summary>
        /// add new value of combobox to the user defaults, or move existing value to the front;
        /// limits the number of values to MAX_COMBOBOX_HISTORY
        /// </summary>
        /// <param name="Sender"></param>
        /// <param name="e"></param>
        public void AddComboBoxHistory(System.Object Sender, TAcceptNewEntryEventArgs e)
        {
            string keyName = "CmbHistory" + ((Control)Sender).Name;
            StringCollection values = StringHelper.StrSplit(TUserDefaults.GetStringDefault(keyName, ""), ",");

            values.Remove(e.ItemString);
            values.Insert(0, e.ItemString);

            while (values.Count > MAX_COMBOBOX_HISTORY)
            {
                values.RemoveAt(values.Count - 1);
            }

            TUserDefaults.SetDefault(keyName, StringHelper.StrMerge(values, ','));
        }
        /// <summary>
        /// ask the user if he wants to add this new item to the combobox
        /// </summary>
        /// <param name="mArgs"></param>
        protected void AskUserAcceptNewEntries(TAcceptNewEntryEventArgs mArgs)
        {
            if (mArgs.Cancel == true)
            {
                return;
            }

            string mMessage = "Do you want to add this item >" + mArgs.ItemString + "< to the list of this combobox?";
            string mCaption = "Confirm adding this item!";
            System.Windows.Forms.MessageBoxButtons mButtons = System.Windows.Forms.MessageBoxButtons.OKCancel;
            System.Windows.Forms.MessageBoxIcon mIcon = System.Windows.Forms.MessageBoxIcon.Question;
            System.Windows.Forms.DialogResult mDialogResult = MessageBox.Show(mMessage, mCaption, mButtons, mIcon);

            if (mDialogResult == System.Windows.Forms.DialogResult.Cancel)
            {
                mArgs.Cancel = true;
            }
        }
        /// <summary>
        /// This procedure is called when this control looses its active status and is
        /// no longer the active control of a form.
        /// </summary>
        /// <param name="e">Event Arguments.
        /// </param>
        /// <returns>void</returns>
        protected override void OnLeave(System.EventArgs e)
        {
            base.OnLeave(e);

            if (DesignMode)
            {
                return;
            }

            String mItemString = this.Text;
            TAcceptNewEntryEventArgs mArgs = new TAcceptNewEntryEventArgs();
            mArgs.ItemString = mItemString;

            // Try to find the text entered in the set of items contained in cmbAutoComplete
            System.Int32 mFoundIndex = this.FindStringExact(mItemString);

            if (mFoundIndex >= 0)
            {
                // Text found and identified.
                // TLogging.Log('Text found and identified. mFoundIndex: ' + mFoundIndex.ToString);

                // Note: AlanP.  Special case in Filter/Find
                // Some comboBoxes (not many) have empty string as their first item, in which case mFoundIndex will be 0
                //  but the current SelectedIndex may be -1.
                // We do not want to fire a SelectedIndexChanged event in this case.  (From -1 to 0)
                // Just in case this affects the behaviour of non-Filter/Find situations we AND the test with FIgnoreNewValues
                //   since that is only ever true in Filter/Find (as of July 2014!)
                // (See Mantis 3117, which gives all sorts of problems when shutting down)
                if ((this.FIgnoreNewValues == true) && (mFoundIndex == 0) && (mItemString == String.Empty) && (this.SelectedIndex == -1))
                {
                    // Do nothing (see above)
                }
                else
                {
                    this.SelectedIndex = mFoundIndex;
                }

                if (AcceptNewEntries != null)
                {
                    // we might want to move the value upwards in the history
                    AcceptNewEntries(this, mArgs);
                }
            }
            else
            {
                // Text could not be found.
                if ((this.FAcceptNewValues == true) || (this.FIgnoreNewValues == true))
                {
                    // User may enter new values.
                    if (AcceptNewEntries != null)
                    {
                        // User wants to do something before the new entry is added
                        AcceptNewEntries(this, mArgs);

                        // could make a call to AskUserAcceptNewEntries instead?

                        if (mArgs.Cancel == true)
                        {
                            // User wants to cancel the adding of a new entry => Original value prevails
                            RestoreOriginalItem();
                        }
                        else if (this.FIgnoreNewValues == false)
                        {
                            // Add the new item to the combobox collection
                            OnAcceptNewEntryEvent(mArgs);
                        }
                    }
                    else if (this.FIgnoreNewValues == false)
                    {
                        OnAcceptNewEntryEvent(mArgs);
                    }
                }
                else
                {
                    if ((this.SelectedItem != null) && this.SelectedItem.Equals(System.DBNull.Value))
                    {
                        this.Text = System.DBNull.Value.ToString();
                        this.UInitialString = System.DBNull.Value.ToString();
                    }
                    else
                    {
                        if ((this.Text == String.Empty) && FAllowBlankValue)
                        {
                            // leave a blank item
                        }
                        else
                        {
                            // Otherwise we restore the original
                            RestoreOriginalItem();
                        }
                    }
                }
            }
        }
        /// <summary>
        /// This procedure is called when a new item should be put in the item list.
        /// </summary>
        /// <param name="Args">TAcceptNewEntryEventArgs.
        /// </param>
        /// <returns>void</returns>
        private void OnAcceptNewEntryEvent(TAcceptNewEntryEventArgs Args)
        {
            System.Int32 mNumDataSourceCols = this.GetNumberOfDataSourceCols();

            if ((mNumDataSourceCols != 1) || (Args.Cancel == true))
            {
                MessageBox.Show("Item could not be added to the items collection!", "Confirm adding this item!");
                return;
            }

            this.AddItemToDataSource(Args.ItemString);
            this.SelectedIndex = this.FindStringExact(Args.ItemString);
            this.Text = Args.ItemString;
        }