本文介绍了如何使tablelayoutpanel从屏幕中心对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 有人能告诉我如何将tablelayoutpanel对齐从屏幕中心开始。我有一个tablelayoutpanel,其中我动态添加了几个按钮&在行和对齐中排列列。这些按钮是由用户添加的,就像用户可以给出的一样。在tablelayoutpanel中添加的按钮。所以基于no.of按钮添加tablelayoutpanel不断增长。我感兴趣的是这个tablelayoutpanel应该从表格中心开始。所有方向均等分。 或 我是在运行时在C#程序中创建TableLayoutPanel。我如何将其居中并根据我添加的行数/列数在表单上动态调整大小? 这里是一些代码: private void GenerateTable( int columnCount, int rowCount, string allignment) { int count = 0 ; int width = 0 ; int height = 0 ; int btncount = 1 ; // 清除现有控件,我们正在生成新的表格布局 tableLayoutPanel1.Controls.Clear(); // 清除现有的行和列样式 tableLayoutPanel1.ColumnStyles.Clear(); tableLayoutPanel1.RowStyles.Clear(); // 现在我们将生成表格,首先设置行数和列数 tableLayoutPanel1.ColumnCount = columnCount; tableLayoutPanel1.RowCount = rowCount; #region AllignMentofButtonIntableLayoutPanel for ( int x = 0 ; x < columnCount; x ++) { if (x == 0 ) { tableLayoutPanel1.ColumnStyles.Add( new ColumnStyle(SizeType.AutoSize)); } width + = 120 ; /// /首先添加一列 for ( int y = 0 ; y < rowCount; y ++) { // 接下来,添加一行。只有在创建第一列时才执行此操作 如果(x == 0 ) { tableLayoutPanel1.RowStyles.Add( new RowStyle(SizeType.AutoSize)); } pnelLayout [count] = new Panel(); btn [count] = new Button(); btn [count] .Height = 60 ; btn [count] .Width = 120 ; btn [count] .Text = data [count]; btn [count] .Name = count.ToString(); btn [count] .Height = 60 ; btn [count] .Width = 120 ; pnelLayout [count] .Width = 120 ; pnelLayout [count] .Height = 60 ; // 从配置窗口获取btnName并检查带有描述的btnName if (dtButtonName.Rows.Count > 0 ) { // 检查相同的btn文本名称,如果是真的更改颜色 ( int i = 0 ; i < dtButtonName.Rows.Count; i ++) { // 检查btnName和btnCount索引 如果( data [count] == dtButtonName.Rows [i] [ BtnName]。ToString()&& (btncount - 1 )。ToString()== dtButtonName.Rows [i] [ BtnIndex]。ToString()) btn [count] .BackColor = Color.WhiteSmoke; else if (btn [count] .BackColor == Color.WhiteSmoke) {} else btn [count] .BackColor = Color.LightGreen; } } else { btn [count] .BackColor = Color.LightGreen; } btn [count] .TextAlign = ContentAlignment.MiddleCenter; btn [count] .FlatStyle = FlatStyle.Popup; btn [count] .MouseDown + = new MouseEventHandler(Annaunciator_Window_MouseDown); btn [count] .ContextMenuStrip = contextMenuStrip1; pnelLayout [count] .Controls.Add(lblCorner [count]); pnelLayout [count] .Controls.Add(btn [count]); } } #endregion } [/ Edit] 解决方案 更改tablelayoutpanel属性dock style fill Hi all, Can anybody tell me how to align tablelayoutpanel to start from center of screen. I have a tablelayoutpanel in which i have added few buttons dynamically & are aligned in rows & columns. These buttons are added by user, like the user can give the no. of buttons to be added in the tablelayoutpanel. So based on the no.of buttons added tablelayoutpanel keeps growing. What i am interested in is that this tablelayoutpanel should start form center & equally divide in all the directions.orI am creating a TableLayoutPanel in a C# program at run-time. How can I center it and dynamically resize it on a form depending on how many rows/columns I add?[Edit]Here is some code:private void GenerateTable(int columnCount, int rowCount,string allignment){ int count=0; int width = 0; int height = 0; int btncount = 1; //Clear out the existing controls, we are generating a new table layout tableLayoutPanel1.Controls.Clear(); //Clear out the existing row and column styles tableLayoutPanel1.ColumnStyles.Clear(); tableLayoutPanel1.RowStyles.Clear(); //Now we will generate the table, setting up the row and column counts first tableLayoutPanel1.ColumnCount = columnCount; tableLayoutPanel1.RowCount = rowCount;#region AllignMentofButtonIntableLayoutPanel for (int x = 0; x < columnCount; x++) { if (x == 0) { tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize)); } width += 120; ////First add a column for (int y = 0; y < rowCount; y++) { //Next, add a row. Only do this when once, when creating the first column if (x == 0) { tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize)); } pnelLayout[count] = new Panel(); btn[count] = new Button(); btn[count].Height = 60; btn[count].Width = 120; btn[count].Text = data[count]; btn[count].Name = count.ToString(); btn[count].Height = 60; btn[count].Width = 120; pnelLayout[count].Width = 120; pnelLayout[count].Height = 60; //Get the btnName from Configuration windows and check the btnName with Description if (dtButtonName.Rows.Count > 0) { //Check for the same btn text Name,if it is true change the colour for (int i = 0; i < dtButtonName.Rows.Count; i++) { //Check for btnName and btnCount Index if (data[count] == dtButtonName.Rows[i]["BtnName"].ToString() && (btncount - 1).ToString() == dtButtonName.Rows[i]["BtnIndex"].ToString()) btn[count].BackColor = Color.WhiteSmoke; else if (btn[count].BackColor == Color.WhiteSmoke) { } else btn[count].BackColor = Color.LightGreen; } } else { btn[count].BackColor = Color.LightGreen; } btn[count].TextAlign = ContentAlignment.MiddleCenter; btn[count].FlatStyle = FlatStyle.Popup; btn[count].MouseDown += new MouseEventHandler(Annaunciator_Window_MouseDown); btn[count].ContextMenuStrip = contextMenuStrip1; pnelLayout[count].Controls.Add(lblCorner[count]); pnelLayout[count].Controls.Add(btn[count]); } }#endregion}[/Edit] 解决方案 这篇关于如何使tablelayoutpanel从屏幕中心对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 22:50