TyeJae

Is there a way to shorten this code

Code Snippet

Private Sub CopyToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripButton.Click

If TextBox1.SelectedText <> "" Then

Clipboard.SetDataObject(TextBox1.SelectedText)

End If

If TextBox2.SelectedText <> "" Then

Clipboard.SetDataObject(TextBox2.SelectedText)

End If

If TextBox3.SelectedText <> "" Then

Clipboard.SetDataObject(TextBox3.SelectedText)

End If

If TextBox4.SelectedText <> "" Then

Clipboard.SetDataObject(TextBox4.SelectedText)

End If

If TextBox5.SelectedText <> "" Then

Clipboard.SetDataObject(TextBox4.SelectedText)

End If

If TextBox6.SelectedText <> "" Then

Clipboard.SetDataObject(TextBox4.SelectedText)

End If

If TextBox7.SelectedText <> "" Then

Clipboard.SetDataObject(TextBox4.SelectedText)

End If

If TextBox8.SelectedText <> "" Then

Clipboard.SetDataObject(TextBox4.SelectedText)

End If

End Sub

I also cannot figure out how to paste the clipboard text to whichever textbox i'm currently on. Any help is much appriciated.




Re: Visual Basic Express Edition Is there a way to shorten this?

ReneeC

Yes, there is a way to shorten it with two assumptions

If those are the only texboxes on the form or they are all in a groupbox as both the form and a groupboxs are containers for controls

dim tb as textbox

for each c as control in me.controls ' or gb.controls

if typeof(c) = Textbox then

tb=c

if tb.selectedtext <> "" Then Clipboard.SetDataObject(tb.SelectedText)

end if

next

That's done from memory and you'll have to diddle with it a bit, I'm sure. I think there is a clipboard set text which would be better






Re: Visual Basic Express Edition Is there a way to shorten this?

Solitaire

The first listing should do it if you only have text selected in one of the textboxes in your group, and not another textbox. If you have more textboxes that should not be included, then the second listing should work if you assign a tag property to each of the involved textboxes.

For Each ctl As Control In Controls

If TypeOf ctl Is TextBox Then

Dim txt As TextBox = CType(ctl, TextBox)

Clipboard.SetDataObject(txt.SelectedText)

End If

Next ctl

For Each ctl As Control In Controls

If TypeOf ctl Is TextBox Then

Dim txt As TextBox = CType(ctl, TextBox)

If CType(txt.Tag, String) = "clip" Then

Clipboard.SetDataObject(txt.SelectedText)

End If

End If

Next ctl






Re: Visual Basic Express Edition Is there a way to shorten this?

Tall Dude

If all of the textboxs are children of the main form then:

Code Snippet

Public Class Form1

Dim LastTextbox As String

Private Sub TB_Enter(ByVal sender As System.Object, _

ByVal e As System.EventArgs)

Dim TxtBX As TextBox = DirectCast(sender, TextBox)

LastTextbox = TxtBX.Name

End Sub

Private Sub ToolStripCopy_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles ToolStripCopy.Click

Application.DoEvents()

If LastTextbox IsNot Nothing Then

Clipboard.SetDataObject(CType(Me.Controls(LastTextbox), TextBox).SelectedText)

End If

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles MyBase.Load

For Each tb As Control In Me.Controls

If TypeOf (tb) Is TextBox Then

AddHandler tb.Enter, AddressOf TB_Enter

End If

Next

End Sub

End Class






Re: Visual Basic Express Edition Is there a way to shorten this?

nobugz

Private Sub CopyToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripButton.Click
If Me.ActiveControl IsNot Nothing Then Clipboard.SetText(Me.ActiveControl.Text)
End Sub






Re: Visual Basic Express Edition Is there a way to shorten this?

JohnWein

Hans: That I really like!



Re: Visual Basic Express Edition Is there a way to shorten this?

tyejizzle

nobugz wrote:
Private Sub CopyToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripButton.Click
If Me.ActiveControl IsNot Nothing Then Clipboard.SetText(Me.ActiveControl.Text)
End Sub

This works great, but it copies the whole textbox, I played around with it some but cannot get it to only copy the selected text. Any help






Re: Visual Basic Express Edition Is there a way to shorten this?

Karl Davies-Barrett - MCT

If Me.ActiveControl IsNot Nothing Then Clipboard.SetText(Me.ActiveControl.SelectedText)






Re: Visual Basic Express Edition Is there a way to shorten this?

nobugz

Private Sub CopyToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripButton.Click
Dim ctl As TextBoxBase = TryCast(Me.ActiveControl, TextBoxBase)
If ctl IsNot Nothing AndAlso ctl.SelectionLength > 0 Then Clipboard.SetText(ctl.SelectedText)
End Sub






Re: Visual Basic Express Edition Is there a way to shorten this?

tyejizzle

Very impressed nobugz....thanks ALOT!