Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 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 January 5th, 2007, 09:14 AM
Authorized User
 
Join Date: Feb 2006
Posts: 63
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Hannibal
Default disable tabpage in windows application?

Hi.

I have a windows application in c#. In the application i have put a tabcontrol, which has 2 tabpages. In the form load, i want to disable the tabpage2. How to disable the tabpage2?


 
Old January 16th, 2007, 12:09 PM
Authorized User
 
Join Date: Apr 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

tabControl has an array of tabpages and i think it's possible to do something like this:

tabControl.TabPages[1].Enabled = false;

this disables all controls for this tabpage and not the tabpage itself

 
Old January 22nd, 2007, 11:13 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What about making the tab not visible? That would certainly disable it.

-Peter
 
Old January 23rd, 2007, 03:43 PM
Authorized User
 
Join Date: Apr 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I guess that's possible but i think that you need to remove the tabpage from the tabpage collection in your tabcontrol for the visible property to do something

 
Old February 1st, 2007, 11:11 AM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Here you hava a CustomTabControl class that allows you to have disabled tab pages:

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace org.mmakers.controls {

    /// <summary>
    /// Custom tab control that allows its tab pages to be enabled/disabled
    /// </summary>
    public class CustomTabControl : TabControl {

        /// <summary>
        /// Default constructor
        /// </summary>
        public CustomTabControl() : base() {
            // Set the draw mode property to owner draw
            DrawMode = TabDrawMode.OwnerDrawFixed;
        }

        /// <summary>
        /// Called whenever the control is going to be drawn
        /// </summary>
        /// <param name="e">The event arguments</param>
        protected override void OnDrawItem(DrawItemEventArgs e) {
            base.OnDrawItem(e);

            TabPage tabPage = TabPages[e.Index];
            Rectangle tabRectangle = GetTabRect(e.Index);

            if (Alignment == TabAlignment.Left || Alignment == TabAlignment.Right) {
                float rotateAngle = 90;
                if (Alignment == TabAlignment.Left) {rotateAngle = 270;}
                PointF cp = new PointF(tabRectangle.Left + (tabRectangle.Width / 2),tabRectangle.Top + (tabRectangle.Height / 2));
                e.Graphics.TranslateTransform(cp.X,cp.Y);
                e.Graphics.RotateTransform(rotateAngle);
                tabRectangle = new Rectangle(-(tabRectangle.Height / 2),-(tabRectangle.Width / 2),tabRectangle.Height,tabRectangle.Width);
            }
            using (SolidBrush foreBrush = new SolidBrush(tabPage.ForeColor)) {
                using (SolidBrush backBrush = new SolidBrush(tabPage.BackColor)) {
                    if (!tabPage.Enabled) {foreBrush.Color = SystemColors.GrayText;}
                    e.Graphics.FillRectangle(backBrush,tabRectangle);
                    using (StringFormat stringFormat = new StringFormat()) {
                        stringFormat.Alignment = StringAlignment.Center;
                        stringFormat.LineAlignment = StringAlignment.Center;
                        e.Graphics.DrawString(tabPage.Text,e.Font,foreBrush,tabRectangle,stringFormat);
                    }
                }
            }
            e.Graphics.ResetTransform();
        }

        /// <summary>
        /// Called whenever a tab page is going to be selected
        /// </summary>
        /// <param name="e">The event arguments</param>
        protected override void OnSelecting(TabControlCancelEventArgs e) {
            base.OnSelecting(e);

            // If tab page is disabled, cancel the event
            if (!e.TabPage.Enabled) {e.Cancel = true;}
        }

        /// <summary>
        /// Called whenever a tab page is added to the tab control
        /// </summary>
        /// <param name="e">The event arguments</param>
        protected override void OnControlAdded(ControlEventArgs e) {
            // Add a enabled-changed listener to the tab page so we can repaint the tab control
            TabPage tabPage = e.Control as TabPage;
            if (tabPage != null) {tabPage.EnabledChanged += new EventHandler(tabPage_EnabledChanged);}
            base.OnControlAdded(e);
        }

        /// <summary>
        /// Called whenever a tab page is removed from the tab control
        /// </summary>
        /// <param name="e">The event arguments</param>
        protected override void OnControlRemoved(ControlEventArgs e) {
            // Remove the enabled-changed listener
            TabPage tabPage = e.Control as TabPage;
            if (tabPage != null) {tabPage.EnabledChanged -= new EventHandler(tabPage_EnabledChanged);}
            base.OnControlRemoved(e);
        }

        /// <summary>
        /// Called whenever the enabled property of a tab page changes
        /// </summary>
        /// <param name="sender">The event sender object</param>
        /// <param name="e">The event arguments</param>
        private void tabPage_EnabledChanged(object sender,EventArgs e) {
            // Invalidate the tab page title so it's redrawn again
            TabPage tabPage = sender as TabPage;
            TabControl tabControl = tabPage.Parent as TabControl;
            tabControl.Invalidate(tabPage.ClientRectangle);
        }

    }
}
Maybe it's optimizable in some points, but it works and that was all I needed. When used from the windows designer, I always put the DrawMode property to OwnerDrawFixed (because the WFD will overwrite the property setted at the constructor), but maybe it's not necessary. As maybe it's not necessary to call the base class method in the OnDrawItem() override. When I have some time I will test it.

Any sugerences would be appreciated! ;)



 
Old February 7th, 2007, 12:44 PM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nice job with providing this class, however I am unable to access the Enabled property on the following line:

if (!tabPage.Enabled) {foreBrush.Color = SystemColors.GrayText;}

Enabled does not show up. I am using VS2005 with V2.0 of the Framework.

-- Lance






Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable User control in Windows Sebastiaan VBScript 3 June 17th, 2007 04:20 PM
Web Application OR Windows Application adesilva .NET Framework 2.0 2 May 4th, 2007 07:12 AM
Tabpage in runtime g0dchild_85 C# 3 August 9th, 2006 09:29 AM
Web application Vs Windows Application Ned .NET Web Services 2 January 20th, 2004 01:27 PM
Disabling Tabpage rajustha VB How-To 0 December 9th, 2003 10:11 PM





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