PocketPC users like these features, and I hope you will add them to your applications.
What you need to add Cut, Copy etc. functionality to your TextBox:
- Add this reference, there is the Clipboard class.
using System.Windows.Forms;
- Add these methods:
private void CopySelectedText(TextBox aTextBox)
{
DataObject m_data = new DataObject();
m_data.SetData(DataFormats.Text, true, aTextBox.SelectedText);
Clipboard.SetDataObject(m_data);
}
private void PasteSelectedText(TextBox aTextBox)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
int start = aTextBox.SelectionStart;
if (aTextBox.SelectionLength == 0)
{
aTextBox.Text = aTextBox.Text.Insert(aTextBox.SelectionStart,
Clipboard.GetDataObject().GetData(DataFormats.Text).
ToString());
}
else
{
aTextBox.Text = aTextBox.Text.Replace(aTextBox.SelectedText,
Clipboard.GetDataObject().GetData(DataFormats.Text).
ToString());
}
aTextBox.SelectionStart = (aTextBox.Text.Length >= start) ? start : aTextBox.Text.Length;
}
}
private void RemoveSelectedText(TextBox aTextBox)
{
if (aTextBox.SelectionLength > 0)
{
int start = aTextBox.SelectionStart;
//remove selected text
aTextBox.Text = aTextBox.Text.Replace(aTextBox.SelectedText, "");
aTextBox.SelectionStart = (aTextBox.Text.Length >= start) ? start : aTextBox.Text.Length;
}
}
- Then you are ready to handle the menu items:
private void mnuCopy_Click(object sender, EventArgs e)
{
CopySelectedText(this.tbNotes);
}
private void mnuPaste_Click(object sender, EventArgs e)
{
PasteSelectedText(this.tbNotes);
}
private void mnuCut_Click(object sender, EventArgs e)
{
CopySelectedText(this.tbNotes);
RemoveSelectedText(this.tbNotes);
}
I think everybody know how to add the menu items Cut, Copy, Paste etc. into a ContextMenu. :)
You can download the source code from:
http://www.winmobile.euweb.cz/#Post03
0 comments:
Post a Comment