繁体   English   中英

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

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

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

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

代码示例

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM