can't clear clipboard

  • Thread starter Thread starter dennist
  • Start date Start date
D

dennist

I'm opening up a file and putting it's contents into a
textbox. I want to clear the clipboard before setting the
focus to the textbox, but apparently there's no such
animal in vb.net.

Private Sub btnOpenFile_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnOpenFile.Click

Dim StreamR1 As System.IO.StreamReader
OFD1.Filter = ("text files(*.txt)|*.txt|(Word
files(*.doc)|*.doc")
OFD1.ShowDialog()
If OFD1.FileName <> "" Then
Try 'open file and trap any erros using
handler
StreamR1 = New StreamReader(OFD1.FileName)
txtTextGeneral.Text = StreamR1.ReadToEnd
StreamR1.Close()
txtTextGeneral.Select(0, 0)
Catch
MsgBox("Error opening file." & vbCr
& "Perhaps bad file name", , "Error!")
Finally
txtTextGeneral.Focus()
'MsgBox(txtTextGeneral.SelectedText)
End Try
End If
End Sub

don't mind my debugging message box.

Then, when I leave or lose focus of the text box, I want
to set the clipboard to whatever text is selected in the
textbox. There's lot's of funny debugging statements,
please don't laugh too loudly.

Private Sub txtTextGeneral_LostFocus(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
txtTextGeneral.LostFocus

'' Takes the selected text from a text box and
puts it on the clipboard.
'txtTopicTitle.Text = "Leave" 'see leave
'MsgBox("Leave")
'If txtTextGeneral.SelectedText <> "" Then
' Clipboard.SetDataObject
(txtTextGeneral.SelectedText, True)
'Else
' MsgBox("No text selected", , "Warning")
' txtTitle.Text = ""
'End If

End Sub

what I find is that if I click on a control that can
accept text, I get a 'leave' in the topictext box.
Otherwise, nothing happens. No messagebox, no leave, no
nothing. Why can't I do something in vb.net that's as
simple as pie in vb6. Why can't I clear the clipboard.

In the end, everything works out - I think. I'm sure a
user will find differently.

dennist
 
Hi dennist,
The .NET framework didn't provide such method, so you must use the
Platform Invoke to do this,
the API you need is OpenClipboard, EmptyClipboard and CloseClipboard,
you may declare these functions like,
Public Declare Function OpenClipboard Lib "user32" Alias "OpenClipboard"
(ByVal hwnd As Long) As Long

for more usage information, you may refer to the MSDN page
ms-help://MS.MSDNQTR.2003FEB.1033/winui/winui/windowsuserinterface/dataexcha
nge/
clipboard/clipboardreference/clipboardfunctions/openclipboard.htm

thanks, if you still have problem, please let me know.

Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| Content-Class: urn:content-classes:message
| From: "dennist" <[email protected]>
| Sender: "dennist" <[email protected]>
| Subject: can't clear clipboard
| Date: Tue, 2 Sep 2003 19:28:06 -0700
| Lines: 65
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNxwwIW3y2/YXyJSY6/VsEsLgLmfQ==
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51520
| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| I'm opening up a file and putting it's contents into a
| textbox. I want to clear the clipboard before setting the
| focus to the textbox, but apparently there's no such
| animal in vb.net.
|
| Private Sub btnOpenFile_Click(ByVal sender As
| System.Object, ByVal e As System.EventArgs) Handles
| btnOpenFile.Click
|
| Dim StreamR1 As System.IO.StreamReader
| OFD1.Filter = ("text files(*.txt)|*.txt|(Word
| files(*.doc)|*.doc")
| OFD1.ShowDialog()
| If OFD1.FileName <> "" Then
| Try 'open file and trap any erros using
| handler
| StreamR1 = New StreamReader(OFD1.FileName)
| txtTextGeneral.Text = StreamR1.ReadToEnd
| StreamR1.Close()
| txtTextGeneral.Select(0, 0)
| Catch
| MsgBox("Error opening file." & vbCr
| & "Perhaps bad file name", , "Error!")
| Finally
| txtTextGeneral.Focus()
| 'MsgBox(txtTextGeneral.SelectedText)
| End Try
| End If
| End Sub
|
| don't mind my debugging message box.
|
| Then, when I leave or lose focus of the text box, I want
| to set the clipboard to whatever text is selected in the
| textbox. There's lot's of funny debugging statements,
| please don't laugh too loudly.
|
| Private Sub txtTextGeneral_LostFocus(ByVal sender As
| Object, ByVal e As System.EventArgs) Handles
| txtTextGeneral.LostFocus
|
| '' Takes the selected text from a text box and
| puts it on the clipboard.
| 'txtTopicTitle.Text = "Leave" 'see leave
| 'MsgBox("Leave")
| 'If txtTextGeneral.SelectedText <> "" Then
| ' Clipboard.SetDataObject
| (txtTextGeneral.SelectedText, True)
| 'Else
| ' MsgBox("No text selected", , "Warning")
| ' txtTitle.Text = ""
| 'End If
|
| End Sub
|
| what I find is that if I click on a control that can
| accept text, I get a 'leave' in the topictext box.
| Otherwise, nothing happens. No messagebox, no leave, no
| nothing. Why can't I do something in vb.net that's as
| simple as pie in vb6. Why can't I clear the clipboard.
|
| In the end, everything works out - I think. I'm sure a
| user will find differently.
|
| dennist
|
 
I can see by your answer that I can solve the clear
clipboard problem, although it is certainly more
difficult than in visual basic 6. In some regards I
think MS took a wrong turn in vb.net. They were catering
to the elite programmers and not the average vb
programmer. Easy things become complicated.

However your answer didn't indicate my problem with
leaving or losingfocus of the textbox. It seems to me no
matter what I click outside the textbox should
create "leave" in the txtTopicTitle control. Don't tell
me we've lost something so fundamental and simple and
that, as well?

dennist
 
Hi dennist,
what I find is that if I click on a control that can
accept text, I get a 'leave' in the topictext box.
Otherwise, nothing happens. No messagebox, no leave, no
nothing.
What do you mean *control that can accept focus*?
If you click on any control that can get the input focus the textbox will
receive LostFocus event.
If you click on a control like panel, which obviously cannot be focused the
text boxt won't lose the focus and won't get the LostFocus event.

B\rgds
100
 
oops. If I click text boxes or comboboxes, which can
certainly accept focus, the leave or lostfocus events of
the txttextgeneral do not fire.

dennist
 
Hi Dennis,
Clicking on controls that can receive focus should change the focus to it,
It's strange that the event handlers don't fire, I think there might be
somthing wrong in your code. Have you tried this it a new VB Application
Project? Just Add a TextBox and a Button on it, then handle the GotFocus
and LostFocus event of the TextBox, and set the different values to the
TextProperty, then run it for test.
If the LostFocus event still not fired ,please sent that project to me.
Thanks!


Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| Content-Class: urn:content-classes:message
| From: "dennist" <[email protected]>
| Sender: "dennist" <[email protected]>
| References: <[email protected]>
<#gPm7#[email protected]>
| Subject: Re: can't clear clipboard
| Date: Thu, 4 Sep 2003 23:32:26 -0700
| Lines: 27
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNzd3k98uXnImREQ6+ynz6PLyK+/w==
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51691
| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| oops. If I click text boxes or comboboxes, which can
| certainly accept focus, the leave or lostfocus events of
| the txttextgeneral do not fire.
|
| dennist
| >-----Original Message-----
| >Hi dennist,
| >> what I find is that if I click on a control that can
| >> accept text, I get a 'leave' in the topictext box.
| >> Otherwise, nothing happens. No messagebox, no leave,
| no
| >> nothing.
| >What do you mean *control that can accept focus*?
| >If you click on any control that can get the input focus
| the textbox will
| >receive LostFocus event.
| >If you click on a control like panel, which obviously
| cannot be focused the
| >text boxt won't lose the focus and won't get the
| LostFocus event.
| >
| >B\rgds
| >100
| >
| >
| >.
| >
|
 
Hi dennist,
I tried your code and everything works just fine. It looks like the problems
is somewhere else. If you can post a working code which, demostrates the
problem we might be able help you better.

B\rgds
100
 
I've fixed this as well as I'm going to. I just hope in
the next version of vb.net they return clipboard.clear
and a more robust lostfocus event to us. It'll do for
now.

Thank you both for your work and suggestions.

dennist
 
Back
Top