C # TableLayoutPanel controls apply dynamic add controls and events __c#

Source: Internet
Author: User
<summary>
Adjust the layout of the screen
</summary>
<param name= "Videonum" ></param>
<returns></returns>
private bool Initializevideo (int videonum)
{
Count the ranks
int rowcol;
if (videonum <= 0 | | |!int. TryParse (Math.sqrt (videonum). ToString (), out Rowcol))
{
return false;
}
Calculate width Height
int widthheight = (int) (MAINPANEL.WIDTH/ROWCOL);

Reset Table
Mainpanel as TableLayoutPanel Control
MainPanel.Controls.Clear ();
Mainpanel.rowcount = Mainpanel.columncount = Rowcol;
Mainpanel.cellborderstyle = Tablelayoutpanelcellborderstyle.single;
Mainpanel.refresh ();
for (int i = 0; i < MainPanel.ColumnStyles.Count; i++)
{
Mainpanel.columnstyles[i]. Sizetype = Sizetype.absolute;
Mainpanel.columnstyles[i]. Width = Widthheight;
}
for (int i = 0; i < MainPanel.RowStyles.Count; i++)
{
Mainpanel.rowstyles[i]. Sizetype = Sizetype.absolute;
Mainpanel.rowstyles[i]. Height = Widthheight;
}

Adding controls
for (int i = 0; i < videonum; i++)
{
PictureBox Pvideo = new PictureBox ();
pvideo.padding = Pvideo.margin = new Padding (0);
Pvideo.name = "Pvideo" + i.tostring ();
Pvideo.width = Pvideo.height = Widthheight;
Pvideo.dock = DockStyle.Fill;
Pvideo.backgroundimage = resources.bg;
Pvideo.backgroundimagelayout = Imagelayout.stretch;
Pvideo.click + = new EventHandler (Pvideo_click);

MAINPANEL.CONTROLS.ADD (pvideo, I% rowcol, i/rowcol);
}


return true;
}

<summary>
Click image Event
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Pvideo_click (object sender, EventArgs e)
{
PictureBox Pvideo = (PictureBox) sender;
MessageBox.Show (Pvideo.name);
if (Mainpanel.getcolumnspan (pvideo) = = 1)
{
Hide other controls
foreach (Control Ctr in Mainpanel.controls)
{
if (Ctr. Name!= Pvideo.name)
Ctr. Visible = false;
}
pos = Mainpanel.getpositionfromcontrol (Pvideo);
Mainpanel.setcellposition (Pvideo, New tablelayoutpanelcellposition (0, 0));
Mainpanel.setrowspan (Pvideo, Mainpanel.rowcount);
Mainpanel.setcolumnspan (Pvideo, Mainpanel.columncount);
}
Else
{
Show All controls
foreach (Control Ctr in Mainpanel.controls)
{
Ctr. Visible = true;
}
Mainpanel.setcellposition (Pvideo, POS);
Mainpanel.setrowspan (Pvideo, 1);
Mainpanel.setcolumnspan (Pvideo, 1);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.