2012-01-31 7 views
3

私はTabControlを無効にするか、有効にするため、無効にする必要はありません。しかし、TabControlが無効になっているときは、タブページが無効になっているように見えます(グレー表示)。TabControl内のTabPageを無効にする方法はありますか?

+1

はこの 'WinForms'ですか? – Tigran

+0

可能な複製:http://stackoverflow.com/questions/418006/how-can-i-disable-a-tab-inside-a-tabcontrol。その質問に対する最善の答えはhttp://stackoverflow.com/a/418033/635634 –

+0

です。申し訳ありませんが、それを言いました。 –

答えて

6

以下に記載されている人々は、トリックを個別には行いませんが、それらは組み合わされます。それが動作するこのコードを使用してみてください

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     //Disable tabPage2 
     this.tabPage2.Enabled = false; // no casting required. 
     this.tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting); 
     this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed; 
     this.tabControl1.DrawItem += new DrawItemEventHandler(DisableTab_DrawItem); 
    } 
    private void DisableTab_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     TabControl tabControl = sender as TabControl; 
     TabPage page = tabControl.TabPages[e.Index]; 
     if (!page.Enabled) 
     { 
      //Draws disabled tab 
      using (SolidBrush brush = new SolidBrush(SystemColors.GrayText)) 
      { 
       e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3); 
      } 
     } 
     else 
     { 
      // Draws normal tab 
      using (SolidBrush brush = new SolidBrush(page.ForeColor)) 
      { 
       e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3); 
      } 
     } 
    } 

    private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e) 
    { 
     //Cancels click on disabled tab. 
     if (!e.TabPage.Enabled) 
      e.Cancel = true; 
    } 
} 
+0

ありがとう、私はちょうど試みたが、私はタブページのコントロールでEnabledプロパティが表示されません。どのようにあなたのものを手に入れましたか? –

+0

それをキャストすることもできます。 ((Control)tabPage2).Enabled = false; 私は両方の方法をテストし、それらは機能します。 – Corylulu

+0

私はそれを試してみましょう。 –

3

:これを試してみて

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e) 
     { 
      if (e.TabPage == tabControl1.TabPages[1]) 
      { 
       e.Cancel = true; 
      }    
     } 

は、あなたがもし状態で無効にするタブページのインデックスまたは名前にしてくださいここで私は1 としてインデックスを保持しています:)

2

@Cylyluluが提供する書き換えソリューションは、コントロール自体のすべてをカプセル化しません。

public class DimmableTabControl : TabControl 
{ 
    public DimmableTabControl() 
    { 
     DrawMode = TabDrawMode.OwnerDrawFixed; 
     DrawItem += DimmableTabControl_DrawItem; 
     Selecting += DimmableTabControl_Selecting; 
    } 

    private void DimmableTabControl_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     TabPage page = TabPages[e.Index]; 
     using(SolidBrush brush = new SolidBrush(page.Enabled ? page.ForeColor : SystemColors.GrayText)) 
     { 
      e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3); 
     } 
    } 

    private void DimmableTabControl_Selecting(object sender, TabControlCancelEventArgs e) 
    { 
     if(!e.TabPage.Enabled) 
     { 
      e.Cancel = true; 
     } 
    } 
} 
関連する問題