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

本文关键字:用户 TabPage Winform TabControl | 更新日期: 2023-09-27 18:15:03

我正在创建一个示例库存窗口表单应用程序,如果数量的字典是空的,那么用户不应该被允许进入销售选项卡。

我使用metro设计和材料皮肤混合来设计我的应用程序,我在下面发布了一个代码示例,在简单的winform控制的情况下工作,但在metro和材料设计的情况下不工作。

代码示例

//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");
  }
}

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

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

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;
}

但问题是为什么要显示这个选项卡。我建议不要创建不必要的选项卡。