所以我有一个tab容器,我动态创建并在运行时添加标签,如下所示: -
private void DynamicTabCreation()
{
#region Code for dynamically creating a tab
//Getting all Product Types from database into a dataSet.
DataBaseConnection pt = new DataBaseConnection();
pt.passSqlCmdandFillDs = "SELECT * FROM [tblProductType]";
foreach (DataRow row in pt.dataSetWithDB.Tables[0].Rows)
{
//Adding new tabpages to the tabControl container, the name of each tab being the product type
//Key for later use //Name of tab
tabContainer.TabPages.Add((row["ProductType"].ToString()), ((string)row["Description"]));
}
//Make the tabs bigger so that they are touchable.
tabContainer.ItemSize = new Size(90, 50);
//Setting default settings for each tabpage created
foreach (TabPage tp in tabContainer.TabPages)
{
tp.BackColor = Color.White;
tp.AutoScroll = true; //Not doing anything :(
}
#endregion
}
执行此操作后,我将按钮添加到这些标签页: -
#region Code for adding buttons which represent products into each tab
foreach (TabPage tp in tabContainer.TabPages)
{
DataBaseConnection pt = new DataBaseConnection();
pt.passSqlCmdandFillDs = "SELECT * FROM [tblProduct]";
FlowLayoutPanel flp = new FlowLayoutPanel();
//Will make any controls added to the tab page be placed appropiately within it.
flp.Dock = DockStyle.Fill;
foreach (DataRow row in pt.dataSetWithDB.Tables[0].Rows)
{
if (tp.Text == row["ProductType"].ToString()) //If the Product Type corrosponds with the Tab Name in the tabContainer
{
//Button generated to add to the tab page (remmember buttons represent products)
Button b = new Button();
//Giving button appropiate size so it looks good and is suffient size to be touch screen compatible
b.Size = new Size(80, 80);
//Displaying the Product/Buttons details on the button.
b.Text = row["Description"].ToString() + Environment.NewLine + "£" + Convert.ToDecimal(row["Price"]);
//The properties of this product object store data about the 'Product' which the button represents.
ProductSelected product = new ProductSelected();
//Setting the properties of the product from the DB.
product.Description = row["Description"].ToString();
product.Price = Convert.ToDecimal(row["Price"]);
product.ProductID = (int)row["ProductID"];
//Changing the image of the button
b.BackgroundImage = tblProducts.readimageFromDB(row);
b.BackgroundImageLayout = ImageLayout.Stretch;
//changes
product.SecondaryID = 0;
//This 'Product' obj, contains all the information about the product.
//I am tagging it to the button
//So the button now represents the product object
b.Tag = product;
//Upon pressing the button, the information has to be added to the list.
b.Click += new EventHandler(updateProductList);
//The flowlayoutpanel makes sure that all the buttons are displayed in a organised layout.
//The flp can be thought of as a organised container
//This container can then be stored into another container (later on in the code, it will be stored into the tabPage(Which we are currently looping through))
flp.Controls.Add(b);
}
}
//Adding flp to the tabPage that we are current accesed at in the foreach loop.
tp.Controls.Add(flp);
}
#endregion
}
如果按钮太多,那么它们会从屏幕溢出,所以看起来像这样: -
那么在这种情况下如何添加垂直滚动条?
感谢。