ActiveControl - Cut Copy Paste - Improvement

K

Keith

I'm trying to create - what I thought was a simple Cut,
Copy, Paste, Undo Contextmenu - that I could use. The
menu is easy to create - but the functions - seem very
difficult to pull off in the CompactFramework.

Correct me in I'm wrong - but it would be a lot easier -
if I could call the activecontrol - but from what I've
read that is not available.

So I've come up with the following crude code. It works -
but the question is - how can I make it simpler. So I do
not have to hardcode all of the textboxes - one by one?
In other words, how can I count up all the textboxes on
the form - and then iterate through all of them - until it
is done with the last one? Or how could I call a control
(like a textbox) - but with a variable name. Like call
Textbox(var).text - so I could loop through them all?

Here's the crude code:

Private Sub ContextMenu1_Popup(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ContextMenu1.Popup
'Highlights all text as soon as the contextmenu is
pressed
If TextBox1.Focused = True Then
TextBox1.SelectAll()
ElseIf TextBox2.Focused = True Then
TextBox2.SelectAll()
End If
End Sub

Private Sub MenuItem4_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MenuItem4.Click
'Cuts the text - actually copies then just clears
out the textbox
If TextBox1.Focused = True Then
Clipboard.SetClipboardText(TextBox1.Text)
TextBox1.Text = ""
ElseIf TextBox2.Focused = True Then
Clipboard.SetClipboardText(TextBox2.Text)
TextBox2.Text = ""
End If
End Sub

Private Sub MenuItem5_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MenuItem5.Click
'Pastes the text in the Clipboard in the
respective textbox
If TextBox1.Focused = True Then
TextBox1.Text = Clipboard.GetClipboardText()
ElseIf TextBox2.Focused = True Then
TextBox2.Text = Clipboard.GetClipboardText()
End If
End Sub

Private Sub MenuItem7_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MenuItem7.Click
'Undo a cut
If TextBox1.Focused = True And TextBox1.Text = ""
Then
TextBox1.Text = Clipboard.GetClipboardText()
ElseIf TextBox2.Focused = True And TextBox2.Text
= "" Then
TextBox2.Text = Clipboard.GetClipboardText()
End If
End Sub

Private Sub MenuItem6_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MenuItem6.Click
'Copies the text but does not blank out the
textbox like the cut command
If TextBox1.Focused = True Then
Clipboard.SetClipboardText(TextBox1.Text)
ElseIf TextBox2.Focused = True Then
Clipboard.SetClipboardText(TextBox2.Text)
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top