简体   繁体   中英

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

I am Creating a sample Inventory windows form application in which if the dictionary of quantity is empty then user should not be allowed to go into sales tab.

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.

Code Sample

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

You need to handle tab selecting event of the control. Try this:

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

But the question is why you're even displaying the tab. I'd recommend not to create tabs which aren't required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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