- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有一个 DataGridView
,它由 Cells 中的许多不同的 strings
(不同的长度、数字和纯文本)填充。
我想做的就是复制粘贴这些字符串,可以是任意选择的Cells。
我复制的方法是:
if (e.Control && e.KeyCode == Keys.C)
{
// copy
DataGridViewSelectedCellCollection tmpCells = this.MyDataGridView.SelectedCells;
Clipboard.SetDataObject(tmpCells);
}
哪个工作正常。
我粘贴的方法是:
if (e.Control && e.KeyCode == Keys.V)
{
// paste
IDataObject dataInClipboard = Clipboard.GetDataObject();
string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);
char[] rowSplitter = { '\r', '\n' };
char[] columnSplitter = { '\t' };
string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);
int r1 = this.MyDataGridView.SelectedCells[0].RowIndex;
int c1 = this.MyDataGridView.SelectedCells[0].ColumnIndex;
int r2 = this.MyDataGridView.SelectedCells[this.MyDataGridView.SelectedCells.Count-1].RowIndex;
int c2 = this.MyDataGridView.SelectedCells[this.MyDataGridView.SelectedCells.Count-1].ColumnIndex;
int r = Math.Min(r1, r2); // Do not care if selection was taken by drag mouse up or down, always start from min
int c = Math.Min(c1, c2); // Do not care if selection was taken by drag mouse left or right, always start from min
for (int iRow = 0; iRow < rowsInClipboard.Length; ++iRow )
{
string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);
for (int iCol = 0; iCol < valuesInRow.Length; ++iCol )
{
if (this.MyDataGridView.ColumnCount-1 >= c + iCol)
{
DataGridViewCell DGVC = (this.MyDataGridView.Rows[r + iRow].Cells[c + iCol]);
DGVC.Value = valuesInRow[iCol];
}
}
}
}
}
效果很好除非字符串本身不包含我用rowSplitter
和columnSplitter
指定的任何分隔符。但不幸的是,这种情况经常发生。然后它将字符串分开并将其扩展到下一个单元格。
示例:
Cell[n] = {"This string contains a new line delimiter \n but should use only one cell."}
将被粘贴到:
Cell[n] = {"This string contains a new line delimiter"};
Cell[n+1] = {"but should use only one cell."}
所以我的问题是:是否可以恢复之前复制到剪贴板的 DataGridViewSelectedCellCollection
?仅从 object
转换为 DataGridViewSelectedCellCollection
是行不通的:
DataGridViewSelectedCellCollection DGSCC = (DataGridViewSelectedCellCollection)dataInClipboard; // compiles, but throws exception at runtime
除了按定义的格式解析每个字符串之外,我还有其他选择吗?
最佳答案
您必须为剪贴板定义自己的格式,这将执行默认格式无法为您执行的操作。
在这种特定情况下,最简单的解决方案是将多行分隔符转换为 \n
,然后在您粘贴 时转换回来,但无论如何它不再意味着
DataGridViewSelectedCellCollection tmpCells = this.MyDataGridView.SelectedCells;
Clipboard.SetDataObject(tmpCells);
但是
DataGridViewSelectedCellCollection tmpCells = this.MyDataGridView.SelectedCells;
string result = "";
foreach(DataGridViewCell cell in tempCells)
// ... try to replicate what default clipboard text representation does
// change line breaks
Clipboard.SetDataObject(result.Replace("\xd\xa", "\\n"));
和粘贴将是:
IDataObject dataInClipboard = Clipboard.GetDataObject();
string stringInClipboard = dataInClipboard.GetData(DataFormats.Text).ToString().Replace("\\n", "\xd\xa");
关于c# - 将 DataGridViewSelectedCellCollection 复制+粘贴到剪贴板或从剪贴板粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20700950/
我正在遵循一个教程,老师通过以下方式将 html 粘贴到我们的 scrappy shell 中:%paste (下面的 html) html_doc = " " Title of hte page
例如 1.1.1.1 a.com 2.1.1.1 b.com 1.3.1.1 c.com 1.1.5.1 d.com 1.2.1.1 e.com 现在我想从另一个文本中替换这个 ip,不一样
是否有机会在 Angular 中实现粘贴按钮。 FE:用户复制网站的链接,当他或她点击按钮时,在我的页面上复制的链接应该出现在文本框中。 谢谢! 最佳答案 您只能以编程方式从网页复制。您不能以编程方式
我正在尝试提高 Vim 中粘贴功能的可用性,因为太多不同的删除操作(实际上我认为它们都是这样)也会拉到粘贴缓冲区。 这意味着我不再能够删除一些我想粘贴到某处的文本,清理一些东西,以及。然后 做我的粘贴
我正在构建一个简单的 Electron 应用程序,以在屏幕上的其他所有内容上显示一些文本。 有一个键盘快捷键可以打开带有文本的弹出窗口。 我想添加一个小功能。 最好的情况是:在计算机上的任意位置选择一
我有一个双击事件,我希望它保存特定范围的副本。 Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boo
我已经为这个烦人的问题苦苦挣扎了一段时间,但没有找到一个优雅的解决方案。 假设我有这样一个类层次结构: class StatWithBounds[A](val min: A, val max: A,
现在我有一个 Word 宏,可以通过将图像复制并粘贴到该位置来将图像移动到特定文本前面。这种方法效果很好,但成本很高。如果我的 Word 文档中有 1,000 张图像,则运行宏可能需要 30 分钟。
让我以我是自学成才的事实作为我的问题的开头,所以请提供尽可能详细的信息,如果我需要您以不同的方式或多次解释,请耐心等待。 我使用 Microsoft Visual Basic 7.0 为我的团队创建了
我已经为这个烦人的问题苦苦挣扎了一段时间,但没有找到一个优雅的解决方案。 假设我有这样一个类层次结构: class StatWithBounds[A](val min: A, val max: A,
我正在使用 Meteor 开发一个聊天应用程序,我不希望用户能够出于明显的垃圾邮件原因将内容复制/粘贴到表单中。这可能吗?这是我用来运行聊天应用程序的代码: Javascript: // render
我已经为此搜索了很多,但找不到任何建议...我提供了我自己的经典操作栏实现,所以我在所有 Activity 中声明粘贴/等..有谁知道如何做到这一点? 此外,我将提供我自己的复制/粘贴功能,并且仅在需
Windows 中 SWT Text 的默认上下文菜单有几个我们不想要的选项。由于操作系统提供的默认上下文菜单无法修改,因此我创建了一个自定义上下文菜单,其中只有基本的文本操作,例如文本框的删除、剪切
我最后的问题不是很清楚,我再试一次。 在我的 Tumblr 博客 (http://anti-standard.tumblr.com) 上,您可以看到一张图片(图片上写着“ANTI STANDARD”)
我必须编写一个脚本文件来剪切以下列并将其粘贴到新 .arff 文件中同一行的末尾。我想文件类型无关紧要。 当前文件: 63,male,typ_angina,145,233,t,left_vent_hy
是否可以发送过去的命令,以便将文本粘贴到当前聚焦的编辑文本中。场景: 后台服务监听通知(完成) 收到通知后,需要将文本复制到剪贴板(完成) 将文本粘贴到任何当前聚焦的字段,如果不可能则放弃粘贴命令。
我想用 PIL 粘贴一堆图片。出于某种原因,当我运行 blank.paste(img,(i*128,j*128)) 行时,出现以下错误:ValueError: cannot determine reg
如何在我的网页中禁用复制粘贴功能。准确地说,我不希望我的用户从我的网站上复制任何信息并将其用于个人目的。上一个关于同一主题的问题没有给出足够的解释。 onselect 和 ondrag 不起作用。请帮
废话不多说,直接上代码,小伙伴们仔细看下注释吧。 复制代码代码如下: /*简单的 复制 剪切 粘贴 功能 操作:
我应该在 vimrc 中添加哪一行以便在终端之间或不同文件/选项卡之间轻松复制/粘贴? 我现在有: " Better copy & paste set pastetoggle= set clipboa
我是一名优秀的程序员,十分优秀!