我有一个简单的winform应用程序,允许用户将控件拖到tablelayoutpanel。但经过一些测试并试图将控件拖到特定的索引行后,我发现它不起作用,甚至没有硬编码的索引号。
使用提供的代码示例,我尝试将文本框添加到行索引2,但是当我将内容从列表框拖到tablelayoutpanel时,它只是将文本框添加到' random'地点如下面的截图所示
据我所知,我希望现有的文本框可以向下移动并为添加的文本框添加内容:https://social.msdn.microsoft.com/Forums/windows/en-US/e4312cd8-6031-4a5c-92bf-e8adb1941fe5/insert-row-at-particular-position-in-table-layout-panel?forum=winforms。
我做错了吗?
设计师代码:
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(367, 12);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(190, 407);
this.listBox1.TabIndex = 0;
this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 1;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Location = new System.Drawing.Point(13, 12);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 1;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(332, 407);
this.tableLayoutPanel1.TabIndex = 1;
this.tableLayoutPanel1.DragDrop += new System.Windows.Forms.DragEventHandler(this.tableLayoutPanel1_DragDrop);
this.tableLayoutPanel1.DragEnter += new System.Windows.Forms.DragEventHandler(this.tableLayoutPanel1_DragEnter);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(569, 431);
this.Controls.Add(this.tableLayoutPanel1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
}
表格代码:
public partial class Form1 : Form
{
private int tempInt = 0;
public Form1()
{
InitializeComponent();
listBox1.Items.AddRange(new object[] { "test" });
tableLayoutPanel1.AllowDrop = true;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.RowCount = 3;
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
tempInt++;
DoDragDrop("test" + tempInt, DragDropEffects.Copy);
}
private void tableLayoutPanel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void tableLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
string text = e.Data.GetData(typeof(String)) as string;
TextBox tb = new TextBox();
tb.Dock = DockStyle.Fill;
tb.Text = text;
// I want to add the textbox to the second row
tableLayoutPanel1.Controls.Add(tb, 0, 2);
tableLayoutPanel1.SetRow(tb, 2);
}
}
编辑:
添加了基于DonBoitnott建议的代码
private void tableLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
string text = e.Data.GetData(typeof(String)) as string;
TextBox tb = new TextBox();
tb.Dock = DockStyle.Fill;
tb.Text = text;
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
int pos = tableLayoutPanel1.GetRow(tableLayoutPanel1.Controls[i]);
if (pos > 1)
{
tableLayoutPanel1.SetRow(tableLayoutPanel1.Controls[i], pos + 1);
}
}
tableLayoutPanel1.Controls.Add(tb, 0, 2);
}
答案 0 :(得分:0)
尝试修改此表单类,我在整个过程中做了一些更改:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.AddRange(new Object[] { "TextBox" });
tableLayoutPanel1.AllowDrop = true;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f));
}
private void listBox1_MouseDown(Object sender, MouseEventArgs e)
{
var count = tableLayoutPanel1.Controls.Count;
DoDragDrop($"test{count + 1}", DragDropEffects.Copy);
}
private void tableLayoutPanel1_DragEnter(Object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void tableLayoutPanel1_DragDrop(Object sender, DragEventArgs e)
{
var tb = new TextBox();
tb.Dock = DockStyle.Fill;
tb.Text = (e.Data.GetData(typeof(String)) as String);
var newRow = tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
var ctrl = tableLayoutPanel1.GetChildAtPoint(tableLayoutPanel1.PointToClient(new Point(e.X, e.Y)));
if (ctrl != null)
{
var pos = tableLayoutPanel1.GetRow(ctrl);
for (Int32 i = tableLayoutPanel1.RowStyles.Count - 2; i >= pos; i--)
{
var c = tableLayoutPanel1.GetControlFromPosition(0, i);
if (c != null)
tableLayoutPanel1.SetRow(c, i + 1);
}
tableLayoutPanel1.Controls.Add(tb, 0, pos);
}
else
tableLayoutPanel1.Controls.Add(tb, 0, newRow);
}
}
基本步骤是: