C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




tabControl with Hotkeys and Disabling individual tab pages features


Posted Date: 15 Aug 2008    Resource Type: Code Snippets    Category: Winforms Controls
Author: Bhushan PoojaryMember Level: Silver    
Rating: Points: 10



Issues with existing Tab Control are
1: It does not support Hotkey features for navigation.
2: It does not support individual disabling of tab pages as in VB6.

In my code I have enhanced the existing control in such a manner that these features are incorporated.

Hotkeys: place & just before any character in the tab page text property .And then press Alt and that character automatically that tab page would be selected.

Disabling of tabpages is taken care by one method SetTabPageEnable which gives you exact output as in VB6.In current .NET TabControl you cannot disable any Tab Page.






using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;


namespace Bhushan.Poojary.WinForm.Controls
{
public class TabControl : System.Windows.Forms.TabControl
{



#region Windows Form Designer generated code

public TabControl()
: base()
{


// This call is required by the Windows Form Designer.
InitializeComponent();


this.DrawMode = TabDrawMode.OwnerDrawFixed;//So that mnemonics are shown properly in tab pages
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (!((components == null)))
{
components.Dispose();
}
}
base.Dispose(disposing);
}


// Required by the Windows Form Designer
private System.ComponentModel.IContainer components;

// NOTE: The following procedure is required by the Windows Form Designer
// It can be modified using the Windows Form Designer.
// Do not modify it using the code editor.
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}


#endregion

#region IControl Implementation




#endregion



#region tab page enable disable implementaion
System.Collections.Generic.Dictionary<string, bool> _enableTabPageStatus = new System.Collections.Generic.Dictionary<string, bool>();
public void SetTabPageEnabled(int tabPageIndex, bool enabled)
{
string tapPageName = this.TabPages[tabPageIndex].Name;
SetTabPageEnabled(tapPageName, enabled);
}

public void SetTabPageEnabled(string tabPageName, bool enabled)
{
if (_enableTabPageStatus.ContainsKey(tabPageName))
{
_enableTabPageStatus[tabPageName] = enabled;
}
else
{
_enableTabPageStatus.Add(tabPageName, enabled);
}
}

public bool IsTabPageEnabled(int tabPageIndex)
{
string tapPageName = this.TabPages[tabPageIndex].Name;
return IsTabPageEnabled(tapPageName);

}
public bool IsTabPageEnabled(string tabPageName)
{
if (_enableTabPageStatus.ContainsKey(tabPageName))
{
return _enableTabPageStatus[tabPageName];
}
else
{//Assume by default enabled
return true;
}
}

protected override void OnSelecting(TabControlCancelEventArgs e)
{
try
{
base.OnSelecting(e);
if (e.Cancel != true)
{
e.Cancel = !this.IsTabPageEnabled(e.TabPage.Name);
}
}
catch (Exception exception)
{

}
}

#endregion


#region Overriden

protected override bool ProcessMnemonic(char charCode)
{
foreach (TabPage page in this.TabPages)
{
if (Control.IsMnemonic(charCode, page.Text))
{
this.SelectedTab = page;
this.Focus();
return true;
}
}
return false;
}

/// <summary>

protected override void OnDrawItem(DrawItemEventArgs e)
{

base.OnDrawItem(e);
Graphics g = null;
try
{
DrawItemImplementation(e, g);

}
catch (Exception ex)
{
//eat the exception. You dont want drawing exception to bubble up.
}
finally
{
if (g != null)
{
g.Dispose();
}
}
}

/// <summary>

/// </summary>
/// <param name="e"></param>
/// <param name="g"></param>
/// <returns></returns>
protected virtual void DrawItemImplementation(DrawItemEventArgs e, Graphics g)
{
g = e.Graphics;
StringFormat sf = new StringFormat();
TabPage tp = this.TabPages[e.Index];
Rectangle rt = this.GetTabRect(e.Index);

if (e.Index == this.SelectedIndex)
{
//For the current Tab draw little below so that the while line
//will disappear making the current tab stand out. Otherwise it is hard to tell which is
// the current tab.
Rectangle rt2 = rt;
rt2.Inflate(0, 1);
g.FillRectangle(System.Drawing.SystemBrushes.Control, rt2);
}

rt.Offset(tp.Margin.Left, tp.Margin.Top);
if (tp.ImageIndex > -1)
{
Image im = this.ImageList.Images[tp.ImageIndex];
g.DrawImage(im, rt.Left, rt.Top);
//Ofsset the start so that the text placed properly
rt.Offset(im.Size.Width + tp.Margin.Left, 0);
}

Brush drawingBrush = GetBrush(tp);

sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
g.DrawString(tp.Text,
this.Font,
drawingBrush,
rt,
sf);
}
protected virtual Brush GetBrush(TabPage tp)
{
Brush drawingBrush = (this.IsTabPageEnabled(tp.Name)) ? SystemBrushes.WindowText : SystemBrushes.InactiveCaption;
return drawingBrush;
}

#endregion
}//class

}//namespace


Attachments

  • TabControl with Hotkeys and Disabling individual tab pages features (20269-151357-TabControl.rar)



  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    TABCONTROL HOTKEY DISABLE ENABLE FEATURES ADDED  .  Enhanced tabcontrol hotkey  .  Enhanced tabcontrol  .  Enhacned tabcontrol with enable feature  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Progress bar sample in VB.net windows Application
    Previous Resource: Displaying Directory Structure of Local Hard Drive
    Return to Discussion Resource Index
    Post New Resource
    Category: Winforms Controls


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers   BizTalk Adaptors    Web Design


    Contact Us    Privacy Policy    Terms Of Use