【问题标题】:C# Winform: How to restrict a user to go into particular TabPage in TabControlC# Winform:如何限制用户进入 TabControl 中的特定 TabPage
【发布时间】:2016-10-26 09:51:06
【问题描述】:

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

我正在使用 Metro 设计和材质皮肤混合来设计我的应用程序我在下面发布了一个代码示例,该示例适用于简单的 Winform 控件,但不适用于 Metro 和 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");
  }
}

【问题讨论】:

    标签: c# winforms microsoft-metro


    【解决方案1】:

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

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

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

    【讨论】:

    • 那么我应该删除方法 private void tcmain_SelectedIndexChanged(Object sender, EventArgs e) 这样你的解决方案才有效
    • 我建议您在加载字典并且它为空时禁用标签页。所以用户没有得到消息框。无论您在何处加载数据,请检查销售额是否为空以及何时调用 tpSales.Enable = false;
    • @Jaydeep,如果您不想在更改的选定选项卡上执行任何特定任务,那么您可以删除此事件。如果有用也请标记为已回答
    • sebi 肯定会做kashi_rock谢谢你的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    相关资源
    最近更新 更多