惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
D
DataBreaches.Net
F
Full Disclosure
腾讯CDC
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
O
OpenAI News
Project Zero
Project Zero
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Schneier on Security
Schneier on Security

博客园 - 缤纷夏日

文件分割合并DOS版 安装vs2017后,RDLC 报表定义具有无法升级的无效目标命名空间 frp+TeamViewer 完美解决TeamViewer5分钟商业提醒 TortoiseGit版本库中某个文件显示问号或叹号的问题解决办法 vs2012调试时,抛出异常的等待时间很慢,原来是QQ电脑管家搞的鬼。 wordpress4.4+版本自动生成一个768w像素缩略图的解决办法 vs2012 与 win7 不兼容的问题 自制《要塞:十字军东征》无限金钱修改器 ms Sql 数据库出现 “提供的统计信息流已损坏”的解决办法。 C#+Midi 模拟各种乐器演奏 如何让DataGridView根据数据“0”或“1”等值显示为“是”或“否”(复选框的使用) DataGridView 显示和隐藏DataGridViewButtonCell按钮的办法 文件隐藏助手,将一个压缩文件隐藏到图片中 如何让DataGridview控件自动滚动到指定的行或列 为Winform程序中DataGridView控件增加自动显示行号功能 在Excel Vba程序中自制进度条,显示实时进度信息 Winform中的DataGridView控件内容自动保存 地下城守护者2 无限魔法修改器 缤纷影视系统3.0源码开放
c#+Winform实现自定义的“复制、粘贴”右键快捷菜单,多个控件共享使用一个右键菜单。
缤纷夏日 · 2012-02-16 · via 博客园 - 缤纷夏日

程序中需要用到文本框或者RichTextBox进行文字输入或显示,对于一般使用者,可能连快捷键Ctrl+C复制,Ctrl+V粘贴都不会用,作为开发者就必须做一个右键菜单,以显示“复制”,“粘贴”。

可以将一个上下文菜单(ContextMenuStrip,也叫右键菜单、快捷菜单)分配给几个控件使用,方法是将这几个控件的ContextMenuStrip属性设置为需要显示的菜单。

在菜单事件中,如何判断是在哪个控件点击的呢?答案是此上下文菜单的SourceControl属性,以下是复制、粘贴的代码:

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
string selectText = ((RichTextBox)menuSend.SourceControl).SelectedText;
if (selectText != "")
{
Clipboard.SetText(selectText);
}
}

private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Clipboard.ContainsText())
{
RichTextBox txtBox = (RichTextBox)menuSend.SourceControl;
int index = txtBox.SelectionStart; //记录下粘贴前的光标位置
string text = txtBox.Text;
//删除选中的文本
text = text.Remove(txtBox.SelectionStart, txtBox.SelectionLength);
//在当前光标输入点插入剪切板内容
text = text.Insert(txtBox.SelectionStart, Clipboard.GetText());
txtBox.Text = text;
//重设光标位置
txtBox.SelectionStart = index;
}
}

 为了使右键菜单更显智能,没有选中文本时“复制”先效;剪切板无内容时,“粘贴”无效,特在其Opened事件中增加以下代码。

 private void menuSend_Opened(object sender, EventArgs e)
{
//没有选择文本时,复制菜单禁用
string selectText = ((RichTextBox)menuSend.SourceControl).SelectedText;
if (selectText != "")
复制ToolStripMenuItem.Enabled = true;
else
复制ToolStripMenuItem.Enabled = false;
//剪切板没有文本内容时,粘贴菜单禁用
if (Clipboard.ContainsText())
{
粘贴ToolStripMenuItem.Enabled = true;
}
else
粘贴ToolStripMenuItem.Enabled = false;

}