tableLayoutPanel运行时添加行动态 [英] tableLayoutPanel runtime add row dynamice
问题描述
我想使用tableLayoutPanel来发表背诵。这应该是通过运行时和动态的,这样用户就可以根据需要在背诵中添加一行。
,请帮助
我需要帮助
谢谢
I want to use a tableLayoutPanel to issue a recite. this should be through runtime and dynamic so that the user can add a row to the recite as many as they want.
please , help
I need to help
thanks
推荐答案
我建议OP在这里忘记使用TableLayoutPanel,而只是创建TextBoxes或RichTextBoxes的网格,在一个小组中。
总结:我越是使用TableLayoutPanel并发现它的怪癖,我越不喜欢它并想要使用它。 TableLayoutPanel的自动流行为似乎无法控制,您可以可靠地添加带控件的新行,而不是以奇怪的方式更改行/列顺序。
今天的最后一根稻草发现如果Control不可见,TableLayoutPanel基于其Column,Row位置(GetControlFromPosition)查找Control的方法将不起作用;所以,为了使控件按需显示,你必须解析TableLayoutPanel的整个ControlCollection。
但是,按照承诺,这里有效将创建一个包含51行和8列的TableLayoutPanel的代码。第一行将有标签显示行号;第一列将有标签显示列号和单元格1,1~7,50将包含TextBoxes。
我不会支持或回答问题关于此代码;我可以,何时以及如果我有时间,延长或修改它,或许,如果我达到可用和可靠的东西,在CP上发布提示或文章,展示我所学到的东西。
最初,只显示一行TextBoxes:当用户在任意行的最后一(第八)列的TextBox中按下Return / Enter键时,下一行将是可见。
由于TableLayoutPanel的自动流行为的副作用,我不建议任何人承担尝试实际删除行的任务。
I recommend the OP here forget about using a TableLayoutPanel and instead just create a "grid" of TextBoxes, or RichTextBoxes, in a Panel.
Summary: the more I work with the TableLayoutPanel and discover its quirks the less I like it and want to use it. The auto-flow behavior of the TableLayoutPanel seems to be not controllable in a way that you can reliably add new Rows with Controls, and not have the Row/Column order altered in strange ways.
The "last straw" today was discovering that the TableLayoutPanel's method to find a Control based on its Column, Row position (GetControlFromPosition) will not work if the Control is not 'visible; so, to make Controls visible on demand, you have to parse the entire ControlCollection of the TableLayoutPanel.
But, as promised, here's working code that will create a TableLayoutPanel with 51 rows, and 8 columns. The first Row will have Labels showing the Row Number; the first Column will have Labels showing the Column Number and Cells 1,1~7,50 will contain TextBoxes.
I will not support, or answer questions about this code; I may, when and if I have time, extend it, or modify it, and, perhaps, if I reach something usable, and reliable, post a Tip, or article, on CP showing what I've learned.
Initially, only one Row of TextBoxes is displayed: when the user presses the Return/Enter key in the TextBox in the last (eighth) column of ANY Row, the next Row will be made visible.
Because of the side-effects of the auto-flow behavior of the TableLayoutPanel, I do not recommend anyone take on the task of trying to actually delete Rows.
WinForms
Controls
Single Main-Form named 'MainForm
TableLayoutPanel named 'TestTableLayoutPanel
Button named 'btnCreateTableLayoutPanel
ContextMenuStrip named 'ctxMnuStripDeleteRow
0。设计时设置:
TestTableLayoutPanel:
'RowCount 1
'ColumnCount 1
'GrowStyle AddRows
'CellBorderStyle TableLayoutPanelCellBorderStyle.None
ctxMnuStripDeleteRow:添加一个菜单项:'Delete Row',并创建一个'点击它的EventHandler。注意:删除未在此处实现的行的代码。
1.表格范围常量:
0. Design-time settings:
TestTableLayoutPanel:
'RowCount 1
'ColumnCount 1
'GrowStyle AddRows
'CellBorderStyle TableLayoutPanelCellBorderStyle.None
ctxMnuStripDeleteRow: add one Menu Item: 'Delete Row', and create a 'Clicked EventHandler for it. Note: code to delete a row not implemented here.
1. Form-scoped constants:
private const int NRows = 50;
private const int NCols = 7;
private const int RwHeight = 40;
private const int ClWidth = 100;
2。表格范围的变量:
2. Form-scoped variables:
private int currentRow;
private int currentCol;
private int count = 1;
private TextBox currentTextBox;
private List<textbox> selectedLabels = new List<textbox>();</textbox></textbox>
3。表单加载代码:
private void Form1_Load(object sender, EventArgs e)
{
TestTableLayoutPanel.RowCount = NRows + 1;
TestTableLayoutPanel.ColumnCount = NCols + 1;
TestTableLayoutPanel.AutoScroll = true;
}
4。初始化TableLayoutPanel:
4. Initializing the TableLayoutPanel:
private void btnCreateLayoutPanel_Click(object sender, EventArgs e)
{
TestTableLayoutPanel.SuspendLayout();
TextBox newTextBox;
Label newLabel;
TestTableLayoutPanel.RowStyles[0].Height = RwHeight;
TestTableLayoutPanel.ColumnStyles[0].Width = ClWidth;
for (int row = 0; row <= NRows; row++)
{
TestTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, RwHeight));
for (int col = 0; col < NCols; col++)
{
TestTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, ClWidth));
// add the column header Label
if (row == 0)
{
newLabel = new Label
{
Margin = new Padding(0),
AutoSize = false,
Dock = DockStyle.Fill,
BackColor = Color.AliceBlue,
Text = string.Format("column {0}", (col + 1).ToString()),
TextAlign = ContentAlignment.MiddleCenter,
TabStop = false
};
TestTableLayoutPanel.Controls.Add(newLabel, col + 1, 0);
// not implemented here
//newLabel.Click += ColumnHeader_Click;
continue;
}
// add the row header Label
if (col == 0)
{
newLabel = new Label()
{
Margin = new Padding(0),
AutoSize = false,
Dock = DockStyle.Fill,
BackColor = Color.AliceBlue,
Text = string.Format("row {0}", row.ToString()),
TextAlign = ContentAlignment.MiddleCenter,
TabStop = false
};
newLabel.ContextMenuStrip = ctxMnuStripDeleteRow;
TestTableLayoutPanel.Controls.Add(newLabel, 0, row);
// not implemented here
newLabel.Click += RowHeader_Click;
}
// add the TextBoxes to the interior Cells
newTextBox = new TextBox()
{
AutoSize = false,
Dock = DockStyle.Fill,
Margin = new Padding(0),
Padding = new Padding(0),
TextAlign = HorizontalAlignment.Center
};
newTextBox.Click += CellLabel_Click;
if (col + 1 == NCols)
{
newTextBox.AcceptsReturn = true;
newTextBox.KeyUp += NewLabelOnKeyUp;
}
TestTableLayoutPanel.Controls.Add(newTextBox, col + 1, row);
if (row > 1) newTextBox.Hide();
}
}
TestTableLayoutPanel.ResumeLayout();
TestTableLayoutPanel.Refresh();
}
5。当用户在7日按下Return时,使新行可见。 TextBox:
5. Making a new Row visible when the user presses Return in the 7th. TextBox:
private void NewLabelOnKeyUp(object sender, KeyEventArgs keyEventArgs)
{
if (keyEventArgs.KeyCode != Keys.Enter) return;
var position = TestTableLayoutPanel.GetPositionFromControl(currentTextBox);
if (position.Column == NCols)
{
currentTextBox.AcceptsReturn = false;
int row = position.Row + 1;
TestTableLayoutPanel.RowStyles[row].Height = RwHeight;
for (int i = 1; i < NCols + 1; i++)
{
<big>// See comment !</big>
foreach (Control cntrl in TestTableLayoutPanel.Controls)
{
if (TestTableLayoutPanel.GetCellPosition(cntrl).Column == i)
{
if (TestTableLayoutPanel.GetCellPosition(cntrl).Row == row)
{
cntrl.Visible = true;
break;
}
}
}
}
}
}
private void CellLabel_Click(object sender, EventArgs e)
{
currentTextBox = sender as TextBox;
if (currentTextBox == null) return;
// for future use ... code to track selected Cells ...
}
注释:TableLayoutPanel的实现中存在一个错误:当一个单元格中的控件被隐藏时:使用GetControlFromPosition无法找到它!所以你在这里看到找到隐藏控件所需的黑客。
要继续吗?
Comment: there is a bug in the implementation of TableLayoutPanel: when a Control in a Cell is hidden: it cannot be "found" using GetControlFromPosition ! So you see here the hack necessary to find the hidden Controls.
To be continued ?
我找到一个新答案
我使用datagridview事件单元格输入
获取数据,插入单元格动态,添加运行时单元格
I find a new answer
I use datagridview event cell enter
fetch data ,insert cell dynamic , add runtime cell
这篇关于tableLayoutPanel运行时添加行动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!