예제 #1
0
        // <summary>
        // Get the text for the specified item.
        // </summary>
        // <param name="item">The item for which to get the message text</param>
        // <remarks>If <see cref="StatusBarPropertyOptions.ShowAsBlank"/> is true, it
        // returns an empty string.  Otherwise, it returns the
        // <see cref="StatusBarPropertyOptions.Message"/> value.  If the item has no
        // status bar text defined, it returns null.</remarks>
        private string ItemText(object item)
        {
            if (item == null || !htOptions.Contains(item))
            {
                return(null);
            }

            TabControl tc = item as TabControl;

            // Show status bar text for the tab page if there is any
            if (tc != null && htOptions.Contains(tc.SelectedTab))
            {
                item = tc.SelectedTab;
            }

            StatusBarPropertyOptions po = (StatusBarPropertyOptions)htOptions[item];

            if (po.ShowAsBlank)
            {
                return(String.Empty);
            }

            return(po.Message);
        }
예제 #2
0
        /// <summary>
        /// This stores the status bar text for the specified component.
        /// </summary>
        /// <param name="comp">The component associated with the message</param>
        /// <param name="message">The status bar text for the component</param>
        /// <remarks>The designer converts strings containing nothing but
        /// spaces to empty strings and won't serialize them to code.  If you
        /// want a blank string to display, use <see cref="SetShowAsBlank"/>
        /// to set the "show as blank" flag to true instead.</remarks>
        /// <exception cref="ArgumentException">This is thrown if the component
        /// is null or if it is not a menu item, control, or a tool strip item.
        /// </exception>
        public void SetStatusBarText(Component comp, string message)
        {
            if (comp == null)
            {
                throw new ArgumentException("Component cannot be null");
            }

            MenuItem   mi  = comp as MenuItem;
            Control    ctl = comp as Control;
            TabControl tc  = comp as TabControl;

            ToolStripItem        ti   = comp as ToolStripItem;
            ToolStripControlHost tsch = comp as ToolStripControlHost;

            if (mi == null && ti == null && ctl == null)
            {
                throw new ArgumentException(
                          "Component must be a MenuItem, ToolStripItem, or a Control");
            }

            if (message != null && message.Length == 0)
            {
                message = null;
            }

            if (!htOptions.Contains(comp))
            {
                htOptions.Add(comp, new StatusBarPropertyOptions(message));

                if (!this.DesignMode && message != null)
                {
                    if (mi != null)
                    {
                        mi.Select += new EventHandler(Menu_Select);
                    }
                    else
                    if (ti != null)
                    {
                        ti.MouseEnter += new EventHandler(Control_Enter);
                        ti.MouseLeave += new EventHandler(Control_Leave);

                        // If it's a control host, hook the enter and
                        // leave events too.
                        if (tsch != null)
                        {
                            tsch.Enter += new EventHandler(Control_Enter);
                            tsch.Leave += new EventHandler(Control_Leave);
                        }
                    }
                    else
                    {
                        ctl.GotFocus += new EventHandler(Control_Enter);
                        ctl.Enter    += new EventHandler(Control_Enter);
                        ctl.Leave    += new EventHandler(Control_Leave);

                        // If it's a tab control, hook the SelectedIndexChanged
                        // event to allow displaying status bar text for it and
                        // its pages.  The tab control and tab pages don't
                        // reliably show their status bar text due to the way
                        // they handle the focus.  As such, this event is
                        // needed to update the text.  Note that it still won't
                        // show the text if you use Shift+Tab to go from the
                        // first control on a tab back to the tab in the tab
                        // control itself or when it is the first control
                        // to have the focus.  You must also set ShowAsBlank or
                        // StatusBarText on the tab control itself for text to
                        // appear for the tab pages.
                        if (tc != null)
                        {
                            tc.SelectedIndexChanged += new EventHandler(
                                Control_Enter);
                        }
                    }
                }
            }
            else
            {
                StatusBarPropertyOptions po = (StatusBarPropertyOptions)htOptions[comp];
                po.Message = message;

                if (!this.DesignMode && message == null &&
                    po.ShowAsBlank == false)
                {
                    if (mi != null)
                    {
                        mi.Select -= new EventHandler(Menu_Select);
                    }
                    else
                    if (ti != null)
                    {
                        ti.MouseEnter -= new EventHandler(Control_Enter);
                        ti.MouseLeave -= new EventHandler(Control_Leave);

                        // If it's a control host, unhook the enter and
                        // leave events too.
                        if (tsch != null)
                        {
                            tsch.Enter -= new EventHandler(Control_Enter);
                            tsch.Leave -= new EventHandler(Control_Leave);
                        }
                    }
                    else
                    {
                        ctl.GotFocus -= new EventHandler(Control_Enter);
                        ctl.Enter    -= new EventHandler(Control_Enter);
                        ctl.Leave    -= new EventHandler(Control_Leave);

                        if (tc != null)
                        {
                            tc.SelectedIndexChanged -= new EventHandler(
                                Control_Enter);
                        }
                    }
                }
            }

            // Refresh the text if the control is focused
            if (message != null && ctl != null && ctl.Focused)
            {
                Control_Enter(ctl, EventArgs.Empty);
            }
        }
