Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / February 2005

Tip: Looking for answers? Try searching our database.

Make ComboBox ReadOnly

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jean-dot-paul-at-opelwilly-dot-com - 22 Feb 2005 20:57 GMT
Hello,

Is there a way to make a combobox readOnly instead of disabling it.

tnx

Jean Paul
Herfried K. Wagner [MVP] - 22 Feb 2005 22:03 GMT
"jean-dot-paul-at-opelwilly-dot-com" <i@a> schrieb:
> Is there a way to make a combobox readOnly instead of disabling it.

Store the selected index somewhere and reset it in the combobox's
'SelectedIndexChanged' event.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

Bruce Wood - 22 Feb 2005 23:34 GMT
You can override the combo box and override various On... methods so as
to swallow mouse clicks and any typing that might change the combo
box's selected index.
Bruce Wood - 22 Feb 2005 23:37 GMT
I meant inherit. You can inherit from ComboBox and override various
On... methods.

I think it's time to go home. :)
jean-dot-paul-at-opelwilly-dot-com - 24 Feb 2005 08:15 GMT
Hello Bruce,

Good idee but how can I swallow the mouse clicks, there is no such thing as
Handled

geerts
Jean Paul

> You can override the combo box and override various On... methods so as
> to swallow mouse clicks and any typing that might change the combo
> box's selected index.
Bruce Wood - 24 Feb 2005 19:23 GMT
Easy:  just don't call the base method, which is the method that takes
action on the MouseDown, etc.
jean-dot-paul-at-opelwilly-dot-com - 25 Feb 2005 07:39 GMT
Bruce,

That do not seem te work. I created wy own 'public class roComboBox :
System.Windows.Forms.ComboBox '
Created a property ReadOnly and call only the base events if the ReadOnly is
false (see code beneath) but the mousclics still pass.

Who knows what Im doing wrong

protected override void OnMouseDown(MouseEventArgs e)

{

   if (!lokReadOnly)

  {

       base.OnMouseDown(e);

   }

}

protected override void OnMouseUp(MouseEventArgs e)

{

   if (!lokReadOnly)

   {

       base.OnMouseUp (e);

   }

}

protected override void OnClick(EventArgs e)

{

   if (!lokReadOnly)

   {

       base.OnClick (e);

   }

}

public bool ReadOnly

{

   get

   {

       return lokReadOnly;

   }

   set

   {

       lokReadOnly = value;

       if (lokReadOnly)

       {

           this.BackColor = Color.Yellow;

       }

       else

       {

           this.BackColor = Color.White;

       }

   }

}

