我想做一個測驗,通過問題,請記住,雖然問題1正在使用,其他人被禁用。一旦點擊下一步按鈕,它應該直接更改爲Q2,禁用Q1等。我如何禁用選項卡,以便用戶不能更改它?
如何使其禁用上一個選項卡並在點擊下一步按鈕後保持當前選項的啓用?
我想做一個測驗,通過問題,請記住,雖然問題1正在使用,其他人被禁用。一旦點擊下一步按鈕,它應該直接更改爲Q2,禁用Q1等。我如何禁用選項卡,以便用戶不能更改它?
如何使其禁用上一個選項卡並在點擊下一步按鈕後保持當前選項的啓用?
選項卡可以通過它的索引來訪問,就像這樣:
tabControl.TabPages[0]
所以,說你開始在標籤1(索引= 0),要禁用所有其他選項卡。
// This can be done manually in the designer as well.
foreach(TabPage tab in tabControl.TabPages)
{
tab.Enabled = false;
}
(tabControl.TabPages[0] as TabPage).Enabled = true;
現在,當你按下一步按鈕,要禁用當前選項卡,選中下一個,並轉到下一個。但請記住檢查標籤是否存在!
if(tabControl.TabCount - 1 == tabControl.SelectedIndex)
return; // No more tabs to show!
tabControl.SelectedTab.Enabled = false;
var nextTab = tabControl.TabPages[tabControl.SelectedIndex+1] as TabPage;
nextTab.Enabled = true;
tabControl.SelectedTab = nextTab;
免責聲明:這不是測試,但它應該是沿着這些路線的東西。
你說你得到一個關於不包含啓用定義的對象的錯誤 - 我的代碼將每個標籤頁作爲TabPage進行類型轉換。但是我沒有測試過它。
我遵循這種方式:
i)具有currentIndex值的global。
ii)將SelectedIndexChanged事件處理程序添加到tabControl。
iii)在SelectedIndexChanged處理程序中,將索引設置回currentIndex。
四)變更CURRENTINDEX在Next按鈕點擊事件
這可能工作:
currentIndex = 0; //global initial setting
tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged);
void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
tabControl1.SelectedIndex = currentIndex;
return;
}
private void nextButton_Click(object sender, EventArgs e)
{
currentIndex += 1;
if (currentIndex >= tabControl1.TabPages.Count)
{
currentIndex = 0;
}
foreach (TabPage pg in tabControl1.TabPages)
{
pg.Enabled = false;
}
tabControl1.TabPages[currentIndex].Enabled = true;
tabControl1.SelectedIndex = currentIndex;
}
另一種解決方案(最簡單的,我認爲):
使用事件選擇
private void tabWizardControl_Selecting(object sender, TabControlCancelEventArgs e)
{
int selectedTab = tabWizardControl.SelectedIndex;
//Disable the tab selection
if (currentSelectedTab != selectedTab)
{
//If selected tab is different than the current one, re-select the current tab.
//This disables the navigation using the tab selection.
tabWizardControl.SelectTab(currentSelectedTab);
}
}
如前所述,可以通過索引選擇標籤。
所以和以前一樣,我們關閉所有其他選項卡:
foreach(TabPage tab in tabControl.TabPages)
{
tab.Enabled = false;
}
(tabControl.TabPages[0] as TabPage).Enabled = true;
我們阻止導航到任何其他標籤的方式很簡單:
private void tabControl_Selecting(object sender, TabControlCancelEventArgs e)
{
if (!e.TabPage.Enabled)
{
e.Cancel = true;
}
}
唯一的缺點是,它們將出現可選,意味着它們不會變灰。如果您希望外觀不可用,則必須自己執行此操作。
您可以使用['TabControl.Selecting event'](http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selecting.aspx),設置'e。 「禁用」選項卡取消= true。但是,除非您自己繪製標籤,否則標籤實際上不會顯示爲禁用(灰顯)。我可能會完全刪除這些標籤,並在用戶進行測驗時添加它們。 – JosephHirn 2013-04-18 16:39:59