예제 #3
0
        /// <summary>
        /// This stores the "show as blank" flag for the specified component.
        /// </summary>
        /// <param name="comp">The component associated with the property</param>
        /// <param name="showBlank">The flag value for the component</param>
        /// <remarks>The designer converts strings containing nothing but
        /// spaces to empty strings and this equates to the default and the
        /// value is not serialized to code. If you want a blank string to
        /// display, set this property to true instead.  This property takes
        /// precedence over the <b>StatusBarText</b> property.</remarks>
        /// <exception cref="ArgumentException">This is thrown if the component
        /// is null or if it is not a menu item, control, or a tool strip item.
        /// </exception>
        public void SetShowAsBlank(Component comp, bool showBlank)
        {
            if (comp == null)
            {
                throw new ArgumentException("Component cannot be null");
            }

            MenuItem   mi  = comp as MenuItem;
            Control    ctl = comp as Control;
            TabControl tc  = comp as TabControl;

            ToolStripItem        ti   = comp as ToolStripItem;
            ToolStripControlHost tsch = comp as ToolStripControlHost;

            if (mi == null && ti == null && ctl == null)
            {
                throw new ArgumentException(
                          "Component must be a MenuItem, ToolStripItem, or a Control");
            }

            if (!htOptions.Contains(comp))
            {
                htOptions.Add(comp, new StatusBarPropertyOptions(showBlank));

                // Hook up the event handlers if necessary
                if (!this.DesignMode && showBlank)
                {
                    if (mi != null)
                    {
                        mi.Select += new EventHandler(Menu_Select);
                    }
                    else
                    if (ti != null)
                    {
                        ti.MouseEnter += new EventHandler(Control_Enter);
                        ti.MouseLeave += new EventHandler(Control_Leave);

                        // If it's a control host, hook the enter and
                        // leave events too.
                        if (tsch != null)
                        {
                            tsch.Enter += new EventHandler(Control_Enter);
                            tsch.Leave += new EventHandler(Control_Leave);
                        }
                    }
                    else
                    {
                        ctl.GotFocus += new EventHandler(Control_Enter);
                        ctl.Enter    += new EventHandler(Control_Enter);
                        ctl.Leave    += new EventHandler(Control_Leave);

                        // See SetStatusBarText for why we do this
                        if (tc != null)
                        {
                            tc.SelectedIndexChanged += new EventHandler(
                                Control_Enter);
                        }
                    }
                }
            }
            else
            {
                StatusBarPropertyOptions po = (StatusBarPropertyOptions)htOptions[comp];
                po.ShowAsBlank = showBlank;

                // Disconnect the event handlers if necessary
                if (!this.DesignMode && po.Message == null && showBlank == false)
                {
                    if (mi != null)
                    {
                        mi.Select -= new EventHandler(Menu_Select);
                    }
                    else
                    if (ti != null)
                    {
                        ti.MouseEnter -= new EventHandler(Control_Enter);
                        ti.MouseLeave -= new EventHandler(Control_Leave);

                        // If it's a control host, unhook the enter and
                        // leave events too.
                        if (tsch != null)
                        {
                            tsch.Enter -= new EventHandler(Control_Enter);
                            tsch.Leave -= new EventHandler(Control_Leave);
                        }
                    }
                    else
                    {
                        ctl.GotFocus -= new EventHandler(Control_Enter);
                        ctl.Enter    -= new EventHandler(Control_Enter);
                        ctl.Leave    -= new EventHandler(Control_Leave);

                        if (tc != null)
                        {
                            tc.SelectedIndexChanged -= new EventHandler(
                                Control_Enter);
                        }
                    }
                }
            }

            // Refresh the text if the control is focused
            if (showBlank && ctl != null && ctl.Focused)
            {
                Control_Enter(ctl, EventArgs.Empty);
            }
        }