簡體   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