You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MessageBox干扰剪贴板及注释后ATOM缺失问题求助

解决方案

1. 修复MessageBox干扰剪贴板的问题

MessageBox.Show会抢占UI线程焦点,甚至可能导致剪贴板内容被系统或其他进程意外覆盖,推荐两种解决方式:

方式一:改用非阻塞提示替代弹窗

用状态栏提示代替MessageBox,既不干扰剪贴板,也不阻塞用户操作:

// 设置剪贴板后添加以下代码
toolStripStatusLabel1.Text = "已成功复制到剪贴板";
// 3秒后自动清空提示
var tipTimer = new Timer { Interval = 3000 };
tipTimer.Tick += (s, args) => {
    toolStripStatusLabel1.Text = string.Empty;
    tipTimer.Stop();
};
tipTimer.Start();

方式二:缓存数据后再显示弹窗

如果必须保留弹窗,先将dataText缓存到临时变量,确保弹窗显示的是复制前的内容:

string cachedData = dataText;
Clipboard.SetText(cachedData, TextDataFormat.Text);
MessageBox.Show(cachedData, "Copied data", MessageBoxButtons.OK, MessageBoxIcon.Information);

2. 修复注释MessageBox后ATOM字样消失的问题

核心原因是DataGridView默认Ctrl+C行为与自定义复制逻辑冲突,或者PdbAtom的ToString()方法未正确输出ATOM前缀:

步骤一:禁用DataGridView默认复制行为

在dataGridView1_KeyDown方法开头添加代码,阻止控件处理默认的Ctrl+C操作:

e.SuppressKeyPress = true; // 禁用控件默认的Ctrl+C行为

步骤二:手动拼接ATOM格式文本

不要依赖第三方库的ToString()方法,手动拼接符合PDB标准的ATOM行:

// 替换原有的dataText += pdbAtomList_[row.Index] + "\n";
var atom = pdbAtomList_[row.Index];
// 按照PDB格式规范拼接ATOM行
dataText += $"ATOM  {atom.AtomNo,5} {atom.AtomName,-4} {atom.ResidueName,-3} {atom.ChainName} {atom.ResidueNo,4}{atom.InsertionCode,-1}   {atom.Coordinate.x,8:F3}{atom.Coordinate.y,8:F3}{atom.Coordinate.z,8:F3}{atom.Occupancy,6:F2}{atom.TemperatureFactor,6:F2}          {atom.SegmentIdentifier,-4}{atom.SegmentIdentifierSymbol,-2}{atom.ChargeOfTheAtom,-2}\n";

3. 剪贴板操作健壮性优化

Windows剪贴板操作可能因权限、占用等问题失败,建议添加异常处理:

try
{
    Clipboard.SetText(dataText, TextDataFormat.Text);
    toolStripStatusLabel1.Text = "已成功复制到剪贴板";
}
catch (Exception ex)
{
    toolStripStatusLabel1.Text = $"复制失败:{ex.Message}";
}

内容的提问来源于stack exchange,提问作者user366312

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 12:40:24