jaydeep more jaydeep more - 1 month ago 7
C# Question

How to Prevent a Person To Go into tab page in winfom

I am Creating a windows form application in which if the dictionary of quantity is empty then user shall not go into sales tab i am creating it using metro design and material skin mix up but using regular form control this works but using metro and material it doesn't work here is piece of code

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

Answer

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.

Comments