• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#TableLayoutPanel控件应用动态添加控件及事件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
/// <summary>
        /// 调整画面布局
        /// </summary>
        /// <param name="videoNum"></param>
        /// <returns></returns>
        private bool InitializeVideo(int videoNum)
        {
            //计算行列
            int rowcol;
            if (videoNum <= 0 || !int.TryParse(Math.Sqrt(videoNum).ToString(), out rowcol))
            {
                return false;
            }
            //计算宽高
            int WidthHeight = (int)(MainPanel.Width / rowcol);
            
            //重新设置表格
            //MainPanel为TableLayoutPanel控件
            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;
            }

            //添加控件
            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>
        /// 点击图像事件
        /// </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)
            {
                //隐藏其它控件
                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
            {
                //显示所有控件
                foreach (Control ctr in MainPanel.Controls)
                {
                    ctr.Visible = true;
                }
                MainPanel.SetCellPosition(pVideo, pos);
                MainPanel.SetRowSpan(pVideo, 1);
                MainPanel.SetColumnSpan(pVideo, 1);
            }
        }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#连接mysql实用工具类发布时间:2022-07-13
下一篇:
c#操作oracle的通用类发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap