C# Winform:如何限制用户进入 TabControl 中的特定 TabPage

C# Winform: How to restrict a user to go into particular TabPage in TabControl

我正在创建一个示例库存 windows 表单应用程序,如果数量字典为空,则不应允许用户进入销售选项卡。

I am using metro design and material skin mix up to design my application I have posted a code sample below which works in case of simple winform control but not working in case of metro and material design.

代码示例

//check if selected tab is sales tab 
if (tcmain.SelectedTab == tpSales)
{
  //check if our cart is empty or not 
  if (Globals.qty.Count == 0)
  {
     //show error msg
     var diaEmptCart = MessageBox.Show("There Are 0 Products in Cart", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     //set selected tab as purchase 
     tcmain.SelectedTab = tpPurchase;
  }
  else
  {
     //or show the products available in cart for sales 
     //populate combo box with them 
     cmbPro.DataSource = new BindingSource(Globals.qty, null);
     //set key as display member 
     cmbPro.DisplayMember = "Key";
  }
}
//check if selectedd tab is tab purchase 
if (tcmain.SelectedTab == tpPurchase)
{
  if (Globals.qty.Count == 0)
  {
    //if yes, setting cart empty
    pbCart.Image = Image.FromFile(@"C:\Users\ThE PrOgRaMmEr\Documents\Visual Studio 2013\Projects\simpleInventory.cs.MUI\simpleInventory.cs\Resources\crt_empty.png");
  }
  else
  {
    //if not, setting cart full
    pbCart.Image = Image.FromFile(@"C:\Users\ThE PrOgRaMmEr\Documents\Visual Studio 2013\Projects\simpleInventory.cs.MUI\simpleInventory.cs\Resources\crt_full.png");
  }
}

您需要处理控件的选项卡选择事件。试试这个:

private void tcmain_Selecting(object sender, TabControlCancelEventArgs e)
{
      //Change whatever you want
      if (tcmain.TabPages[e.TabPageIndex] == tpSales && Globals.qty.Count == 0)
            e.Cancel = true;
}

但问题是您为什么还要显示该选项卡。我建议不要创建不需要的标签。