Winform开发中TableLayoutPanel应用实例源码,带右键菜单
using System;
using System.Drawing;
using System.Windows.Forms;
namespace HdhCmsTableTest
{
public partial class EditTableFrom : Form
{
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();
ContextMenuStrip contextMenu = new ContextMenuStrip();
TableLayoutPanel tablePanel = new TableLayoutPanel();
private string hdhcmsTag;
public EditTableFrom()
{
InitializeComponent();
CreateRightMenu();
InitTableLayout();
InitTableSecond();
}
private void InitTableLayout()
{
tableLayoutPanel.Dock = DockStyle.Fill;
tableLayoutPanel.AutoScroll = true;
tableLayoutPanel.ColumnStyles.Clear();
tableLayoutPanel.Width = 800;
tableLayoutPanel.Width = 600;
tableLayoutPanel.BackColor = Color.AliceBlue;
tableLayoutPanel.Size = new Size(230,30);
int col = 0;
int row = 0;
for (int i = 0; i < 100; i++)
{
//for (int col = 0; col < 3; col++)
{
#region 添加图片控件
//PictureBox pic = new PictureBox();
//pic.Width = 200;
//pic.Height = 200;
//pic.Dock = DockStyle.Fill;
//pic.SizeMode = PictureBoxSizeMode.Zoom; // 图片按比例缩放
//pic.BackColor = Color.Blue;
////pic.Image = Image.FromFile($"image_{row}_{col}.jpg"); // 替换为实际路径
////pic.ContextMenuStrip = picContextMenu; // 绑定右键菜单
//// 记录图片位置信息(可选)
//pic.Tag = new Point(row, col);
// 添加到TableLayoutPanel
//tableLayoutPanel.Controls.Add(pic, col, row);
#endregion
#region 添加textbox控件
TextBox textBox = new TextBox();
textBox.Size = new Size(230,30);
textBox.Text = i.ToString();
textBox.Tag=i+"_"+col+"_"+row;
textBox.ContextMenuStrip = contextMenu;
textBox.MouseDown += new MouseEventHandler(MouseDown_Click);
tableLayoutPanel.Controls.Add(textBox, col, row);
#endregion
col++;
if (col >= 3) { col = 0; row++; }
}
}
panel1.Controls.Add(tableLayoutPanel);
}
private void CreateRightMenu()
{
ToolStripMenuItem toolMenu = new ToolStripMenuItem();
toolMenu.Text = "编辑数据";
toolMenu.Click += new EventHandler(Edit_Click);
contextMenu.Items.Add(toolMenu);
toolMenu = new ToolStripMenuItem();
toolMenu.Text = "删除数据";
toolMenu.Click += new EventHandler(Dele_Click);
contextMenu.Items.Add(toolMenu);
}
private void MouseDown_Click(object sender, MouseEventArgs e)
{
TextBox txt = (TextBox)sender;
hdhcmsTag = txt.Tag.ToString();
}
private void Edit_Click(object sender, EventArgs e)
{
MessageBox.Show(hdhcmsTag);
}
private void Dele_Click(object sender, EventArgs e)
{
MessageBox.Show(hdhcmsTag);
}
private void InitTableSecond()
{
}
}
}
查看更多关于Winform开发中TableLayoutPanel应用实例源码,带右键菜单的详细内容...