> Easy:  just don't call the base method, which is the method that takes
> action on the MouseDown, etc.
Bruce Wood - 25 Feb 2005 17:27 GMT
Sorry... I misled you. You can't do anything about the mouse clicks...
you have to change the DropDownStyle of the combo box. Here is the code
for my read-only  combo box:

    /// <summary>
    /// A combo box that offers a <c>ReadOnly</c> property that
    /// will display the currently selected item clearly but not
    /// allow the user to change it.
    /// </summary>
    public class ComboBoxWithReadOnly : System.Windows.Forms.ComboBox
    {
        #region Private members

        private bool ro;
        private Color roBackColor;
        private Color roForeColor;
        private Color backColorSave;
        private Color foreColorSave;
        private ComboBoxStyle dropDownStyleSave;
        private ContextMenu contextMenuSave;

        #endregion

        #region Constructor

        /// <summary>
        /// Creates a new combo box.
        /// </summary>
        public ComboBoxWithReadOnly()
        {
            this.ro = false;
            this.roBackColor = SystemColors.Control;
            this.roForeColor = SystemColors.WindowText;
        }

        #endregion

        #region Public properties

        /// <summary>
        /// Sets the combo box so that it still has the standard
        /// (enabled) appearance, but it cannot be manipulated by the
        /// user.
        /// </summary>
        /// <value><c>true</c> if the user should not be allowed to
        /// change the currently selected item in the combo box, <c>false</c>
if the user should
        /// be able to change the combo box contents.</value>
        [Description("Controls whether the user can change the combo box
state"), Category("Behavior"), DefaultValue(false)]
        public bool ReadOnly
        {
            get { return this.ro; }
            set
            {
                if (this.ro != value)
                {
                    this.ro = value;
                    if (this.ReadOnly)
                    {
                        this.backColorSave = base.BackColor;
                        this.foreColorSave = base.ForeColor;
                        this.dropDownStyleSave = base.DropDownStyle;
                        this.contextMenuSave = base.ContextMenu;

                        base.BackColor = this.roBackColor;
                        base.ForeColor = this.roForeColor;
                        base.DropDownStyle = ComboBoxStyle.Simple;
                        base.ContextMenu = new ContextMenu();
                    }
                    else
                    {
                        base.BackColor = this.backColorSave;
                        base.ForeColor = this.foreColorSave;
                        base.DropDownStyle = this.dropDownStyleSave;
                        base.ContextMenu = this.contextMenuSave;
                    }
                }
            }
        }

        /// <summary>
        /// Gets or sets a value specifying the style of the combo box.
        /// This property replaces the <see cref="ComboBox.DropDownStyle"/>
property.
        /// </summary>
        new public ComboBoxStyle DropDownStyle
        {
            get { return base.DropDownStyle; }
            set
            {
                if (this.ReadOnly)
                {
                    this.dropDownStyleSave = value;
                }
                else
                {
                    base.DropDownStyle = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the shortcut menu associated with the control.
        /// This property overrides the <see cref="Control.ContextMenu"/>
property.
        /// </summary>
        public override ContextMenu ContextMenu
        {
            get { return base.ContextMenu; }
            set
            {
                if (this.ReadOnly)
                {
                    this.contextMenuSave = value;
                }
                else
                {
                    base.ContextMenu = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the background color for the combo box.
        /// This property overrides the <see cref="Control.BackColor"/>
property.
        /// </summary>
        public override Color BackColor
        {
            get { return base.BackColor; }
            set
            {
                if (this.ReadOnly)
                {
                    this.backColorSave = value;
                }
                else
                {
                    base.BackColor = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the foreground color for the combo box.
        /// This property overrides the <see cref="Control.ForeColor"/>
property.
        /// </summary>
        public override Color ForeColor
        {
            get { return base.ForeColor; }
            set
            {
                if (this.ReadOnly)
                {
                    this.foreColorSave = value;
                }
                else
                {
                    base.ForeColor = value;
                }
            }
        }

        #endregion

        #region Methods for implementing read-only

        /// <summary>
        /// This method throws away keypresses in "read-only" mode,
preventing
        /// the user from interacting with the control.
        /// </summary>
        /// <param name="e">Further information about the key down
event.</param>
        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (this.ro)
            {
                e.Handled = true;
            }
            else
            {
                base.OnKeyUp (e);
            }
        }

        /// <summary>
        /// This method throws away keypresses in "read-only" mode,
preventing
        /// the user from interacting with the control.
        /// </summary>
        /// <param name="e">Further information about the key down
event.</param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (this.ReadOnly)
            {
                e.Handled = true;
            }
            else
            {
                base.OnKeyDown (e);
            }
        }

        /// <summary>
        /// This method throws away keypresses in "read-only" mode,
preventing
        /// the user from interacting with the control.
        /// </summary>
        /// <param name="e">Further information about the key press
event.</param>
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (this.ReadOnly)
            {
                e.Handled = true;
            }
            else
            {
                base.OnKeyPress (e);
            }
        }

        #endregion
    }
}
jean-dot-paul-at-opelwilly-dot-com - 26 Feb 2005 07:56 GMT
Bruce,

tnx  for sugestions, nowit works like I want itto work. The lack of being
able to change the background color of a disabled control is solved now. I
had more than once the complain that the disabled controls where not verry
wel readable so this way I have full control of the readebillity.

tnx cuagn,

Jean Paul

> Sorry... I misled you. You can't do anything about the mouse clicks...
> you have to change the DropDownStyle of the combo box. Here is the code
[quoted text clipped - 225 lines]
> }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.