gpt4 book ai didi

c# - 当用户从上下文菜单中选择 "Copy"时如何将数据复制到剪贴板

转载 作者:行者123 更新时间:2023-11-30 12:46:33 24 4
gpt4 key购买 nike

当用户从特定单元格的上下文菜单中选择复制时,您如何将数据网格单元格的内容复制到剪贴板?

public Form1()
{
InitializeComponent();
dataGridView1.MouseClick += dataGridView1_MouseClick;
}

void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu m = new ContextMenu();
m.MenuItems.Add(new MenuItem("Copy"));

int currentMouseOuverRow = dataGridView1.HitTest(e.X, e.Y).RowIndex;

m.Show(dataGridView1, new Point(e.X, e.Y));
}
}

最佳答案

我想我会这样做:

var hitTestInfo = dataGridView1.HitTest(e.X, e.Y);
if (hitTestInfo.Type != DataGridViewHitTestType.Cell) { return; }

var mi = new MenuItem("Copy")
mi.Tag = hitTestInfo;
mi.Click += (s, e) =>
{
var hti = ((MenuItem)s).Tag as HitTestInfo;
var val = dataGridView1.Rows[hti.RowIndex].Cells[hti.ColumnIndex].Value;

Clipboard.SetData(DataFormats.Text, val);
}

m.MenuItems.Add(mi);

关于c# - 当用户从上下文菜单中选择 "Copy"时如何将数据复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19752523/

24 4 0