Clipboard

G

Graf

Hello Everyone,

the .net cf 2.0 supports the clipboard directly, but is there any easy
way to find out, which textbox has the focus? Here is the code, that I
have at the moment and as you can see I have to check for every textbox
if it has the focus. Any ideas how to make this work for every textbox
easier?

Thanks in advance!

Regards,
Andreas

Private Sub miPaste_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles miPaste.Click, miPaste2.Click
Dim strTextToInsert As String
Dim strTextInForm As String
Dim strNewInForm As String
Dim intPosition As Int16
Dim activeTextbox As TextBox
Dim bolNoAction As Boolean = False

'Welche Textbox hat den Fokus? (keine Activecontrol Eigenschaft
in CF.net?)
If txtDecryptText.Focused Then
activeTextbox = DirectCast(txtDecryptText, TextBox)
ElseIf txtExpression.Focused Then
activeTextbox = DirectCast(txtExpression, TextBox)
ElseIf txtExpression2.Focused Then
activeTextbox = DirectCast(txtExpression2, TextBox)
ElseIf txtResult.Focused Then
activeTextbox = DirectCast(txtResult, TextBox)
ElseIf txtResult2.Focused Then
activeTextbox = DirectCast(txtResult2, TextBox)
ElseIf txtInput.Focused Then
activeTextbox = DirectCast(txtInput, TextBox)
Else
bolNoAction = True

End If

If bolNoAction = False Then
'Ist beim Einfügen Text markiert, diesen vorher löschen
If activeTextbox.SelectedText <> "" Then
activeTextbox.SelectedText = ""
End If
'Wo steht der Cursor? Dort den Text einfügen
intPosition = CShort(activeTextbox.SelectionStart)
strTextInForm = activeTextbox.Text
If
Windows.Forms.Clipboard.GetDataObject.GetData(DataFormats.Text) IsNot
Nothing Then
strTextToInsert =
Windows.Forms.Clipboard.GetDataObject.GetData(DataFormats.Text).ToString


strNewInForm = strTextInForm.Insert(intPosition,
strTextToInsert)

activeTextbox.Text = strNewInForm
End If
End If
End Sub
 
D

Daniel Moth

You can either keep track of the control with focus (catching the gotfocus
event and storing the control), iterate through all controls checking the
focused property or explicitly checking the focused property of each control
of interest (like you are doing):
http://groups.google.com/group/micr...et.framework.compactframework&q=activecontrol

Cheers
Daniel
--
http://www.danielmoth.com/Blog/

Hello Everyone,

the .net cf 2.0 supports the clipboard directly, but is there any easy
way to find out, which textbox has the focus? Here is the code, that I
have at the moment and as you can see I have to check for every textbox
if it has the focus. Any ideas how to make this work for every textbox
easier?

Thanks in advance!

Regards,
Andreas

Private Sub miPaste_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles miPaste.Click, miPaste2.Click
Dim strTextToInsert As String
Dim strTextInForm As String
Dim strNewInForm As String
Dim intPosition As Int16
Dim activeTextbox As TextBox
Dim bolNoAction As Boolean = False

'Welche Textbox hat den Fokus? (keine Activecontrol Eigenschaft
in CF.net?)
If txtDecryptText.Focused Then
activeTextbox = DirectCast(txtDecryptText, TextBox)
ElseIf txtExpression.Focused Then
activeTextbox = DirectCast(txtExpression, TextBox)
ElseIf txtExpression2.Focused Then
activeTextbox = DirectCast(txtExpression2, TextBox)
ElseIf txtResult.Focused Then
activeTextbox = DirectCast(txtResult, TextBox)
ElseIf txtResult2.Focused Then
activeTextbox = DirectCast(txtResult2, TextBox)
ElseIf txtInput.Focused Then
activeTextbox = DirectCast(txtInput, TextBox)
Else
bolNoAction = True

End If

If bolNoAction = False Then
'Ist beim Einfügen Text markiert, diesen vorher löschen
If activeTextbox.SelectedText <> "" Then
activeTextbox.SelectedText = ""
End If
'Wo steht der Cursor? Dort den Text einfügen
intPosition = CShort(activeTextbox.SelectionStart)
strTextInForm = activeTextbox.Text
If
Windows.Forms.Clipboard.GetDataObject.GetData(DataFormats.Text) IsNot
Nothing Then
strTextToInsert =
Windows.Forms.Clipboard.GetDataObject.GetData(DataFormats.Text).ToString


strNewInForm = strTextInForm.Insert(intPosition,
strTextToInsert)

activeTextbox.Text = strNewInForm
End If
End If
End Sub
 
G

Graf

Hi Daniel,

thanks for your answer.

Found this quite useful code in the messages, that you sent me. Just
translated it from C# to VB, copy&paste into my code and works :)

Public Overridable Property ActiveControl() As Control
Get
Return GetFocusedControl(Me)
End Get
Set(ByVal value As Control)
If Not value.Focused Then
value.Focus()
End If
End Set
End Property


Private Function GetFocusedControl(ByVal parent As Control) As
Control
If parent.Focused Then
Return parent
End If
Dim ctrl As Control
For Each ctrl In parent.Controls
Dim temp As Control = GetFocusedControl(ctrl)
If Not (temp Is Nothing) Then
Return temp
End If
Next ctrl
Return Nothing
End Function 'GetFocusedControl

Original message from Tim Wilson:
http://groups.google.com/group/micr...k.compactframework/msg/ac56cafb99b8dbb6?hl=en
 

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