Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 5th, 2007, 09:14 AM
Authorized User
 
Join Date: Feb 2006
Location: Bangalore, Karnataka, India.
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?


Reply With Quote
  #2 (permalink)  
Old January 16th, 2007, 12:09 PM
Authorized User
 
Join Date: Apr 2006
Location: , , .
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

Reply With Quote
  #3 (permalink)  
Old January 22nd, 2007, 11:13 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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

-Peter
Reply With Quote
  #4 (permalink)  
Old January 23rd, 2007, 03:43 PM
Authorized User
 
Join Date: Apr 2006
Location: , , .
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

Reply With Quote
  #5 (permalink)  
Old February 1st, 2007, 11:11 AM
Registered User
 
Join Date: Feb 2007
Location: , , .
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! ;)



Reply With Quote
  #6 (permalink)  
Old February 7th, 2007, 12:44 PM
Registered User
 
Join Date: Feb 2007
Location: , , .
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

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable User control in Windows Sebastiaan VBScript 3 June 17th, 2007 05:20 PM
Web Application OR Windows Application adesilva .NET Framework 2.0 2 May 4th, 2007 08:12 AM
Tabpage in runtime g0dchild_85 C# 3 August 9th, 2006 10: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



All times are GMT -4. The time now is 01:55 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.