mobile开发中使用右键

右键使用标准的windows mobile 6的右键样式,有复原,剪下,复制,帖上,清除,全选等功能

首先做一个ContextMenu菜单的UI,然后添加以下代码

private void contextMenu1_Popup(object sender, EventArgs e)
        {
            TextBox tb = contextMenu1.SourceControl as TextBox;
            object objClipboard = Clipboard.GetDataObject().GetData(typeof(string));
            if (tb != null)
            {
                if (tb.SelectionLength == 0)
                {
                    miCut.Enabled = false;
                    miCopy.Enabled = false;
                    miClear.Enabled = false;
                }
                else
                {
                    miCut.Enabled = true;
                    miCopy.Enabled = true;
                    miClear.Enabled = true;
                }
                if (objClipboard == null)
                {
                    miPaste.Enabled = false;
                }
                else
                {
                    miPaste.Enabled = true;
                }
                if (tb.Text.Length == 0)
                {
                    miSelectAll.Enabled = false;
                }
                else
                {
                    miSelectAll.Enabled = true;
                }
            }
        }

        private void miRecover_Click(object sender, EventArgs e)
        {
            TextBox tb = contextMenu1.SourceControl as TextBox;
            if (tb != null && tb.CanUndo)
            {
                tb.Undo();
            }
        }

        private void miCut_Click(object sender, EventArgs e)
        {
            TextBox tb = contextMenu1.SourceControl as TextBox;
            if (tb != null)
            {
                Clipboard.SetDataObject(tb.SelectedText);
                tb.Text = tb.Text.Replace(tb.SelectedText, "");
            }
        }

        private void miCopy_Click(object sender, EventArgs e)
        {
            TextBox tb = contextMenu1.SourceControl as TextBox;
            if (tb != null)
            {
                Clipboard.SetDataObject(tb.SelectedText);
            }
        }

        private void miPaste_Click(object sender, EventArgs e)
        {
            string strClipboard = Clipboard.GetDataObject().GetData(typeof(string)).ToString();
            TextBox tb = contextMenu1.SourceControl as TextBox;
            string strSelected = tb.SelectedText;
            if (tb != null)
            {
                if (strSelected.Length == 0)
                {
                    tb.Text = tb.Text.Insert(tb.SelectionStart, strClipboard);
                }
                else
                {
                    tb.Text = tb.Text.Replace(strSelected, strClipboard);
                }
            }
        }

        private void miClear_Click(object sender, EventArgs e)
        {
            TextBox tb = contextMenu1.SourceControl as TextBox;
            if (tb != null)
            {
                tb.Text = tb.Text.Replace(tb.SelectedText, "");
            }
        }

        private void miSelectAll_Click(object sender, EventArgs e)
        {
            TextBox tb = contextMenu1.SourceControl as TextBox;
            if (tb != null)
            {
                tb.Focus();
                tb.SelectAll();
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值